版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。
灰度的算法请参看第17.3.1.3节。17.3.1.3 灰度-CSDN博客
【例 17.43】灰度算法一:最大值法。
- //灰度:最大值
- private void btnGray1_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 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];
- byte MaxColor;
- MaxColor =(byte)( R > G? R: G);
- MaxColor = MaxColor > (byte)B ? MaxColor: (byte)B;
- rgbvalues[pos] = MaxColor;
- rgbvalues[pos + 1] = MaxColor;
- rgbvalues[pos + 2] = MaxColor;
- 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.44】灰度算法二:平均值法。
- //灰度:均值法
- private void btnGray2_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 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 = (B + G + R) / 3;
- rgbvalues[pos] = (byte)avgValue;
- rgbvalues[pos + 1] = (byte)avgValue;
- rgbvalues[pos + 2] = (byte)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.45】灰度算法三:指数加权法。
- //灰度:指数加权法
- private void btnGray3_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 y;
-
- 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];
-
- y =(byte)( R * 0.3 + G * 0.59 + B * 0.11);
- rgbvalues[pos] = y;
- rgbvalues[pos + 1] = y;
- rgbvalues[pos + 2] = y;
- 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# 教程 目录