Image processing in C #

This article is reproduced connection:  https://blog.csdn.net/wchstrife/article/details/78984735

Using C # image processing
preface
before always thought that the image processing is a matter of a very big, encountered a job of image processing on an elective class, started a few simple image processing algorithms, it can be considered into the the simplest door. 
The interface is simple, ugly, the code name also more casual, we focus on algorithms like 
here to achieve a total of vignetting, reducing brightness, grayscale, reliefs, mosaics, six diffusion algorithm. 
Project github address: https: //github.com/wchstrife/ImageProcessing

Interface design
used here is VS2010, after the new C # project. Draw on the screen 
- as the pictures show the control two pictureBox. 
- Six button as a trigger different effects, 
- 2 button to open and save the file as a trigger, 
- 1 label responsible for displaying the running time.

File Open and Save

Here is the main call openFileDialog and openFileDialog, not specifically. 
open a file:

private void button7_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string path = openFileDialog1.FileName;
                bitmap = (Bitmap)Image.FromFile(path);
                pictureBox1.Image = bitmap.Clone() as Image;
            }
        }

save document:

private void button8_Click(object sender, EventArgs e)
        {
            bool isSave = true;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string fileName = saveFileDialog1.FileName.ToString();

                if (fileName != "" && fileName != null)
                {
                    string fileExtName = fileName.Substring(fileName.LastIndexOf(".") + 1).ToString();

                    System.Drawing.Imaging.ImageFormat imgformat = null;

                    if (fileExtName != "")
                    {
                        switch (fileExtName)
                        {
                            case "jpg":
                                imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;
                                break;
                            case "bmp":
                                imgformat = System.Drawing.Imaging.ImageFormat.Bmp;
                                break;
                            case "gif":
                                imgformat = System.Drawing.Imaging.ImageFormat.Gif;
                                break;
                            default:
                                MessageBox.Show ( "only have access to: jpg, bmp, gif format");
                                isSave = to false;
                                BREAK;
                        }

                    }

                    // Default Save JPG format   
                    IF (imgformat == null)
                    {
                        imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;
                    }

                    IF (isSave)
                    {
                        the try
                        {
                            this.pictureBox2.Image.Save (fileName, imgformat);
                            //MessageBox.Show ( "Image has been successfully saved!");   
                        }
                        the catch
                        {
                            MessageBox.Show ( "Failed to save, you have not intercepted had been emptied pictures or images ");!
                        }
                    }
                }
            }
        }

Add vignetting
vignetting is to add a circle gradually darkening in the corners of the image.

Basic steps of:
calculating a distance of the center maxDistance vertex
distance is calculated for each pixel from the center point
calculation factor = distance / maxDistance
the current color pixel is set to the original color * (1-factor)
effects in FIG.

private void button1_Click(object sender, EventArgs e)
        {
            if (bitmap != null)
            {
                newbitmap = bitmap.Clone() as Bitmap;

                sw.Reset();
                sw.Restart();

                int width = newbitmap.Width;
                int height = newbitmap.Height;
                float cx = width / 2;
                float cy = height / 2;
                float maxDist = cx * cx + cy * cy;
                float currDist = 0, factor;
                Color pixel; 

                for (int i = 0; i < width; i++)
                {
                    for (int j = 0; j < height; j++)
                    {
                        currDist = ((float)i - cx) * ((float)i - cx) + ((float)j - cy) * ((float)j - cy);
                        factor = currDist / maxDist;

                        pixel = newbitmap.GetPixel(i, j);
                        int red = (int)(pixel.R * (1 - factor));
                        int green = (int)(pixel.G * (1 - factor));
                        int blue = (int)(pixel.B * (1 - factor));
                        newbitmap.SetPixel(i, j, Color.FromArgb(red, green, blue));
                    }
                }

                sw.Stop();
                timer.Text = sw.ElapsedMilliseconds.ToString();
                pictureBox2.Image = newbitmap.Clone() as Image;               
            }
        }

Decrease brightness

The basic steps

It is provided to reduce the brightness of the original color of the current pixel a coefficient of less than 1, to be noted that components of the colors can not exceed 255. Here we choose 0.6 as a coefficient.

Renderings

private void button2_Click(object sender, EventArgs e)
        {
            if (bitmap != null)
            {
                newbitmap = bitmap.Clone() as Bitmap;
                sw.Reset();
                sw.Restart();
                Color pixel;              
                int red, green, blue;
                for (int x = 0; x < newbitmap.Width; x++)
                {
                    for (int y = 0; y < newbitmap.Height; y++)
                    {
                        pixel = newbitmap.GetPixel(x, y);
                        red = (int)(pixel.R * 0.6);
                        green = (int)(pixel.G * 0.6);
                        blue = (int)(pixel.B * 0.6);
                        newbitmap.SetPixel(x, y, Color.FromArgb(red, green, blue));
                    }
                }
                sw.Stop();
                timer.Text = sw.ElapsedMilliseconds.ToString();
                pictureBox2.Image = newbitmap.Clone() as Image;
            }
        }

To color

The basic steps

That is, to take color photographs ashing, RGB adjust photo of gray. 
Particular color is the current pixel should be adjusted as the following equation 
gary = 0.3 * R + 0.59 * G + 0.11 * B

Renderings

private void button3_Click(object sender, EventArgs e)
        {
            if (bitmap != null)
            {
                newbitmap = bitmap.Clone() as Bitmap;
                sw.Reset();
                sw.Restart();
                Color pixel;
                int gray;
                for (int x = 0; x < newbitmap.Width; x++)
                {
                    for (int y = 0; y < newbitmap.Height; y++)
                    {
                        pixel = newbitmap.GetPixel(x, y);
                        gray = (int)(0.3 * pixel.R + 0.59 * pixel.G + 0.11 * pixel.B);
                        newbitmap.SetPixel(x, y, Color.FromArgb(gray, gray, gray));
                    }
                }
                sw.Stop();
                timer.Text = sw.ElapsedMilliseconds.ToString();
                pictureBox2.Image = newbitmap.Clone() as Image;
            }
        }

Relief

The basic steps

Relief effect is to put three RGB colors inverted. 
255- component with a specific implementation of the current color

Renderings

private void button4_Click(object sender, EventArgs e)
        {
            if (bitmap != null)
            {
                newbitmap = bitmap.Clone() as Bitmap;
                sw.Reset();
                sw.Restart();
                Color pixel;
                int red, green, blue;
                for (int x = 0; x < newbitmap.Width; x++)
                {
                    for (int y = 0; y < newbitmap.Height; y++)
                    {
                        pixel = newbitmap.GetPixel(x, y);
                        red = (int)(255 - pixel.R);
                        green = (int)(255 -  pixel.G);
                        blue = (int)(255 - pixel.B);
                        newbitmap.SetPixel(x, y, Color.FromArgb(red, green, blue));
                    }
                }
                sw.Stop();
                timer.Text = sw.ElapsedMilliseconds.ToString();
                pictureBox2.Image = newbitmap.Clone() as Image;
            }
        }

Mosaics

The basic steps

Mosaic basic idea is to pixels around a pixel to take an average, then these color pixels is set to this average. 
Pixels around the take, the more effect Mosaic is also more obvious.

Renderings

/ **
         * mosaic
         * default effect grid 50
         * * /
        Private void button5_Click (SENDER Object, EventArgs E)
        {
            IF (Bitmap = null!)
            {
                Newbitmap = bitmap.Clone () AS Bitmap;
                sw.Reset () ;
                sw.Restart ();
                int RIDIO = 50; // mosaic scale, surrounding two default pixels
                for (int 0 = H; H <newbitmap.Height; H + = RIDIO)
                {
                    for (int W = 0; W <newbitmap.Width; = W + RIDIO)
                    {
                        int avgRed = 0, 0 = avgGreen, avgBlue = 0;
                        int COUNT = 0;
                        //取周围的像素
                        for (int x = w; (x < w + RIDIO && x < newbitmap.Width); x++)
                        {
                            for (int y = h; (y < h + RIDIO && y < newbitmap.Height); y++)
                            {
                                Color pixel = newbitmap.GetPixel(x,y);
                                avgRed += pixel.R;
                                avgGreen += pixel.G;
                                avgBlue += pixel.B;
                                count++;
                            }
                        }

                        // averaged
                        avgRed = avgRed / COUNT;
                        avgBlue = avgBlue / COUNT;
                        avgGreen = avgGreen / COUNT;

                        //设置颜色
                        for (int x = w; (x < w + RIDIO && x < newbitmap.Width); x++)
                        {
                            for (int y = h; (y < h + RIDIO && y < newbitmap.Height); y++)
                            {
                                Color newColor = Color.FromArgb(avgRed, avgGreen ,avgBlue);
                                newbitmap.SetPixel(x, y, newColor);
                            }
                        }
                    }
                }
                sw.Stop();
                timer.Text = sw.ElapsedMilliseconds.ToString();
                pictureBox2.Image = newbitmap.Clone() as Image;
            }
        }

Diffusion effect

The basic steps

Diffusion of ink on paper similar. A randomly selected adjacent pixels, set their own color. 
Note that this must be randomly selected surrounding pixels.

Renderings

private void button6_Click(object sender, EventArgs e)
        {
            if (bitmap != null)
            {
                newbitmap = bitmap.Clone() as Bitmap;
                sw.Reset();
                sw.Restart();
                Color pixel;
                int red, green, blue;
                int flag = 0;
                for (int x = 0; x < newbitmap.Width; x++)
                {
                    for (int y = 0; y < newbitmap.Height; y++)
                    {
                        Random ran = new Random();
                        int RankKey = ran.Next(-5, 5);
                        if (x + RankKey >= newbitmap.Width || y + RankKey >= newbitmap.Height || x + RankKey < 0 || y + RankKey < 0)
                        {
                            flag = 1;
                            continue;
                        }

                        pixel = newbitmap.GetPixel(x + RankKey, y + RankKey);
                        red = (int)(pixel.R);
                        green = (int)(pixel.G);
                        blue = (int)(pixel.B);
                        newbitmap.SetPixel(x, y, Color.FromArgb(red, green, blue));
                    }
                }
                sw.Stop();
                timer.Text = sw.ElapsedMilliseconds.ToString();
                pictureBox2.Image = newbitmap.Clone() as Image;
            }
        }

 

 


 

Released six original articles · won praise 189 · views 280 000 +

Guess you like

Origin blog.csdn.net/newbie_xymt/article/details/103687459