Click here to Skip to main content
15,880,796 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 826.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>
	/// Threshold binarization with error carry
	/// </summary>
	/// 
	/// <remarks></remarks>
	/// 
	public class ThresholdWithCarry : FilterAnyToGray
	{
		private byte threshold = 128;

		/// <summary>
		/// Threshold value (default is 128)
		/// </summary>
		public byte ThresholdValue
		{
			get { return threshold; }
			set { threshold = value; }
		}

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

		/// <summary>
		/// Initializes a new instance of the <see cref="ThresholdWithCarry"/> class
		/// </summary>
		/// 
		/// <param name="threshold">Threshold value</param>
		/// 
		public ThresholdWithCarry( byte threshold )
		{
			this.threshold = threshold;
		}
		
		/// <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 width and height
			int width = sourceData.Width;
			int height = sourceData.Height;

			int srcOffset = sourceData.Stride - ( ( sourceData.PixelFormat == PixelFormat.Format8bppIndexed ) ? width : width * 3 );
			int dstOffset = destinationData.Stride - width;

			short carry = 0;

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

			if ( sourceData.PixelFormat == PixelFormat.Format8bppIndexed )
			{
				// graysclae binarization
				for ( int y = 0; y < height; y++ )
				{
					for ( int x = 0; x < width; x++, src++, dst++ )
					{
						carry += *src;

						if ( carry >= threshold )
						{
							*dst = (byte) 255;
							carry -= 255;
						}
						else
						{
							*dst = (byte) 0;
						}
					}
					src += srcOffset;
					dst += dstOffset;
				}
			}
			else
			{
				// RGB binarization
				for ( int y = 0; y < height; y++ )
				{
					for ( int x = 0; x < width; x++, src += 3, dst ++ )
					{
						// grayscale value using BT709
						carry += (byte)( 0.2125 * src[RGB.R] + 0.7154 * src[RGB.G] + 0.0721 * src[RGB.B] );

						if ( carry > threshold )
						{
							*dst = (byte) 255;
							carry -= 255;
						}
						else
						{
							*dst = (byte) 0;
						}
					}
					src += srcOffset;
					dst += dstOffset;
				}
			}
		}
	}
}

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