/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

  1. // AForge Image Processing Library
  2. // AForge.NET framework
  3. //
  4. // Copyright © Andrew Kirillov, 2005-2007
  5. // andrew.kirillov@gmail.com
  6. //
  7. namespace AForge.Imaging.Filters
  8. {
  9. using System;
  10. using System.Drawing;
  11. using System.Drawing.Imaging;
  12. /// <summary>
  13. /// Threshold binarization.
  14. /// </summary>
  15. ///
  16. /// <remarks><para>The filter does image binarization using specified threshold value. All pixels
  17. /// with intensities equal or higher than threshold value are converted to white pixels. All other
  18. /// pixels with intensities below threshold value are converted to black pixels.</para>
  19. /// <para>Sample usage:</para>
  20. /// <code>
  21. /// // create filter
  22. /// Threshold filter = new Threshold( 100 );
  23. /// // apply the filter
  24. /// filter.ApplyInPlace( image );
  25. /// </code>
  26. /// <para><b>Initial image:</b></para>
  27. /// <img src="grayscale.jpg" width="480" height="361" />
  28. /// <para><b>Result image:</b></para>
  29. /// <img src="threshold.jpg" width="480" height="361" />
  30. /// </remarks>
  31. ///
  32. public class Threshold : FilterGrayToGrayPartial
  33. {
  34. /// <summary>
  35. /// Threshold value.
  36. /// </summary>
  37. protected byte threshold = 128;
  38. /// <summary>
  39. /// Threshold value.
  40. /// </summary>
  41. ///
  42. /// <remarks>Default value is 128.</remarks>
  43. ///
  44. public byte ThresholdValue
  45. {
  46. get { return threshold; }
  47. set { threshold = value; }
  48. }
  49. /// <summary>
  50. /// Initializes a new instance of the <see cref="Threshold"/> class.
  51. /// </summary>
  52. ///
  53. public Threshold( ) { }
  54. /// <summary>
  55. /// Initializes a new instance of the <see cref="Threshold"/> class.
  56. /// </summary>
  57. ///
  58. /// <param name="threshold">Threshold value.</param>
  59. ///
  60. public Threshold( byte threshold )
  61. {
  62. this.threshold = threshold;
  63. }
  64. /// <summary>
  65. /// Process the filter on the specified image.
  66. /// </summary>
  67. ///
  68. /// <param name="imageData">Image data.</param>
  69. /// <param name="rect">Image rectangle for processing by the filter.</param>
  70. ///
  71. protected override unsafe void ProcessFilter( BitmapData imageData, Rectangle rect )
  72. {
  73. int startX = rect.Left;
  74. int startY = rect.Top;
  75. int stopX = startX + rect.Width;
  76. int stopY = startY + rect.Height;
  77. int offset = imageData.Stride - rect.Width;
  78. // do the job
  79. byte* ptr = (byte*) imageData.Scan0.ToPointer( );
  80. // allign pointer to the first pixel to process
  81. ptr += ( startY * imageData.Stride + startX );
  82. // for each line
  83. for ( int y = startY; y < stopY; y++ )
  84. {
  85. // for each pixel
  86. for ( int x = startX; x < stopX; x++, ptr++ )
  87. {
  88. *ptr = (byte) ( ( *ptr >= threshold ) ? 255 : 0 );
  89. }
  90. ptr += offset;
  91. }
  92. }
  93. }
  94. }