Click here to Skip to main content
15,885,546 members
Articles / Artificial Intelligence

AForge.NET open source framework

Rate me:
Please Sign up or sign in to vote.
4.97/5 (150 votes)
16 May 2007GPL311 min read 828.1K   48.3K   346  
The article describes an open source C# framework for researchers in the areas of Computer Vision and Artificial Intelligence - image processing, neural networks, genetic algorithms, etc.
// AForge Image Processing Library
//
// Copyright � Andrew Kirillov, 2005-2006
// andrew.kirillov@gmail.com
//

namespace AForge.Imaging.Filters
{
	using System;
	using System.Drawing;
	using System.Drawing.Imaging;

	/// <summary>
	/// Homogenity edge detector
	/// </summary>
	/// 
	/// <remarks></remarks>
	/// 
	public class HomogenityEdgeDetector : FilterColorToGray
	{
		/// <summary>
		/// Process the filter on the specified image
		/// </summary>
		/// 
		/// <param name="sourceData">Source image data</param>
		/// <param name="destinationData">Destination image data</param>
		/// 
		protected override unsafe void ProcessFilter( BitmapData sourceData, BitmapData destinationData )
		{
			byte * src = null;
			byte * dst = (byte *) destinationData.Scan0.ToPointer( );

			// get width and height
			int width = sourceData.Width;
			int height = sourceData.Height;
			int widthM1 = width - 1;
			int heightM1 = height - 1;
			int stride = destinationData.Stride;
			int offset = stride - width;

			int d, max, v;

			// convert image to grayscale if it is color
			Bitmap		grayImage = null;
			BitmapData	grayData = null;

			if ( sourceData.PixelFormat != PixelFormat.Format8bppIndexed )
			{
				// create grayscale filter
				IFilter filter = new GrayscaleRMY( );
				// do the processing
				grayImage = filter.Apply( sourceData );
				// lock the image
				grayData = grayImage.LockBits(
					new Rectangle( 0, 0, width, height ),
					ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed );
				// set source pointer
				src = (byte *) grayData.Scan0.ToPointer( );
			}
			else
			{
				src = (byte *) sourceData.Scan0.ToPointer( );
			}

			// do the processing job

			// skip one stride
			src += stride;
			dst += stride;

			// for each line
			for ( int y = 1; y < heightM1; y++ )
			{
				src ++;
				dst ++;

				// for each pixel
				for ( int x = 1; x < widthM1; x++, src++, dst++ )
				{
					max = 0;
					v = *src;

					// top-left
					d = v - src[-stride - 1];
					if ( d < 0 )
						d = -d;
					if ( d > max )
						max = d;
					// top
					d = v - src[-stride];
					if ( d < 0 )
						d = -d;
					if ( d > max )
						max = d;
					// top-right
					d = v - src[-stride + 1];
					if ( d < 0 )
						d = -d;
					if ( d > max )
						max = d;
					// left
					d = v - src[-1];
					if ( d < 0 )
						d = -d;
					if ( d > max )
						max = d;
					// right
					d = v - src[1];
					if ( d < 0 )
						d = -d;
					if ( d > max )
						max = d;
					// bottom-left
					d = v - src[stride - 1];
					if ( d < 0 )
						d = -d;
					if ( d > max )
						max = d;
					// bottom
					d = v - src[stride];
					if ( d < 0 )
						d = -d;
					if ( d > max )
						max = d;
					// bottom-right
					d = v - src[stride + 1];
					if ( d < 0 )
						d = -d;
					if ( d > max )
						max = d;

					*dst = (byte) max;
				}
				src += offset + 1;
				dst += offset + 1;
			}

			// release gray image, if there was conversion
			if ( grayData != null )
			{
				grayImage.UnlockBits( grayData );
				grayImage.Dispose( );
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Software Developer IBM
United Kingdom United Kingdom
Started software development at about 15 years old and it seems like now it lasts most part of my life. Fortunately did not spend too much time with Z80 and BK0010 and switched to 8086 and further. Similar with programming languages – luckily managed to get away from BASIC and Pascal to things like Assembler, C, C++ and then C#. Apart from daily programming for food, do it also for hobby, where mostly enjoy areas like Computer Vision, Robotics and AI. This led to some open source stuff like AForge.NET, Computer Vision Sandbox, cam2web, ANNT, etc.

Comments and Discussions