赞
踩
版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。
二值化的算法请参看第17.3.1.5节。17.3.1.5 二值化(黑白)-CSDN博客
【例 17.48】二值化算法一。
- //黑白1
- private void btn2Color1_Click(object sender, EventArgs e)
- {
- Bitmap destImg = new Bitmap(sourceImg.Width, sourceImg.Height);
- BitmapData sourceData = sourceImg.LockBits(new Rectangle(0, 0, sourceImg.Width, sourceImg.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
- BitmapData destData = destImg.LockBits(new Rectangle(0, 0, sourceImg.Width, sourceImg.Height), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
- IntPtr pSource = sourceData.Scan0;
- int allBytes = sourceData.Stride * sourceData.Height;
- byte[] rgbvalues = new byte[allBytes];
- Marshal.Copy(pSource, rgbvalues, 0, allBytes);
-
- int pos = 0;
- int R, G, B;
- byte avgValue;
-
- for (int j = 0; j < sourceData.Height; j++)
- {
- for (int i = 0; i < sourceData.Width; i++)
- {
- B = rgbvalues[pos];
- G = rgbvalues[pos + 1];
- R = rgbvalues[pos + 2];
- avgValue = (byte)((B + G + R) / 3);
- if (avgValue >= 128)
- avgValue = 255;
- else
- avgValue = 0;
- rgbvalues[pos] = avgValue;
- rgbvalues[pos + 1] = avgValue;
- rgbvalues[pos + 2] = avgValue;
- pos = pos + 3;
- }
- pos = pos + sourceData.Stride - sourceData.Width * 3;
- }
-
- IntPtr pDest = destData.Scan0;
- Marshal.Copy(rgbvalues, 0, pDest, allBytes);
- sourceImg.UnlockBits(sourceData);
- destImg.UnlockBits(destData);
- picDest.Image = destImg;
- }
【例 17.49】二值化算法二
- //黑白2
- private void btn2Color2_Click(object sender, EventArgs e)
- {
- Bitmap destImg = new Bitmap(sourceImg.Width, sourceImg.Height);
- BitmapData sourceData = sourceImg.LockBits(new Rectangle(0, 0, sourceImg.Width, sourceImg.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
- BitmapData destData = destImg.LockBits(new Rectangle(0, 0, sourceImg.Width, sourceImg.Height), ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
- IntPtr pSource = sourceData.Scan0;
- int allBytes = sourceData.Stride * sourceData.Height;
- byte[] rgbvalues = new byte[allBytes];
- Marshal.Copy(pSource, rgbvalues, 0, allBytes);
-
- int pos = 0;
- int R, G, B;
-
- int[] HistGram = new int[256];
-
- for (int j = 0; j < sourceData.Height; j++)
- {
- for (int i = 0; i < sourceData.Width; i++)
- {
- R = rgbvalues[pos + 2];
- HistGram[R] += 1;
- pos = pos + 3;
- }
- pos = pos + sourceData.Stride - sourceData.Width * 3;
- }
-
- int threshold;
-
- int allSum = 0;
- int allCount=0;
- for(int k= 0;k<= 255;k++)
- {
- allCount += HistGram[k];
- allSum += k * HistGram[k];
- }
-
- threshold = allSum / allCount;
- pos = 0;
- for (int j = 0; j < sourceData.Height; j++)
- {
- for (int i = 0; i < sourceData.Width; i++)
- {
- R = rgbvalues[pos + 2];
- if (R >= threshold)
- R = 255;
- else
- R = 0;
- rgbvalues[pos] = (byte)R;
- rgbvalues[pos + 1] = (byte)R;
- rgbvalues[pos + 2] = (byte)R;
- pos = pos + 3;
- }
- pos = pos + sourceData.Stride - sourceData.Width * 3;
- }
-
-
- IntPtr pDest= destData.Scan0;
- Marshal.Copy(rgbvalues, 0, pDest, allBytes);
- sourceImg.UnlockBits(sourceData);
- destImg.UnlockBits(destData);
- picDest.Image = destImg;
- }
学习更多vb.net知识,请参看vb.net 教程 目录
学习更多C#知识,请参看 C# 教程 目录
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。