首页 > 编程 > C# > 正文

C#验证码识别类完整实例

2019-10-29 21:40:15
字体:
来源:转载
供稿:网友

这篇文章主要介绍了C#验证码识别类,以一个完整实例形式较为详细的分析了验证码图片处理所涉及的各种常用技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了C#验证码识别类。分享给大家供大家参考。具体实现方法如下:

 

 
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Text; 
  5. using System.Drawing; 
  6. using System.Drawing.Imaging; 
  7. using System.Runtime.InteropServices; 
  8. namespace 验证码处理 
  9. class VerifyCode 
  10. public Bitmap bmpobj; 
  11. public VerifyCode(Bitmap pic) 
  12. bmpobj = new Bitmap(pic); //转换为Format32bppRgb 
  13. /// <summary> 
  14. /// 根据RGB,计算灰度值 
  15. /// </summary> 
  16. /// <param name="posClr">Color值</param> 
  17. /// <returns>灰度值,整型</returns> 
  18. private int GetGrayNumColor(System.Drawing.Color posClr) 
  19. return (posClr.R * 19595 + posClr.G * 38469 + posClr.B * 7472) >> 16; 
  20. /// <summary> 
  21. /// 灰度转换,逐点方式 
  22. /// </summary> 
  23. public void GrayByPixels() 
  24. for (int i = 0; i < bmpobj.Height; i++) 
  25. for (int j = 0; j < bmpobj.Width; j++) 
  26. int tmpValue = GetGrayNumColor(bmpobj.GetPixel(j, i)); 
  27. bmpobj.SetPixel(j, i, Color.FromArgb(tmpValue, tmpValue, tmpValue)); 
  28. /// <summary> 
  29. /// 去图形边框 
  30. /// </summary> 
  31. /// <param name="borderWidth"></param> 
  32. public void ClearPicBorder(int borderWidth) 
  33. for (int i = 0; i < bmpobj.Height; i++) 
  34. for (int j = 0; j < bmpobj.Width; j++) 
  35. if (i < borderWidth || j < borderWidth || j > bmpobj.Width - 1 - borderWidth || i > bmpobj.Height - 1 - borderWidth) 
  36. bmpobj.SetPixel(j, i, Color.FromArgb(255, 255, 255)); 
  37. /// <summary> 
  38. /// 灰度转换,逐行方式 
  39. /// </summary> 
  40. public void GrayByLine() 
  41. Rectangle rec = new Rectangle(0, 0, bmpobj.Width, bmpobj.Height); 
  42. BitmapData bmpData = bmpobj.LockBits(rec, ImageLockMode.ReadWrite, bmpobj.PixelFormat);// PixelFormat.Format32bppPArgb); 
  43. // bmpData.PixelFormat = PixelFormat.Format24bppRgb; 
  44. IntPtr scan0 = bmpData.Scan0; 
  45. int len = bmpobj.Width * bmpobj.Height; 
  46. int[] pixels = new int[len]; 
  47. Marshal.Copy(scan0, pixels, 0, len); 
  48. //对图片进行处理 
  49. int GrayValue = 0; 
  50. for (int i = 0; i < len; i++) 
  51. GrayValue = GetGrayNumColor(Color.FromArgb(pixels[i])); 
  52. pixels[i] = (byte)(Color.FromArgb(GrayValue, GrayValue, GrayValue)).ToArgb(); //Color转byte 
  53. bmpobj.UnlockBits(bmpData); 
  54. ////输出 
  55. //GCHandle gch = GCHandle.Alloc(pixels, GCHandleType.Pinned); 
  56. //bmpOutput = new Bitmap(bmpobj.Width, bmpobj.Height, bmpData.Stride, bmpData.PixelFormat, gch.AddrOfPinnedObject()); 
  57. //gch.Free(); 
  58. /// <summary> 
  59. /// 得到有效图形并调整为可平均分割的大小 
  60. /// </summary> 
  61. /// <param name="dgGrayValue">灰度背景分界值</param> 
  62. /// <param name="CharsCount">有效字符数</param> 
  63. /// <returns></returns> 
  64. public void GetPicValidByValue(int dgGrayValue, int CharsCount) 
  65. int posx1 = bmpobj.Width; int posy1 = bmpobj.Height; 
  66. int posx2 = 0; int posy2 = 0; 
  67. for (int i = 0; i < bmpobj.Height; i++) //找有效区 
  68. for (int j = 0; j < bmpobj.Width; j++) 
  69. int pixelValue = bmpobj.GetPixel(j, i).R; 
  70. if (pixelValue < dgGrayValue) //根据灰度值 
  71. if (posx1 > j) posx1 = j; 
  72. if (posy1 > i) posy1 = i; 
  73.  
  74. if (posx2 < j) posx2 = j; 
  75. if (posy2 < i) posy2 = i; 
  76. }; 
  77. }; 
  78. }; 
  79. // 确保能整除 
  80. int Span = CharsCount - (posx2 - posx1 + 1) % CharsCount; //可整除的差额数 
  81. if (Span < CharsCount) 
  82. int leftSpan = Span / 2; //分配到左边的空列 ,如span为单数,则右边比左边大1 
  83. if (posx1 > leftSpan) 
  84. posx1 = posx1 - leftSpan; 
  85. if (posx2 + Span - leftSpan < bmpobj.Width) 
  86. posx2 = posx2 + Span - leftSpan; 
  87. //复制新图 
  88. Rectangle cloneRect = new Rectangle(posx1, posy1, posx2 - posx1 + 1, posy2 - posy1 + 1); 
  89. bmpobj = bmpobj.Clone(cloneRect, bmpobj.PixelFormat); 
  90. /// <summary> 
  91. /// 得到有效图形,图形为类变量 
  92. /// </summary> 
  93. /// <param name="dgGrayValue">灰度背景分界值</param> 
  94. /// <param name="CharsCount">有效字符数</param> 
  95. /// <returns></returns> 
  96. public void GetPicValidByValue(int dgGrayValue) 
  97. int posx1 = bmpobj.Width; int posy1 = bmpobj.Height; 
  98. int posx2 = 0; int posy2 = 0; 
  99. for (int i = 0; i < bmpobj.Height; i++) //找有效区 
  100. for (int j = 0; j < bmpobj.Width; j++) 
  101. int pixelValue = bmpobj.GetPixel(j, i).R; 
  102. if (pixelValue < dgGrayValue) //根据灰度值 
  103. if (posx1 > j) posx1 = j; 
  104. if (posy1 > i) posy1 = i; 
  105.  
  106. if (posx2 < j) posx2 = j; 
  107. if (posy2 < i) posy2 = i; 
  108. }; 
  109. }; 
  110. }; 
  111. //复制新图 
  112. Rectangle cloneRect = new Rectangle(posx1, posy1, posx2 - posx1 + 1, posy2 - posy1 + 1); 
  113. bmpobj = bmpobj.Clone(cloneRect, bmpobj.PixelFormat); 
  114. /// <summary> 
  115. /// 得到有效图形,图形由外面传入 
  116. /// </summary> 
  117. /// <param name="dgGrayValue">灰度背景分界值</param> 
  118. /// <param name="CharsCount">有效字符数</param> 
  119. /// <returns></returns> 
  120. public Bitmap GetPicValidByValue(Bitmap singlepic, int dgGrayValue) 
  121. int posx1 = singlepic.Width; int posy1 = singlepic.Height; 
  122. int posx2 = 0; int posy2 = 0; 
  123. for (int i = 0; i < singlepic.Height; i++) //找有效区 
  124. for (int j = 0; j < singlepic.Width; j++) 
  125. int pixelValue = singlepic.GetPixel(j, i).R; 
  126. if (pixelValue < dgGrayValue) //根据灰度值 
  127. if (posx1 > j) posx1 = j; 
  128. if (posy1 > i) posy1 = i; 
  129.  
  130. if (posx2 < j) posx2 = j; 
  131. if (posy2 < i) posy2 = i; 
  132. }; 
  133. }; 
  134. }; 
  135. //复制新图 
  136. Rectangle cloneRect = new Rectangle(posx1, posy1, posx2 - posx1 + 1, posy2 - posy1 + 1); 
  137. return singlepic.Clone(cloneRect, singlepic.PixelFormat); 
  138. /// <summary> 
  139. /// 平均分割图片 
  140. /// </summary> 
  141. /// <param name="RowNum">水平上分割数</param> 
  142. /// <param name="ColNum">垂直上分割数</param> 
  143. /// <returns>分割好的图片数组</returns> 
  144. public Bitmap [] GetSplitPics(int RowNum,int ColNum) 
  145. if (RowNum == 0 || ColNum == 0) 
  146. return null
  147. int singW = bmpobj.Width / RowNum; 
  148. int singH = bmpobj.Height / ColNum; 
  149. Bitmap [] PicArray=new Bitmap[RowNum*ColNum]; 
  150. Rectangle cloneRect; 
  151. for (int i = 0; i < ColNum; i++) //找有效区 
  152. for (int j = 0; j < RowNum; j++) 
  153. cloneRect = new Rectangle(j*singW, i*singH, singW , singH); 
  154. PicArray[i*RowNum+j]=bmpobj.Clone(cloneRect, bmpobj.PixelFormat);//复制小块图 
  155. return PicArray; 
  156. /// <summary> 
  157. /// 返回灰度图片的点阵描述字串,1表示灰点,0表示背景 
  158. /// </summary> 
  159. /// <param name="singlepic">灰度图</param> 
  160. /// <param name="dgGrayValue">背前景灰色界限</param> 
  161. /// <returns></returns> 
  162. public string GetSingleBmpCode(Bitmap singlepic, int dgGrayValue) 
  163. Color piexl; 
  164. string code = ""
  165. for (int posy = 0; posy < singlepic.Height; posy++) 
  166. for (int posx = 0; posx < singlepic.Width; posx++) 
  167. piexl = singlepic.GetPixel(posx, posy); 
  168. if (piexl.R < dgGrayValue) // Color.Black ) 
  169. code = code + "1"
  170. else 
  171. code = code + "0"
  172. return code; 
  173. /// <summary> 
  174. /// 得到灰度图像前景背景的临界值 最大类间方差法 
  175. /// </summary> 
  176. /// <returns>前景背景的临界值</returns> 
  177. public int GetDgGrayValue() 
  178. int[] pixelNum = new int[256]; //图象直方图,共256个点 
  179. int n, n1, n2; 
  180. int total; //total为总和,累计值 
  181. double m1, m2, sum, csum, fmax, sb; //sb为类间方差,fmax存储最大方差值 
  182. int k, t, q; 
  183. int threshValue = 1; // 阈值 
  184. //生成直方图 
  185. for (int i = 0; i < bmpobj.Width; i++) 
  186. for (int j = 0; j < bmpobj.Height; j++) 
  187. //返回各个点的颜色,以RGB表示 
  188. pixelNum[bmpobj.GetPixel(i, j).R]++; //相应的直方图加1 
  189. //直方图平滑化 
  190. for (k = 0; k <= 255; k++) 
  191. total = 0; 
  192. for (t = -2; t <= 2; t++) //与附近2个灰度做平滑化,t值应取较小的值 
  193. q = k + t; 
  194. if (q < 0) //越界处理 
  195. q = 0; 
  196. if (q > 255) 
  197. q = 255; 
  198. total = total + pixelNum[q]; //total为总和,累计值 
  199. pixelNum[k] = (int)((float)total / 5.0 + 0.5); //平滑化,左边2个+中间1个+右边2个灰度,共5个,所以总和除以5,后面加0.5是用修正值 
  200. //求阈值 
  201. sum = csum = 0.0; 
  202. n = 0; 
  203. //计算总的图象的点数和质量矩,为后面的计算做准备 
  204. for (k = 0; k <= 255; k++) 
  205. sum += (double)k * (double)pixelNum[k]; //x*f(x)质量矩,也就是每个灰度的值乘以其点数(归一化后为概率),sum为其总和 
  206. n += pixelNum[k]; //n为图象总的点数,归一化后就是累积概率 
  207. fmax = -1.0; //类间方差sb不可能为负,所以fmax初始值为-1不影响计算的进行 
  208. n1 = 0; 
  209. for (k = 0; k < 256; k++) //对每个灰度(从0到255)计算一次分割后的类间方差sb 
  210. n1 += pixelNum[k]; //n1为在当前阈值遍前景图象的点数 
  211. if (n1 == 0) { continue; } //没有分出前景后景 
  212. n2 = n - n1; //n2为背景图象的点数 
  213. if (n2 == 0) { break; } //n2为0表示全部都是后景图象,与n1=0情况类似,之后的遍历不可能使前景点数增加,所以此时可以退出循环 
  214. csum += (double)k * pixelNum[k]; //前景的“灰度的值*其点数”的总和 
  215. m1 = csum / n1; //m1为前景的平均灰度 
  216. m2 = (sum - csum) / n2; //m2为背景的平均灰度 
  217. sb = (double)n1 * (double)n2 * (m1 - m2) * (m1 - m2); //sb为类间方差 
  218. if (sb > fmax) //如果算出的类间方差大于前一次算出的类间方差 
  219. fmax = sb; //fmax始终为最大类间方差(otsu) 
  220. threshValue = k; //取最大类间方差时对应的灰度的k就是最佳阈值 
  221. return threshValue; 
  222. /// <summary> 
  223. /// 去掉杂点(适合杂点/杂线粗为1) 
  224. /// </summary> 
  225. /// <param name="dgGrayValue">背前景灰色界限</param> 
  226. /// <returns></returns> 
  227. public void ClearNoise(int dgGrayValue, int MaxNearPoints) 
  228. Color piexl; 
  229. int nearDots = 0; 
  230. //逐点判断 
  231. for (int i = 0; i < bmpobj.Width; i++) 
  232. for (int j = 0; j < bmpobj.Height; j++) 
  233. piexl = bmpobj.GetPixel(i, j); 
  234. if (piexl.R < dgGrayValue) 
  235. nearDots = 0; 
  236. //判断周围8个点是否全为空 
  237. if (i == 0 || i == bmpobj.Width - 1 || j == 0 || j == bmpobj.Height - 1) //边框全去掉 
  238. bmpobj.SetPixel(i, j, Color.FromArgb(255, 255, 255)); 
  239. else 
  240. if (bmpobj.GetPixel(i - 1, j - 1).R < dgGrayValue) nearDots++; 
  241. if (bmpobj.GetPixel(i, j - 1).R < dgGrayValue) nearDots++; 
  242. if (bmpobj.GetPixel(i + 1, j - 1).R < dgGrayValue) nearDots++; 
  243. if (bmpobj.GetPixel(i - 1, j).R < dgGrayValue) nearDots++; 
  244. if (bmpobj.GetPixel(i + 1, j).R < dgGrayValue) nearDots++; 
  245. if (bmpobj.GetPixel(i - 1, j + 1).R < dgGrayValue) nearDots++; 
  246. if (bmpobj.GetPixel(i, j + 1).R < dgGrayValue) nearDots++; 
  247. if (bmpobj.GetPixel(i + 1, j + 1).R < dgGrayValue) nearDots++; 
  248. if (nearDots < MaxNearPoints) 
  249. bmpobj.SetPixel(i, j, Color.FromArgb(255, 255, 255)); //去掉单点 && 粗细小3邻边点 
  250. else //背景 
  251. bmpobj.SetPixel(i, j, Color.FromArgb(255, 255, 255)); 
  252. /// <summary> 
  253. /// 3×3中值滤波除杂 
  254. /// </summary> 
  255. /// <param name="dgGrayValue"></param> 
  256. public void ClearNoise(int dgGrayValue) 
  257. int x, y; 
  258. byte[] p = new byte[9]; //最小处理窗口3*3 
  259. byte s; 
  260. //byte[] lpTemp=new BYTE[nByteWidth*nHeight]; 
  261. int i, j; 
  262. //--!!!!!!!!!!!!!!下面开始窗口为3×3中值滤波!!!!!!!!!!!!!!!! 
  263. for (y = 1; y < bmpobj.Height - 1; y++) //--第一行和最后一行无法取窗口 
  264. for (x = 1; x < bmpobj.Width - 1; x++) 
  265. //取9个点的值 
  266. p[0] = bmpobj.GetPixel(x - 1, y - 1).R; 
  267. p[1] = bmpobj.GetPixel(x, y - 1).R; 
  268. p[2] = bmpobj.GetPixel(x + 1, y - 1).R; 
  269. p[3] = bmpobj.GetPixel(x - 1, y).R; 
  270. p[4] = bmpobj.GetPixel(x, y).R; 
  271. p[5] = bmpobj.GetPixel(x + 1, y).R; 
  272. p[6] = bmpobj.GetPixel(x - 1, y + 1).R; 
  273. p[7] = bmpobj.GetPixel(x, y + 1).R; 
  274. p[8] = bmpobj.GetPixel(x + 1, y + 1).R; 
  275. //计算中值 
  276. for (j = 0; j < 5; j++) 
  277. for (i = j + 1; i < 9; i++) 
  278. if (p[j] > p[i]) 
  279. s = p[j]; 
  280. p[j] = p[i]; 
  281. p[i] = s; 
  282. // if (bmpobj.GetPixel(x, y).R < dgGrayValue) 
  283. bmpobj.SetPixel(x, y, Color.FromArgb(p[4], p[4], p[4])); //给有效值付中值 
  284. /// <summary> 
  285. /// 该函数用于对图像进行腐蚀运算。结构元素为水平方向或垂直方向的三个点, 
  286. /// 中间点位于原点;或者由用户自己定义3×3的结构元素。 
  287. /// </summary> 
  288. /// <param name="dgGrayValue">前后景临界值</param> 
  289. /// <param name="nMode">腐蚀方式:0表示水平方向,1垂直方向,2自定义结构元素。</param> 
  290. /// <param name="structure"> 自定义的3×3结构元素</param> 
  291. public void ErosionPic(int dgGrayValue, int nMode, bool[,] structure) 
  292. int lWidth = bmpobj.Width; 
  293. int lHeight = bmpobj.Height; 
  294. Bitmap newBmp = new Bitmap(lWidth, lHeight); 
  295. int i, j, n, m; //循环变量 
  296. if (nMode == 0) 
  297. //使用水平方向的结构元素进行腐蚀 
  298. // 由于使用1×3的结构元素,为防止越界,所以不处理最左边和最右边 
  299. // 的两列像素 
  300. for (j = 0; j < lHeight; j++) 
  301. for (i = 1; i < lWidth - 1; i++) 
  302. //目标图像中的当前点先赋成黑色 
  303. newBmp.SetPixel(i, j, Color.Black); 
  304.  
  305. //如果源图像中当前点自身或者左右有一个点不是黑色, 
  306. //则将目标图像中的当前点赋成白色 
  307. if (bmpobj.GetPixel(i - 1, j).R > dgGrayValue || 
  308. bmpobj.GetPixel(i, j).R > dgGrayValue || 
  309. bmpobj.GetPixel(i + 1, j).R > dgGrayValue) 
  310. newBmp.SetPixel(i, j, Color.White); 
  311. else if (nMode == 1) 
  312. //使用垂真方向的结构元素进行腐蚀 
  313. // 由于使用3×1的结构元素,为防止越界,所以不处理最上边和最下边 
  314. // 的两行像素 
  315. for (j = 1; j < lHeight - 1; j++) 
  316. for (i = 0; i < lWidth; i++) 
  317. //目标图像中的当前点先赋成黑色 
  318. newBmp.SetPixel(i, j, Color.Black); 
  319. //如果源图像中当前点自身或者左右有一个点不是黑色, 
  320. //则将目标图像中的当前点赋成白色 
  321. if (bmpobj.GetPixel(i, j - 1).R > dgGrayValue || 
  322. bmpobj.GetPixel(i, j).R > dgGrayValue || 
  323. bmpobj.GetPixel(i, j + 1).R > dgGrayValue) 
  324. newBmp.SetPixel(i, j, Color.White); 
  325. else 
  326. if (structure.Length != 9) //检查自定义结构 
  327. return
  328. //使用自定义的结构元素进行腐蚀 
  329. // 由于使用3×3的结构元素,为防止越界,所以不处理最左边和最右边 
  330. // 的两列像素和最上边和最下边的两列像素 
  331. for (j = 1; j < lHeight - 1; j++) 
  332. for (i = 1; i < lWidth - 1; i++) 
  333. //目标图像中的当前点先赋成黑色 
  334. newBmp.SetPixel(i, j, Color.Black); 
  335. //如果原图像中对应结构元素中为黑色的那些点中有一个不是黑色, 
  336. //则将目标图像中的当前点赋成白色 
  337. for (m = 0; m < 3; m++) 
  338. for (n = 0; n < 3; n++) 
  339. if (!structure[m, n]) 
  340. continue
  341. if (bmpobj.GetPixel(i + m - 1, j + n - 1).R > dgGrayValue) 
  342. newBmp.SetPixel(i, j, Color.White); 
  343. break
  344. bmpobj = newBmp; 
  345. /// <summary> 
  346. /// 该函数用于对图像进行细化运算。要求目标图像为灰度图像 
  347. /// </summary> 
  348. /// <param name="dgGrayValue"></param> 
  349. public void ThiningPic(int dgGrayValue) 
  350. int lWidth = bmpobj.Width; 
  351. int lHeight = bmpobj.Height; 
  352. // Bitmap newBmp = new Bitmap(lWidth, lHeight); 
  353. bool bModified; //脏标记  
  354. int i, j, n, m; //循环变量 
  355. //四个条件 
  356. bool bCondition1; 
  357. bool bCondition2; 
  358. bool bCondition3; 
  359. bool bCondition4; 
  360. int nCount; //计数器  
  361. int[,] neighbour = new int[5, 5]; //5×5相邻区域像素值 
  362. bModified = true
  363. while (bModified) 
  364. bModified = false
  365. //由于使用5×5的结构元素,为防止越界,所以不处理外围的几行和几列像素 
  366. for (j = 2; j < lHeight - 2; j++) 
  367. for (i = 2; i < lWidth - 2; i++) 
  368. bCondition1 = false
  369. bCondition2 = false
  370. bCondition3 = false
  371. bCondition4 = false
  372. if (bmpobj.GetPixel(i, j).R > dgGrayValue) 
  373. if (bmpobj.GetPixel(i, j).R < 255) 
  374. bmpobj.SetPixel(i, j, Color.White); 
  375. continue
  376. //获得当前点相邻的5×5区域内像素值,白色用0代表,黑色用1代表 
  377. for (m = 0; m < 5; m++) 
  378. for (n = 0; n < 5; n++) 
  379. neighbour[m, n] = bmpobj.GetPixel(i + m - 2, j + n - 2).R < dgGrayValue ? 1 : 0; 
  380. //逐个判断条件。 
  381. //判断2<=NZ(P1)<=6 
  382. nCount = neighbour[1, 1] + neighbour[1, 2] + neighbour[1, 3] 
  383. + neighbour[2, 1] + neighbour[2, 3] + 
  384. +neighbour[3, 1] + neighbour[3, 2] + neighbour[3, 3]; 
  385. if (nCount >= 2 && nCount <= 6) 
  386. bCondition1 = true
  387. //判断Z0(P1)=1 
  388. nCount = 0; 
  389. if (neighbour[1, 2] == 0 && neighbour[1, 1] == 1) 
  390. nCount++; 
  391. if (neighbour[1, 1] == 0 && neighbour[2, 1] == 1) 
  392. nCount++; 
  393. if (neighbour[2, 1] == 0 && neighbour[3, 1] == 1) 
  394. nCount++; 
  395. if (neighbour[3, 1] == 0 && neighbour[3, 2] == 1) 
  396. nCount++; 
  397. if (neighbour[3, 2] == 0 && neighbour[3, 3] == 1) 
  398. nCount++; 
  399. if (neighbour[3, 3] == 0 && neighbour[2, 3] == 1) 
  400. nCount++; 
  401. if (neighbour[2, 3] == 0 && neighbour[1, 3] == 1) 
  402. nCount++; 
  403. if (neighbour[1, 3] == 0 && neighbour[1, 2] == 1) 
  404. nCount++; 
  405. if (nCount == 1) 
  406. bCondition2 = true
  407. //判断P2*P4*P8=0 or Z0(p2)!=1 
  408. if (neighbour[1, 2] * neighbour[2, 1] * neighbour[2, 3] == 0) 
  409. bCondition3 = true
  410. else 
  411. nCount = 0; 
  412. if (neighbour[0, 2] == 0 && neighbour[0, 1] == 1) 
  413. nCount++; 
  414. if (neighbour[0, 1] == 0 && neighbour[1, 1] == 1) 
  415. nCount++; 
  416. if (neighbour[1, 1] == 0 && neighbour[2, 1] == 1) 
  417. nCount++; 
  418. if (neighbour[2, 1] == 0 && neighbour[2, 2] == 1) 
  419. nCount++; 
  420. if (neighbour[2, 2] == 0 && neighbour[2, 3] == 1) 
  421. nCount++; 
  422. if (neighbour[2, 3] == 0 && neighbour[1, 3] == 1) 
  423. nCount++; 
  424. if (neighbour[1, 3] == 0 && neighbour[0, 3] == 1) 
  425. nCount++; 
  426. if (neighbour[0, 3] == 0 && neighbour[0, 2] == 1) 
  427. nCount++; 
  428. if (nCount != 1) 
  429. bCondition3 = true
  430. //判断P2*P4*P6=0 or Z0(p4)!=1 
  431. if (neighbour[1, 2] * neighbour[2, 1] * neighbour[3, 2] == 0) 
  432. bCondition4 = true
  433. else 
  434. nCount = 0; 
  435. if (neighbour[1, 1] == 0 && neighbour[1, 0] == 1) 
  436. nCount++; 
  437. if (neighbour[1, 0] == 0 && neighbour[2, 0] == 1) 
  438. nCount++; 
  439. if (neighbour[2, 0] == 0 && neighbour[3, 0] == 1) 
  440. nCount++; 
  441. if (neighbour[3, 0] == 0 && neighbour[3, 1] == 1) 
  442. nCount++; 
  443. if (neighbour[3, 1] == 0 && neighbour[3, 2] == 1) 
  444. nCount++; 
  445. if (neighbour[3, 2] == 0 && neighbour[2, 2] == 1) 
  446. nCount++; 
  447. if (neighbour[2, 2] == 0 && neighbour[1, 2] == 1) 
  448. nCount++; 
  449. if (neighbour[1, 2] == 0 && neighbour[1, 1] == 1) 
  450. nCount++; 
  451. if (nCount != 1) 
  452. bCondition4 = true
  453. if (bCondition1 && bCondition2 && bCondition3 && bCondition4) 
  454. bmpobj.SetPixel(i, j, Color.White); 
  455. bModified = true
  456. else 
  457. bmpobj.SetPixel(i, j, Color.Black); 
  458. // 复制细化后的图像 
  459. // bmpobj = newBmp; 
  460. /// <summary> 
  461. /// 锐化要启用不安全代码编译 
  462. /// </summary> 
  463. /// <param name="val">锐化程度。取值[0,1]。值越大锐化程度越高</param> 
  464. /// <returns>锐化后的图像</returns> 
  465. public void Sharpen(float val) 
  466. int w = bmpobj.Width; 
  467. int h = bmpobj.Height; 
  468. Bitmap bmpRtn = new Bitmap(w, h, PixelFormat.Format24bppRgb); 
  469. BitmapData srcData = bmpobj.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); 
  470. BitmapData dstData = bmpRtn.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb); 
  471. unsafe 
  472. byte* pIn = (byte*)srcData.Scan0.ToPointer(); 
  473. byte* pOut = (byte*)dstData.Scan0.ToPointer(); 
  474. int stride = srcData.Stride; 
  475. byte* p; 
  476. for (int y = 0; y < h; y++) 
  477. for (int x = 0; x < w; x++) 
  478. //取周围9点的值。位于边缘上的点不做改变。 
  479. if (x == 0 || x == w - 1 || y == 0 || y == h - 1) 
  480. //不做 
  481. pOut[0] = pIn[0]; 
  482. pOut[1] = pIn[1]; 
  483. pOut[2] = pIn[2]; 
  484. else 
  485. int r1, r2, r3, r4, r5, r6, r7, r8, r0; 
  486. int g1, g2, g3, g4, g5, g6, g7, g8, g0; 
  487. int b1, b2, b3, b4, b5, b6, b7, b8, b0; 
  488. float vR, vG, vB; 
  489. //左上 
  490. p = pIn - stride - 3; 
  491. r1 = p[2]; 
  492. g1 = p[1]; 
  493. b1 = p[0]; 
  494. //正上 
  495. p = pIn - stride; 
  496. r2 = p[2]; 
  497. g2 = p[1]; 
  498. b2 = p[0]; 
  499. //右上 
  500. p = pIn - stride + 3; 
  501. r3 = p[2]; 
  502. g3 = p[1]; 
  503. b3 = p[0]; 
  504. //左侧 
  505. p = pIn - 3; 
  506. r4 = p[2]; 
  507. g4 = p[1]; 
  508. b4 = p[0]; 
  509. //右侧 
  510. p = pIn + 3; 
  511. r5 = p[2]; 
  512. g5 = p[1]; 
  513. b5 = p[0]; 
  514. //右下 
  515. p = pIn + stride - 3; 
  516. r6 = p[2]; 
  517. g6 = p[1]; 
  518. b6 = p[0]; 
  519. //正下 
  520. p = pIn + stride; 
  521. r7 = p[2]; 
  522. g7 = p[1]; 
  523. b7 = p[0]; 
  524. //右下 
  525. p = pIn + stride + 3; 
  526. r8 = p[2]; 
  527. g8 = p[1]; 
  528. b8 = p[0]; 
  529. //自己 
  530. p = pIn; 
  531. r0 = p[2]; 
  532. g0 = p[1]; 
  533. b0 = p[0]; 
  534. vR = (float)r0 - (float)(r1 + r2 + r3 + r4 + r5 + r6 + r7 + r8) / 8; 
  535. vG = (float)g0 - (float)(g1 + g2 + g3 + g4 + g5 + g6 + g7 + g8) / 8; 
  536. vB = (float)b0 - (float)(b1 + b2 + b3 + b4 + b5 + b6 + b7 + b8) / 8; 
  537. vR = r0 + vR * val; 
  538. vG = g0 + vG * val; 
  539. vB = b0 + vB * val; 
  540. if (vR > 0) 
  541. vR = Math.Min(255, vR); 
  542. else 
  543. vR = Math.Max(0, vR); 
  544. if (vG > 0) 
  545. vG = Math.Min(255, vG); 
  546. else 
  547. vG = Math.Max(0, vG); 
  548. if (vB > 0) 
  549. vB = Math.Min(255, vB); 
  550. else 
  551. vB = Math.Max(0, vB); 
  552. pOut[0] = (byte)vB; 
  553. pOut[1] = (byte)vG; 
  554. pOut[2] = (byte)vR; 
  555. pIn += 3; 
  556. pOut += 3; 
  557. }// end of x 
  558. pIn += srcData.Stride - w * 3; 
  559. pOut += srcData.Stride - w * 3; 
  560. // end of y 
  561. bmpobj.UnlockBits(srcData); 
  562. bmpRtn.UnlockBits(dstData); 
  563. bmpobj = bmpRtn; 
  564. /// <summary> 
  565. /// 图片二值化 
  566. /// </summary> 
  567. /// <param name="hsb"></param> 
  568. public void BitmapTo1Bpp(Double hsb) 
  569. int w = bmpobj.Width; 
  570. int h = bmpobj.Height; 
  571. Bitmap bmp = new Bitmap(w, h, PixelFormat.Format1bppIndexed); 
  572. BitmapData data = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed); 
  573. for (int y = 0; y < h; y++) 
  574. byte[] scan = new byte[(w + 7) / 8]; 
  575. for (int x = 0; x < w; x++) 
  576. Color c = bmpobj.GetPixel(x, y); 
  577. if (c.GetBrightness() >= hsb) scan[x / 8] |= (byte)(0x80 >> (x % 8)); 
  578. Marshal.Copy(scan, 0, (IntPtr)((int)data.Scan0 + data.Stride * y), scan.Length); 
  579. bmp.UnlockBits(data); 
  580. bmpobj = bmp; 

希望本文所述对大家的C#程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表