ITeye博客

GDI+入门(十三、GDI+实例――灰度效果)

bmabma · 2012-01-17 00:29 · 技术杂绘 · 非技术 · Markdown

GDI+入门(十三、GDI+实例――灰度效果) 2011年11月19日   十三、GDI+实例――灰度效果   图片灰度化处理的方法主要有三种:   最大化法。这种方法的原理是使RGB的值等于值,即:   R=G=B=max(R,G,B)   使用最大化法处理后的灰度图像的亮度很高。   平均值法。这种方法的原理是使RGB的恒等于三个色彩分量的平均值,即   R=G=B= (R+G+B)/3   使用平均值法处理后的灰度图像亮度较为柔和。   加权平均值法。 “加权”是统计学中常用的名词,这种方法是根据重要性或其他   指标给R、G、D赋于不同的权值,并位R、G、B等于它们的加权平均值。即:   R=G=B=(rR+gG+bB))/3,r、g、b分别是R、G、B的权值*权数r、g、b取不同的值时,加权平均值法能够形成不同灰度的灰度图像。由于人眼对绿色的敏感度最高,红色次之,对蓝色的敏感度最低,因此,当权值矿的时,所生成的灰度图像更符合人眼的视觉感受。通常,当r=30%,g=59%,b=11%时,这种图像的灰度较为合理。   private static Bitmap bitmap;   private int width;   private int height;   public Form1()   {   InitializeComponent();   bitmap = new Bitmap(Application.StartupPath + "//1.jpg");   width = bitmap.Width - 1;   height = bitmap.Height - 1;   }   Bitmap bmp2;   Bitmap bmp3;   Bitmap bmp4;   private void button1_Click(object sender, EventArgs e)   {   Graphics panel2G = panel2.CreateGraphics();   bmp2 = new Bitmap(width, height);   Color color;   for (int i = 0; i bmp2, panel2.ClientRectangle);   panel2G.ResetTransform();   panel2G.TranslateTransform(width, 0);   // bmp2.Dispose();   panel2G.Dispose();   }   private void panel1_Paint(object sender, PaintEventArgs e)   {   Graphics panel1G = panel1.CreateGraphics();   panel1G.DrawImage(bitmap, panel1.ClientRectangle);   panel1G.Dispose();   }   private void ReDrawImage(Bitmap bmp0)   {   Color color;   for (int i = width - 1; i >= 0; i--)   {   for (int j = height - 1; j >= 0; j--)   {   color = bmp0.GetPixel(i, j);   int r = color.R;   int g = color.G;   int b = color.B;   //还原红色   if (r = 192)   r = 255;   if (r = 128)   r = 4 * r - 510;   //还原绿色   bool yes;   yes = false;   if (g = 128 && (!yes))   {   g = 255;   yes = true;   }   if (g >= 192 && (!yes))   {   g = 1022 - 4 * g;   yes = true;   }   if (g = 67 && (!yes))   {   g = 4 * g - 257;   }   //还原蓝色分量   if (b = 128)   b = 0;   if (b >= 67 && b bmp0, panel3.ClientRectangle);   bmp0.Dispose();   panel3G.Dispose();   }   ///   /// 最大值   ///   ///   ///   private void button3_Click(object sender, EventArgs e)   {   Graphics panel2G = panel2.CreateGraphics();
  bmp3 = new Bitmap(width, height);   Color color;   for (int i = 0; i bmp3, panel2.ClientRectangle);   // bmp3.Dispose();   panel2G.Dispose();   }   ///   /// 加权平均   ///   ///   ///   private void button4_Click(object sender, EventArgs e)   {   double r = 0.30;   double g = 0.59;   double b = 0.11;   bmp4 = new Bitmap(width, height);   Graphics panel2G = panel2.CreateGraphics();   Color color;   for (int i = 0; i bmp4, panel2.ClientRectangle);   // bmp4.Dispose();   panel2G.Dispose();   }   private int maxColor(int r, int g, int b)   {   if (r > g)   {   if (r > b)   {   return r;   }   else   {   return b;   }   }   else   {   if (g > b)   {   return g;   }   else   {   return b;   }   }   }   private void button2_Click(object sender, EventArgs e)   {   if (bmp3 == null)   {   MessageBox.Show("请先点击变灰按钮");   return;   }   ReDrawImage(bmp3.Clone(new Rectangle(0,0,width,height),PixelFormat.DontCare));   }   private void button2_Click_1(object sender, EventArgs e)   {   if (bmp2 == null)   {   MessageBox.Show("请先点击变灰按钮");   return;   }   ReDrawImage(bmp2.Clone(new Rectangle(0,0,width,height),PixelFormat.DontCare));   }   private void button6_Click(object sender, EventArgs e)   {   if (bmp4 == null)   {   MessageBox.Show("请先点击变灰按钮");   return;   }   ReDrawImage(bmp4.Clone(new Rectangle(0,0,width,height),PixelFormat.DontCare));   }   button1_Click:平均值法   panel1_Paint:加载原图   ReDrawImage:灰色图像还原彩色图像   button3_Click:最大值法制作灰度图像   button4_Click:加权平均法制作灰度图像   maxColor:求颜色分量最大值   平均值方法:   最大值法:   加权平均法

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。