/bin/AForge.NET/Sources/Imaging/Filters/Binarization/Threshold.cs
https://bitbucket.org/VahidN/neural-net-digit-recognition · C# · 103 lines · 38 code · 9 blank · 56 comment · 2 complexity · ce2d7cbffe911adfd61e86f631ce9c0a MD5 · raw file
- // AForge Image Processing Library
- // AForge.NET framework
- //
- // Copyright © Andrew Kirillov, 2005-2007
- // andrew.kirillov@gmail.com
- //
-
- namespace AForge.Imaging.Filters
- {
- using System;
- using System.Drawing;
- using System.Drawing.Imaging;
-
- /// <summary>
- /// Threshold binarization.
- /// </summary>
- ///
- /// <remarks><para>The filter does image binarization using specified threshold value. All pixels
- /// with intensities equal or higher than threshold value are converted to white pixels. All other
- /// pixels with intensities below threshold value are converted to black pixels.</para>
- /// <para>Sample usage:</para>
- /// <code>
- /// // create filter
- /// Threshold filter = new Threshold( 100 );
- /// // apply the filter
- /// filter.ApplyInPlace( image );
- /// </code>
- /// <para><b>Initial image:</b></para>
- /// <img src="grayscale.jpg" width="480" height="361" />
- /// <para><b>Result image:</b></para>
- /// <img src="threshold.jpg" width="480" height="361" />
- /// </remarks>
- ///
- public class Threshold : FilterGrayToGrayPartial
- {
- /// <summary>
- /// Threshold value.
- /// </summary>
- protected byte threshold = 128;
-
- /// <summary>
- /// Threshold value.
- /// </summary>
- ///
- /// <remarks>Default value is 128.</remarks>
- ///
- public byte ThresholdValue
- {
- get { return threshold; }
- set { threshold = value; }
- }
-
- /// <summary>
- /// Initializes a new instance of the <see cref="Threshold"/> class.
- /// </summary>
- ///
- public Threshold( ) { }
-
- /// <summary>
- /// Initializes a new instance of the <see cref="Threshold"/> class.
- /// </summary>
- ///
- /// <param name="threshold">Threshold value.</param>
- ///
- public Threshold( byte threshold )
- {
- this.threshold = threshold;
- }
-
- /// <summary>
- /// Process the filter on the specified image.
- /// </summary>
- ///
- /// <param name="imageData">Image data.</param>
- /// <param name="rect">Image rectangle for processing by the filter.</param>
- ///
- protected override unsafe void ProcessFilter( BitmapData imageData, Rectangle rect )
- {
- int startX = rect.Left;
- int startY = rect.Top;
- int stopX = startX + rect.Width;
- int stopY = startY + rect.Height;
- int offset = imageData.Stride - rect.Width;
-
- // do the job
- byte* ptr = (byte*) imageData.Scan0.ToPointer( );
-
- // allign pointer to the first pixel to process
- ptr += ( startY * imageData.Stride + startX );
-
- // for each line
- for ( int y = startY; y < stopY; y++ )
- {
- // for each pixel
- for ( int x = startX; x < stopX; x++, ptr++ )
- {
- *ptr = (byte) ( ( *ptr >= threshold ) ? 255 : 0 );
- }
- ptr += offset;
- }
- }
- }
- }