Click here to Skip to main content
15,896,512 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 831.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-2007
// andrew.kirillov@gmail.com
//
// Original idea found in Paint.NET project
// http://www.eecs.wsu.edu/paint.net/
//
namespace AForge.Imaging.Filters
{
	using System;
	using System.Drawing;
	using System.Drawing.Imaging;
	
	/// <summary>
	/// Extended sharpen filter
	/// </summary>
    /// 
    /// <break></break>
    /// 
	public class SharpenEx : IFilter
	{
		private Correlation	filter;
		private double		sigma = 1.4;
		private int			size = 5;

        /// <summary>
        /// Gaussian sigma value
        /// </summary>
        /// 
        /// <remarks>Sigma value for Gaussian function used to calculate
        /// the kernel. Default value is 1.4. Minimum value is 0.5. Maximum
        /// value is 5.0.</remarks>
        /// 
        public double Sigma
		{
			get { return sigma; }
			set
			{
				// get new sigma value
				sigma = Math.Max( 0.5, Math.Min( 5.0, value ) );
				// create filter
				CreateFilter();
			}
		}

        /// <summary>
        /// Kernel size
        /// </summary>
        /// 
        /// <remarks>Size of Gaussian kernel. Default value is 5. Minimum value is 3.
        /// Maximum value is 5. The value should be odd.</remarks>
        /// 
		public int Size
		{
			get { return size; }
			set
			{
				size = Math.Max( 3, Math.Min( 21, value | 1 ) );
				CreateFilter( );
			}
		}

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

        /// <summary>
        /// Initializes a new instance of the <see cref="SharpenEx"/> class
        /// </summary>
        /// 
        /// <param name="sigma">Gaussian sigma value</param>
        /// 
        public SharpenEx( double sigma )
		{
			Sigma = sigma;
		}

        /// <summary>
        /// Initializes a new instance of the <see cref="SharpenEx"/> class
        /// </summary>
        /// 
        /// <param name="sigma">Gaussian sigma value</param>
        /// <param name="size">Kernel size</param>
        /// 
        public SharpenEx( double sigma, int size )
		{
			Sigma   = sigma;
			Size    = size;
		}

        /// <summary>
        /// Apply filter to an image
        /// </summary>
        /// 
        /// <param name="image">Source image to apply filter to</param>
        /// 
        /// <returns>Returns filter's result obtained by applying the filter to
        /// the source image</returns>
        /// 
        /// <remarks>The method keeps the source image unchanged and returns the
        /// the result of image processing filter as new image.</remarks> 
        ///
        public Bitmap Apply( Bitmap image )
        {
            return filter.Apply( image );
        }

        /// <summary>
        /// Apply filter to an image
        /// </summary>
        /// 
        /// <param name="imageData">Source image to apply filter to</param>
        /// 
        /// <returns>Returns filter's result obtained by applying the filter to
        /// the source image</returns>
        /// 
        /// <remarks>The filter accepts birmap data as input and returns the result
        /// of image processing filter as new image. The source image data are kept
        /// unchanged.</remarks>
        /// 
        public Bitmap Apply( BitmapData imageData )
        {
            return filter.Apply( imageData );
        }

		// Private members
		#region Private Members

		// Create Gaussian filter
		private void CreateFilter ()
		{
			// create Gaussian function
			AForge.Math.Gaussian gaus = new AForge.Math.Gaussian( sigma );

			// create Gaussian kernel
			int[,] kernel = gaus.KernelDiscret2D( size );

			// calculte sum of the kernel
			int sum = 0;

			for ( int i = 0; i < size; i++ )
			{
				for ( int j = 0; j < size; j++ )
				{
					sum += kernel[i, j];
				}
			}

			// recalc kernel
			int c = size >> 1;

			for ( int i = 0; i < size; i++ )
			{
				for ( int j = 0; j < size; j++ )
				{
					if ( ( i == c ) && ( j == c ) )
					{
						// calculate central value
						kernel[i, j] = 2 * sum - kernel[i, j];
					}
					else
					{
						// invert value
						kernel[i, j] = -kernel[i, j];
					}
				}
			}

			// create filter
			filter = new Correlation( kernel );
		}
		#endregion
	}
}

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