Click here to Skip to main content
15,883,841 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 827.8K   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>
	/// Dilatation operator from Mathematical Morphology
	/// </summary>
	/// 
	/// <remarks></remarks>
	/// 
	public class Dilatation : FilterGrayToGrayNewSameSize
	{
		// structuring element
		private short[,]	se = new short[3, 3] { { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } };
		private int			size = 3;

		/// <summary>
		/// Initializes a new instance of the <see cref="Dilatation"/> class
		/// </summary>
		public Dilatation( ) { }

		/// <summary>
		/// Initializes a new instance of the <see cref="Dilatation"/> class
		/// </summary>
		/// 
		/// <param name="se">Structuring element</param>
		/// 
		public Dilatation( short[,] se )
		{
			int s = se.GetLength( 0 );

			// check structuring element size
			if ( ( s != se.GetLength( 1 ) ) || ( s < 3 ) || ( s > 25 ) || ( s % 2 == 0 ) )
				throw new ArgumentException( );

			this.se = se;
			this.size = s;
		}

		/// <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 )
		{
			// get image size
			int width	= destinationData.Width;
			int height	= destinationData.Height;
			int stride	= destinationData.Stride;
			int offset	= stride - width;
			// loop and array indexes
			int	t, ir, jr, i, j;
			// structuring element's radius
			int	r = size >> 1;
			// pixel value
			byte max, v;

			// do the job
			byte * src = (byte *) sourceData.Scan0.ToPointer( );
			byte * dst = (byte *) destinationData.Scan0.ToPointer( );

			// for each line
			for ( int y = 0; y < height; y++ )
			{
				// for each pixel
				for ( int x = 0; x < width; x++, src++, dst++ )
				{
					max = 0;

					// for each structuring element's row
					for ( i = 0; i < size; i++ )
					{
						ir = i - r;
						t = y + ir;

						// skip row
						if ( t < 0 )
							continue;
						// break
						if ( t >= height )
							break;

						// for each structuring slement's column
						for ( j = 0; j < size; j++ )
						{
							jr = j - r;
							t = x + jr;

							// skip column
							if ( t < 0 )
								continue;
							if ( t < width )
							{
								if ( se[i, j] == 1 )
								{
									// get new MAX value
									v = src[ir * stride + jr];
									if ( v > max )
										max = v;
								}
							}
						}
					}
					// result pixel
					*dst = max;
				}
				src += offset;
				dst += offset;
			}
		}
	}
}

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