/bin/AForge.NET/Sources/Imaging/Filters/Color Filters/ContrastStretch.cs

https://bitbucket.org/VahidN/neural-net-digit-recognition · C# · 147 lines · 71 code · 22 blank · 54 comment · 15 complexity · ddad8a4307a3b3ca620883efbf935d84 MD5 · raw file

  1. // AForge Image Processing Library
  2. // AForge.NET framework
  3. //
  4. // Copyright ©
  5. // Andrew Kirillov (andrew.kirillov@gmail.com),
  6. // Mladen Prajdic (spirit1_fe@yahoo.com)
  7. // 2005-2008
  8. //
  9. namespace AForge.Imaging.Filters
  10. {
  11. using System;
  12. using System.Drawing;
  13. using System.Drawing.Imaging;
  14. /// <summary>
  15. /// Contrast stretching filter.
  16. /// </summary>
  17. ///
  18. /// <remarks><para>Contrast stretching (or as it is often called normalization) is a simple image enhancement
  19. /// technique that attempts to improve the contrast in an image by 'stretching' the range of intensity values
  20. /// it contains to span a desired range of values, e.g. the the full range of pixel values that the image type
  21. /// concerned allows. It differs from the more sophisticated <seealso cref="HistogramEqualization">histogram equalization</seealso>
  22. /// in that it can only apply a linear scaling function to the image pixel values.</para>
  23. ///
  24. /// <para>The result of this filter may be achieved by using <see cref="ImageStatistics"/> class, which allows to
  25. /// get pixels' intensities histogram, and <see cref="LevelsLinear"/> filter, which does linear correction
  26. /// of pixel's intensities.</para>
  27. ///
  28. /// <para><note>The class processes only grayscale (8 bpp indexed) and color (24 bpp) images.</note></para>
  29. ///
  30. /// <para>Sample usage:</para>
  31. /// <code>
  32. /// // create filter
  33. /// ContrastStretch filter = new ContrastStretch( );
  34. /// // process image
  35. /// filter.ApplyInPlace( sourceImage );
  36. /// </code>
  37. ///
  38. /// <para><b>Source image:</b></para>
  39. /// <img src="sample5.jpg" width="480" height="387" />
  40. /// <para><b>Result image:</b></para>
  41. /// <img src="contrast_stretch.jpg" width="480" height="387" />
  42. /// </remarks>
  43. ///
  44. public class ContrastStretch : FilterAnyToAnyPartial
  45. {
  46. /// <summary>
  47. /// Process the filter on the specified image.
  48. /// </summary>
  49. ///
  50. /// <param name="imageData">Image data.</param>
  51. /// <param name="rect">Image rectangle for processing by the filter.</param>
  52. ///
  53. protected override unsafe void ProcessFilter( BitmapData imageData, Rectangle rect )
  54. {
  55. int startX = rect.Left;
  56. int startY = rect.Top;
  57. int stopX = startX + rect.Width;
  58. int stopY = startY + rect.Height;
  59. int stride = imageData.Stride;
  60. int offset = stride - rect.Width * ( ( imageData.PixelFormat == PixelFormat.Format8bppIndexed ) ? 1 : 3 );
  61. // levels linear correction filter is going to be used on STEP 2
  62. LevelsLinear levelsLinear = new LevelsLinear( );
  63. // STEP 1 - search for min and max pixel values
  64. byte* ptr = (byte*) imageData.Scan0.ToPointer( );
  65. // check image format
  66. if ( imageData.PixelFormat == PixelFormat.Format8bppIndexed )
  67. {
  68. // allign pointer to the first pixel to process
  69. ptr += ( startY * stride + startX );
  70. byte min = 255;
  71. byte max = 0;
  72. for ( int y = startY; y < stopY; y++ )
  73. {
  74. for ( int x = startX; x < stopX; x++, ptr++ )
  75. {
  76. byte value = *ptr;
  77. if ( value < min )
  78. min = value;
  79. if ( value > max )
  80. max = value;
  81. }
  82. ptr += offset;
  83. }
  84. levelsLinear.InGray = new IntRange( min, max );
  85. }
  86. else
  87. {
  88. // allign pointer to the first pixel to process
  89. ptr += ( startY * stride + startX * 3 );
  90. byte minR = 255, minG = 255, minB = 255;
  91. byte maxR = 0, maxG = 0, maxB = 0;
  92. for ( int y = startY; y < stopY; y++ )
  93. {
  94. for ( int x = startX; x < stopX; x++, ptr += 3 )
  95. {
  96. // red
  97. byte value = ptr[RGB.R];
  98. if ( value < minR )
  99. minR = value;
  100. if ( value > maxR )
  101. maxR = value;
  102. // green
  103. value = ptr[RGB.G];
  104. if ( value < minG )
  105. minG = value;
  106. if ( value > maxG )
  107. maxG = value;
  108. // blue
  109. value = ptr[RGB.B];
  110. if ( value < minB )
  111. minB = value;
  112. if ( value > maxB )
  113. maxB = value;
  114. }
  115. ptr += offset;
  116. }
  117. levelsLinear.InRed = new IntRange( minR, maxR );
  118. levelsLinear.InGreen = new IntRange( minG, maxG );
  119. levelsLinear.InBlue = new IntRange( minB, maxB );
  120. }
  121. // STEP 2 - run levels linear correction
  122. levelsLinear.ApplyInPlace( imageData, rect );
  123. }
  124. }
  125. }