当前位置:   article > 正文

17.3.2.7 二值化(内存处理)

17.3.2.7 二值化(内存处理)

版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。

二值化的算法请参看第17.3.1.5节。17.3.1.5 二值化(黑白)-CSDN博客

【例 17.48二值化算法一。

  1. //黑白1
  2. private void btn2Color1_Click(object sender, EventArgs e)
  3. {
  4. Bitmap destImg = new Bitmap(sourceImg.Width, sourceImg.Height);
  5. BitmapData sourceData = sourceImg.LockBits(new Rectangle(0, 0, sourceImg.Width, sourceImg.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
  6. BitmapData destData = destImg.LockBits(new Rectangle(0, 0, sourceImg.Width, sourceImg.Height), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
  7. IntPtr pSource = sourceData.Scan0;
  8. int allBytes = sourceData.Stride * sourceData.Height;
  9. byte[] rgbvalues = new byte[allBytes];
  10. Marshal.Copy(pSource, rgbvalues, 0, allBytes);
  11. int pos = 0;
  12. int R, G, B;
  13. byte avgValue;
  14. for (int j = 0; j < sourceData.Height; j++)
  15. {
  16. for (int i = 0; i < sourceData.Width; i++)
  17. {
  18. B = rgbvalues[pos];
  19. G = rgbvalues[pos + 1];
  20. R = rgbvalues[pos + 2];
  21. avgValue = (byte)((B + G + R) / 3);
  22. if (avgValue >= 128)
  23. avgValue = 255;
  24. else
  25. avgValue = 0;
  26. rgbvalues[pos] = avgValue;
  27. rgbvalues[pos + 1] = avgValue;
  28. rgbvalues[pos + 2] = avgValue;
  29. pos = pos + 3;
  30. }
  31. pos = pos + sourceData.Stride - sourceData.Width * 3;
  32. }
  33. IntPtr pDest = destData.Scan0;
  34. Marshal.Copy(rgbvalues, 0, pDest, allBytes);
  35. sourceImg.UnlockBits(sourceData);
  36. destImg.UnlockBits(destData);
  37. picDest.Image = destImg;
  38. }

【例 17.49二值化算法二

  1. //黑白2
  2. private void btn2Color2_Click(object sender, EventArgs e)
  3. {
  4. Bitmap destImg = new Bitmap(sourceImg.Width, sourceImg.Height);
  5. BitmapData sourceData = sourceImg.LockBits(new Rectangle(0, 0, sourceImg.Width, sourceImg.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
  6. BitmapData destData = destImg.LockBits(new Rectangle(0, 0, sourceImg.Width, sourceImg.Height), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
  7. IntPtr pSource = sourceData.Scan0;
  8. int allBytes = sourceData.Stride * sourceData.Height;
  9. byte[] rgbvalues = new byte[allBytes];
  10. Marshal.Copy(pSource, rgbvalues, 0, allBytes);
  11. int pos = 0;
  12. int R, G, B;
  13. int[] HistGram = new int[256];
  14. for (int j = 0; j < sourceData.Height; j++)
  15. {
  16. for (int i = 0; i < sourceData.Width; i++)
  17. {
  18. R = rgbvalues[pos + 2];
  19. HistGram[R] += 1;
  20. pos = pos + 3;
  21. }
  22. pos = pos + sourceData.Stride - sourceData.Width * 3;
  23. }
  24. int threshold;
  25. int allSum = 0;
  26. int allCount=0;
  27. for(int k= 0;k<= 255;k++)
  28. {
  29. allCount += HistGram[k];
  30. allSum += k * HistGram[k];
  31. }
  32. threshold = allSum / allCount;
  33. pos = 0;
  34. for (int j = 0; j < sourceData.Height; j++)
  35. {
  36. for (int i = 0; i < sourceData.Width; i++)
  37. {
  38. R = rgbvalues[pos + 2];
  39. if (R >= threshold)
  40. R = 255;
  41. else
  42. R = 0;
  43. rgbvalues[pos] = (byte)R;
  44. rgbvalues[pos + 1] = (byte)R;
  45. rgbvalues[pos + 2] = (byte)R;
  46. pos = pos + 3;
  47. }
  48. pos = pos + sourceData.Stride - sourceData.Width * 3;
  49. }
  50. IntPtr pDest= destData.Scan0;
  51. Marshal.Copy(rgbvalues, 0, pDest, allBytes);
  52. sourceImg.UnlockBits(sourceData);
  53. destImg.UnlockBits(destData);
  54. picDest.Image = destImg;
  55. }

学习更多vb.net知识,请参看vb.net 教程 目录

学习更多C#知识,请参看 C# 教程 目录

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/104230
推荐阅读
相关标签
  

闽ICP备14008679号