飞道的博客

C#,图像二值化(02)——用于图像二值化处理的一些基本图像处理函数之C#源代码

302人阅读  评论(0)

Image binarization

The binarization process is a conversion of a color or grayscale image into a two-color black and white image. The main parameter of this conversion is the threshold t, with the value of which the brightness of all is then compared.

Binarization is important in digital image processing, mainly in computer vision applications. Thresholding is an efficient technique in binarization. The choice of thresholding technique is crucial in binarization. There are various thresholding algorithms have been proposed to define the optimal threshold value.

Binarization can be used in recognising text and symbols, e.g. document processing. Identifying objects with distinctive silhouettes, e.g. components on a conveyor in a manufacturing plant, and determining the orientation of objects are some other examples of binarization applications. Binarization generally involves two steps including the determination of a gray threshold according to some objective criteria and assigning each pixel to one class of background or foreground. If the intensity of the pixel is greater than the determined threshold then it belongs to the foreground class and otherwise to the background. The main problem in binarization is the choice of thresholding technique.

Grayscale image

A grayscale image is the one that has the same value for each channel. Grayscale images have only colours of gray shades, Gray shades are not only black and white. If the image has 8-bit depth. It can have 255 shades of gray between dark and white. Sometimes grayscale images are also called black and white images.
 

Some Support C# Sourcecodes POWER BY TRUFFER.CN

本页面的C#代码包括但不限于:图像Bitmap转byte[]数组;灰度直方图计算;灰度最大值、灰度最小值计算;直方图归一化计算等等。


  
  1. using System;
  2. using System.Linq;
  3. using System.Text;
  4. using System.Drawing;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Runtime.InteropServices;
  8. using System.Drawing.Imaging;
  9. namespace Legalsoft.Truffer.ImageTools
  10. {
  11. public static partial class BinarizationHelper
  12. {
  13. #region 基础函数
  14. /// <summary>
  15. /// 图片转存为byte数组
  16. /// </summary>
  17. /// <param name="bitmap"></param>
  18. /// <returns></returns>
  19. public static byte[] BitmapToArray(Bitmap bitmap, out int img_stride)
  20. {
  21. try
  22. {
  23. // 定义锁定bitmap的rect的指定范围区域
  24. Rectangle rect = new Rectangle( 0, 0, bitmap.Width, bitmap.Height);
  25. // 加锁区域像素
  26. var bitmapData = bitmap.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
  27. // 位图的首地址
  28. var ptr = bitmapData.Scan0;
  29. // stride:扫描行
  30. img_stride = bitmapData.Stride;
  31. int len = bitmapData.Stride * bitmap.Height;
  32. byte[] img_bytes = new byte[len];
  33. // 锁定区域的像素值copy到byte数组中
  34. Marshal.Copy(ptr, img_bytes, 0, len);
  35. // 解锁
  36. bitmap.UnlockBits(bitmapData);
  37. return img_bytes;
  38. }
  39. catch (Exception ex)
  40. {
  41. throw new Exception( "BitmapHelper.BitmapToArray ERROR:" + ex.Message);
  42. }
  43. }
  44. /// <summary>
  45. /// 极大极小值限定(0-255)
  46. /// </summary>
  47. /// <param name="v"></param>
  48. /// <returns></returns>
  49. public static double Clamp(double v)
  50. {
  51. v = (v > 255.0) ? 255.0 : ((v < 0.0) ? 0.0 : v);
  52. return v;
  53. }
  54. /// <summary>
  55. /// 灰度计算(简单)
  56. /// </summary>
  57. /// <param name="r"></param>
  58. /// <param name="g"></param>
  59. /// <param name="r"></param>
  60. /// <returns></returns>
  61. public static byte GreyByte(byte r, byte g, byte b)
  62. {
  63. return ( byte)Clamp(r * 0.299 + g * 0.587 + b * 0.114);
  64. }
  65. /// <summary>
  66. /// 彩色图转灰度图
  67. /// </summary>
  68. /// <param name="data"></param>
  69. /// <param name="width"></param>
  70. /// <param name="height"></param>
  71. /// <param name="stride"></param>
  72. /// <param name="channels"></param>
  73. public static byte[,] ColorToGrayArray( byte[] data, int width, int height, int stride, int channels = 3)
  74. {
  75. byte[,] grayArray = new byte[height, width];
  76. for ( int bz = 0, y = 0; y < height; y++)
  77. {
  78. for ( int zz = bz, x = 0; x < width; x++)
  79. {
  80. byte g = GreyByte(data[zz + 2], data[zz + 1], data[zz + 0]);
  81. grayArray[y, x] = g;
  82. zz += channels;
  83. }
  84. bz += stride;
  85. }
  86. return grayArray;
  87. }
  88. /// <summary>
  89. /// 最小与最大灰度
  90. /// </summary>
  91. /// <param name="data"></param>
  92. /// <param name="min"></param>
  93. /// <param name="max"></param>
  94. public static void Minium_Maxium(byte[,] data, out int min, out int max)
  95. {
  96. int height = data.GetLength( 0);
  97. int width = data.GetLength( 1);
  98. max = 0;
  99. min = 255;
  100. for ( int x = 0; x < width; x++)
  101. {
  102. for ( int y = 0; y < height; y++)
  103. {
  104. byte bt = data[y, x];
  105. if (bt < min)
  106. {
  107. min = bt;
  108. }
  109. if (bt > max)
  110. {
  111. max = bt;
  112. }
  113. }
  114. }
  115. }
  116. /// <summary>
  117. /// 灰度直方图统计
  118. /// </summary>
  119. /// <param name="data"></param>
  120. /// <returns></returns>
  121. public static int[] Gray_Histogram(byte[,] data)
  122. {
  123. int height = data.GetLength( 0);
  124. int width = data.GetLength( 1);
  125. int[] hv = new int[ 256];
  126. for ( int y = 0; y < height; y++)
  127. {
  128. for ( int x = 0; x < width; x++)
  129. {
  130. hv[data[y, x]]++;
  131. }
  132. }
  133. return hv;
  134. }
  135. /// <summary>
  136. /// 左端第一个非零
  137. /// </summary>
  138. /// <param name="histogram"></param>
  139. /// <returns></returns>
  140. public static int Histogram_Left(int[] histogram)
  141. {
  142. for ( int i = 0; i < histogram.Length; i++)
  143. {
  144. if (histogram[i] > 0)
  145. {
  146. return i;
  147. }
  148. }
  149. return 0;
  150. }
  151. /// <summary>
  152. /// 右端第一个非零
  153. /// </summary>
  154. /// <param name="histogram"></param>
  155. /// <returns></returns>
  156. public static int Histogram_Right(int[] histogram)
  157. {
  158. for ( int i = histogram.Length - 1; i >= 0; i--)
  159. {
  160. if (histogram[i] > 0)
  161. {
  162. return i;
  163. }
  164. }
  165. return 0;
  166. }
  167. /// <summary>
  168. /// 直方图和值
  169. /// </summary>
  170. /// <param name="histogram"></param>
  171. /// <returns></returns>
  172. public static int Histogram_Sum(int[] histogram)
  173. {
  174. return histogram.Sum();
  175. }
  176. /// <summary>
  177. /// 加权和值
  178. /// </summary>
  179. /// <param name="histogram"></param>
  180. /// <param name="right"></param>
  181. /// <param name="grade"></param>
  182. public static double Histogram_Sum(int[] histogram, int grade = 0, int right = 255)
  183. {
  184. double sum = 0.0;
  185. for ( int i = 0; i < histogram.Length && i <= right; i++)
  186. {
  187. double v = ( double)histogram[i];
  188. if (grade >= 1) v *= ( double)i;
  189. if (grade >= 2) v *= ( double)i;
  190. if (grade >= 3) v *= ( double)i;
  191. sum += v;
  192. }
  193. return sum;
  194. }
  195. /// <summary>
  196. /// 直方图数据转为浮点数数组
  197. /// </summary>
  198. /// <param name="histogram"></param>
  199. /// <returns></returns>
  200. public static double[] Histogram_To_Float(int[] histogram)
  201. {
  202. double[] hv = new double[histogram.Length];
  203. for ( int i = 0; i < histogram.Length; i++)
  204. {
  205. hv[i] = histogram[i];
  206. }
  207. return hv;
  208. }
  209. /// <summary>
  210. /// 直方图归一化
  211. /// </summary>
  212. /// <param name="histogram"></param>
  213. public static double[] Histogram_Normalize(int[] histogram)
  214. {
  215. double sum = 1.0 / histogram.Sum();
  216. double[] hn = new double[ 256];
  217. for ( int i = 0; i < histogram.Length; i++)
  218. {
  219. hn[i] = ( double)histogram[i] * sum;
  220. }
  221. return hn;
  222. }
  223. /// <summary>
  224. /// 灰度图的直方图均衡化处理
  225. /// </summary>
  226. /// <param name="data"></param>
  227. public static void Gray_Equalization(byte[,] data)
  228. {
  229. int height = data.GetLength( 0);
  230. int width = data.GetLength( 1);
  231. int[] histogram = Gray_Histogram(data);
  232. double[] density = Histogram_Normalize(histogram);
  233. for ( int y = 0; y < height; y++)
  234. {
  235. for ( int x = 0; x < width; x++)
  236. {
  237. byte c = data[y, x];
  238. double dsum = 0.0;
  239. for ( int k = 0; k <= c; k++)
  240. {
  241. dsum += density[k];
  242. }
  243. data[y, x] = ( byte)Math.Round( 255.0 * dsum);
  244. }
  245. }
  246. }
  247. #endregion
  248. }
  249. }

 这些函数将被后续发布的二十三种二值化算法所使用。

The thresholding algorithms can be categorized into different classes: 

Histogram shape-based methods 
. Clustering-based methods 
. Entropy-based methods
. Object attribute-based methods
. Spatial methods and local methods are based on the local characteristics of each pixel.
 

The binarization process is a conversion of a color or grayscale image into a two-color black and white image. The main parameter of this conversion is the threshold t, with the value of which the brightness of all is then compared.

Binarization is important in digital image processing, mainly in computer vision applications. Thresholding is an efficient technique in binarization. The choice of thresholding technique is crucial in binarization. There are various thresholding algorithms have been proposed to define the optimal threshold value.
 


转载:https://blog.csdn.net/beijinghorn/article/details/128425984
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场