天天看點

GDI+——常用的圖像處理技術(三)

目錄 實作銳化效果 将彩色圖像變為黑白色 實作光照效果 實作油畫效果

實作原理:改變圖像像素點顔色值變化較大的地方,顔色值變化小的地方,改變不大,圖像的邊緣,由于相鄰兩個像素的顔色值變化較大,是以在銳化後,顔色值變化較大。

GDI+——常用的圖像處理技術(三)

代碼:

   public Image AcuteEffect(PictureBox Pict)

       {

           int Var_W = Pict.Width;                                     //擷取圖檔的寬度

           int Var_H = Pict.Height;                                        //擷取圖檔的高度

           Bitmap Var_bmp = new Bitmap(Var_W, Var_H);                  //根據圖檔的大小執行個體化Bitmap類

           Bitmap Var_SaveBmp = (Bitmap)Pict.Image;                        //根據圖檔執行個體化Bitmap類

           int[] Laplacian = { -1, -1, -1, -1, 9, -1, -1, -1, -1 };                    //拉普拉斯模闆

           //周遊圖象中的各象素

           for (int i = 1; i < Var_W - 1; i++)

               for (int j = 1; j < Var_H - 1; j++)

               {

                   int tem_r = 0, tem_g = 0, tem_b = 0, tem_index = 0;             //定義變量

                   for (int c = -1; c <= 1; c++)

                       for (int r = -1; r <= 1; r++)

                       {

                           Color tem_color = Var_SaveBmp.GetPixel(i + r, j + c);       //擷取指定象素的顔色值

                           tem_r += tem_color.R * Laplacian[tem_index];            //設定R色值

                           tem_g += tem_color.G * Laplacian[tem_index];            //設定G色值

                           tem_b += tem_color.B * Laplacian[tem_index];            //設定B色值

                           tem_index++;

                       }

                   tem_r = tem_r > 255 ? 255 : tem_r;  //如果R色值大于255,将R色值設為255,否則不變

                   tem_r = tem_r < 0 ? 0 : tem_r;      //如果R色值小于0,将R色值設為0,否則不變

                   tem_g = tem_g > 255 ? 255 : tem_g;  //如果G色值大于255,将R色值設為255,否則不變

                   tem_g = tem_g < 0 ? 0 : tem_g;      //如果G色值小于0,将R色值設為0,否則不變

                   tem_b = tem_b > 255 ? 255 : tem_b;  //如果B色值大于255,将R色值設為255,否則不變

                   tem_b = tem_b < 0 ? 0 : tem_b;      //如果B色值小于0,将R色值設為0,否則不變

                   Var_bmp.SetPixel(i - 1, j - 1, Color.FromArgb(tem_r, tem_g, tem_b));    //設定指定象素的顔色

               }

           return Var_bmp;

       }

       private void button1_Click(object sender, EventArgs e)

           pictureBox1.Image = AcuteEffect(pictureBox1);

原理:将一張彩色的圖檔,按照色圖像的RGB顔色值,通過平均值法,将其改為比較柔和的黑白圖像。

GDI+——常用的圖像處理技術(三)

public Image BlackandWhiteEffect(PictureBox Pict)

           int Var_H = Pict.Image.Height;//擷取圖象的高度

           int Var_W = Pict.Image.Width;//擷取圖象的寬度

           Bitmap Var_bmp = new Bitmap(Var_W, Var_H);//根據圖象的大小執行個體化Bitmap類

           Bitmap Var_SaveBmp = (Bitmap)Pict.Image;//根據圖象執行個體化Bitmap類

           //周遊圖象的象素

           for (int i = 0; i < Var_W; i++)

               for (int j = 0; j < Var_H; j++)

                   Color tem_color = Var_SaveBmp.GetPixel(i, j);//擷取目前象素的顔色值

                   int tem_r, tem_g, tem_b, tem_Value = 0;//定義變量

                   tem_r = tem_color.R;//擷取R色值

                   tem_g = tem_color.G;//擷取G色值

                   tem_b = tem_color.B;//擷取B色值

                   tem_Value = ((tem_r + tem_g + tem_b) / 3);//用平均值法産生黑白圖像

                   Var_bmp.SetPixel(i, j, Color.FromArgb(tem_Value, tem_Value, tem_Value));//改變目前象素的顔色值

           pictureBox1.Image = BlackandWhiteEffect(pictureBox1);

實作原理:在圖檔指定的中心點設定個圓,并按設定好的亮度值将圓中各像素的顔色值變亮,以形成光暈效果

GDI+——常用的圖像處理技術(三)

   public Image pp(PictureBox Pict, int x, int y, int R, float better)//R強光照射面的半徑,即”光暈”

           Bitmap Var_Bmp = new Bitmap(Pict.Image, Pict.Width, Pict.Height);//根據圖象執行個體化Bitmap類

           int tem_W = Var_Bmp.Width;//擷取圖象的寬度

           int tem_H = Var_Bmp.Height;//擷取圖象的高度

           //定義一個Bitmap類的複本

           Bitmap Var_SaveBmp = Var_Bmp.Clone(new RectangleF(0, 0, tem_W, tem_H), System.Drawing.Imaging.PixelFormat.DontCare);

           Point Var_Center = new Point(x, y);//光暈的中心點

           for (int i = tem_W - 1; i >= 1; i--)

           {

               for (int j = tem_H - 1; j >= 1; j--)

                   float Var_Length = (float)Math.Sqrt(Math.Pow((i - Var_Center.X), 2) + Math.Pow((j - Var_Center.Y), 2));//設定光暈的範圍

                   //如果像素位于”光暈”之内_

                   if (Var_Length < R)

                   {

                       Color Var_Color = Var_SaveBmp.GetPixel(i, j);

                       int r, g, b;

                       float Var_Pixel = better * (1.0f - Var_Length / R);//設定光亮度的強弱

                       r = Var_Color.R + (int)Var_Pixel;//設定加強後的R值

                       r = Math.Max(0, Math.Min(r, 255));//如果R值不在顔色值的範圍内,對R值進行設定

                       g = Var_Color.G + (int)Var_Pixel;//設定加強後的G值

                       g = Math.Max(0, Math.Min(g, 255));//如果G值不在顔色值的範圍内,對G值進行設定

                       b = Var_Color.B + (int)Var_Pixel;//設定加強後的B值

                       b = Math.Max(0, Math.Min(b, 255));//如果B值不在顔色值的範圍内,對B值進行設定

                       Var_SaveBmp.SetPixel(i, j, Color.FromArgb(255, r, g, b));//将增亮後的像素值回寫到位圖

                   }

           }

           return Var_SaveBmp;

           pictureBox1.Image = pp(pictureBox1,49,47,40,100F);

實作原理:對彩色圖像的某一範圍内的像素進行随機處理,就可以産生油畫的效果。

GDI+——常用的圖像處理技術(三)

public partial class Frm_Main : Form

   {

       public Frm_Main()

           InitializeComponent();

       private Bitmap SourceBitmap;

       private Bitmap MyBitmap;

       private void button2_Click(object sender, EventArgs e)

           //打開圖像檔案

           OpenFileDialog openFileDialog = new OpenFileDialog();

           openFileDialog.Filter = "圖像檔案(JPeg, Gif, Bmp, etc.)|*.jpg;*.jpeg;*.gif;*.bmp;*.tif; *.tiff; *.png| JPeg 圖像檔案(*.jpg;*.jpeg)|*.jpg;*.jpeg |GIF 圖像檔案(*.gif)|*.gif |BMP圖像檔案(*.bmp)|*.bmp|Tiff圖像檔案(*.tif;*.tiff)|*.tif;*.tiff|Png圖像檔案(*.png)| *.png |所有檔案(*.*)|*.*";

           if (openFileDialog.ShowDialog() == DialogResult.OK)

               //得到原始大小的圖像

               SourceBitmap = new Bitmap(openFileDialog.FileName);

               //得到縮放後的圖像

               MyBitmap = new Bitmap(SourceBitmap, this.pictureBox1.Width, this.pictureBox1.Height);

               this.pictureBox1.Image = MyBitmap;

           //以油畫效果顯示圖像

           Graphics g = this.panel1.CreateGraphics();

           //取得圖檔尺寸

           int width = MyBitmap.Width;

           int height = MyBitmap.Height;

           RectangleF rect = new RectangleF(0, 0, width, height);

           Bitmap img = MyBitmap.Clone(rect, System.Drawing.Imaging.PixelFormat.DontCare);

           //産生随機數序列

           Random rnd = new Random();

           //取不同的值決定油畫效果的不同程度

           int iModel = 2;

           int i = width - iModel;

           while (i > 1)

               int j = height - iModel;

               while (j > 1)

                   int iPos = rnd.Next(100000) % iModel;

                   //将該點的RGB值設定成附近iModel點之内的任一點

                   Color color = img.GetPixel(i + iPos, j + iPos);

                   img.SetPixel(i, j, color);

                   j = j - 1;

               i = i - 1;

           //重新繪制圖像

           g.Clear(Color.White);

           g.DrawImage(img, new Rectangle(0, 0, width, height));

   }

繼續閱讀