Click here to Skip to main content
15,893,668 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 830.9K   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>
	/// Intersect filter - get MIN of two pixels
	/// </summary>
	/// 
	/// <remarks></remarks>
	/// 
	public sealed class Intersect : FilterAnyToAny
	{
		private Bitmap	overlayImage;
		private Point	overlayPos = new Point( 0, 0 );

		/// <summary>
		/// Overlay image
		/// </summary>
		public Bitmap OverlayImage
		{
			get { return overlayImage; }
			set { overlayImage = value; }
		}

		/// <summary>
		/// Overlay position
		/// </summary>
		public Point OverlayPos
		{
			get { return overlayPos; }
			set { overlayPos = value; }
		}

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

		/// <summary>
		/// Initializes a new instance of the <see cref="Merge"/> class
		/// </summary>
		/// 
		/// <param name="overlayImage">Overlay image</param>
		/// 
		public Intersect( Bitmap overlayImage )
		{
			this.overlayImage = overlayImage;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="Merge"/> class
		/// </summary>
		/// 
		/// <param name="overlayImage">Overlay image</param>
		/// <param name="position">Overlay position</param>
		/// 
		public Intersect( Bitmap overlayImage, Point position )
		{
			this.overlayImage = overlayImage;
			this.overlayPos = position;
		}

		/// <summary>
		/// Process the filter on the specified image
		/// </summary>
		/// 
		/// <param name="imageData">image data</param>
		/// 
		protected override unsafe void ProcessFilter( BitmapData imageData )
		{
			// source image and overlay must have same pixel format
			if ( imageData.PixelFormat != overlayImage.PixelFormat )
				throw new ArgumentException( "Source and overlay images must have same pixel format " );

			// get image dimension
			int width	= imageData.Width;
			int height	= imageData.Height;

			// overlay position and dimension
			int ovrX = overlayPos.X;
			int ovrY = overlayPos.Y;
			int ovrW = overlayImage.Width;
			int ovrH = overlayImage.Height;

			// lock overlay image
			BitmapData ovrData = overlayImage.LockBits(
				new Rectangle( 0, 0, ovrW, ovrH ),
				ImageLockMode.ReadOnly, imageData.PixelFormat );

			// initialize other variables
			int pixelSize = ( imageData.PixelFormat == PixelFormat.Format8bppIndexed ) ? 1 : 3;
			int stride = imageData.Stride;
			int offset = stride - pixelSize * width;
			int ovrStide = ovrData.Stride;
			int ovrOffset, lineSize;

			// do the job
			byte * ptr = (byte *) imageData.Scan0.ToPointer( );
			byte * ovr = (byte *) ovrData.Scan0.ToPointer( );

			if ( ( width == ovrW ) && ( height == ovrH ) && ( ovrX == 0 ) && ( ovrY == 0 ) )
			{
				// overlay image has the same size as the source image and its position is (0, 0)
				lineSize = width * pixelSize;

				// for each line
				for ( int y = 0; y < height; y++ )
				{
					// for each pixel
					for ( int x = 0; x < lineSize; x++, ptr++, ovr++ )
					{
						if ( *ovr < *ptr )
							*ptr = *ovr;
					}
					ptr += offset;
					ovr += offset;
				}
			}
			else
			{
				// align Y
				if ( ovrY >= 0 )
				{
					ptr += stride * ovrY;
				}
				else
				{
					ovr -= ovrStide * ovrY;
					ovrH += ovrY;
					ovrY = 0;
				}

				// align X
				if ( ovrX >= 0 )
				{
					ptr += pixelSize * ovrX;
				}
				else
				{
					ovr -= pixelSize * ovrX;
					ovrW += ovrX;
					ovrX = 0;
				}

				// update overlay width and height
				ovrW = Math.Min( ovrW, width - ovrX );
				ovrH = Math.Min( ovrH, height - ovrY );

				// update offset
				ovrOffset = ovrStide - ovrW * pixelSize;
				offset = stride - ovrW * pixelSize;

				if ( ( ovrW > 0 ) && ( ovrH > 0 ) && ( ovrX < width ) && ( ovrY < height ) )
				{
					lineSize = pixelSize * ovrW;

					// for each line
					for ( int y = 0; y < ovrH; y++ )
					{
						// for each pixel
						for ( int x = 0; x < lineSize; x++, ptr++, ovr++ )
						{
							if ( *ovr < *ptr )
								*ptr = *ovr;
						}
						ptr += offset;
						ovr += ovrOffset;
					}
				}
			}

			// unlock overlay image
			overlayImage.UnlockBits( ovrData );
		}
	}
}

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