C# Image Proccessing Techniques: Part I


HTML clipboard

Image processing libraries are not wildly used as in C or in Matlab. Although C# has some functionalities that facilitates image processing programming, but it needs to be well organized. Through out the next few weeks, we will build together our general use image processing class in C#. 

Before We Start

First we need to start a windows forms application.

Second Place a picturebox, and set a default image to any picture you want.

Getting A Color Component

Separating the three main components colors is very easy in C#, so if you want to get a picture with only the component you could use the following code

            Bitmap RedImage = new Bitmap(pictureBox1.Image);
            for (int i = 0; i < RedImage.Height; i++)
                for (int j = 0; j < RedImage.Width; j++)
                {
                    Color pixel = RedImage.GetPixel(j, i);
                    RedImage.SetPixel(j, i, Color.FromArgb(pixel.R, 0, 0));
                }
            return RedImage;

Taking into consideration that pictureBox1.Image is from the picture box object that you placed.

You can make a slight change to the function in order to get the Blue and Green components.

       // Green Component
        public Bitmap GetGreenComponent()
        {
            Bitmap GreenImage = new Bitmap(pictureBox1.Image);
            for (int i = 0; i < GreenImage.Height; i++)
                for (int j = 0; j < GreenImage.Width; j++)
                {
                    Color pixel = GreenImage.GetPixel(j, i);
                    GreenImage.SetPixel(j, i, Color.FromArgb(0, pixel.G, 0));
                }
            return GreenImage;
        }
        public Bitmap GetBlueComponent()
        {
            Bitmap BlueImage = new Bitmap(pictureBox1.Image);
            for (int i = 0; i < BlueImage.Height; i++)
                for (int j = 0; j < BlueImage.Width; j++)
                {
                    Color pixel = BlueImage.GetPixel(j, i);
                    BlueImage.SetPixel(j, i, Color.FromArgb(0, 0, pixel.B));
                }
            return BlueImage;
        }
 

Converting An Image From Colored (RGB) to Gray

We can convert the  image color scale from RGB to Gray scale, by getting the average value of each color component in a pixel, and placing the new value in each component.

        public Bitmap ConvertToGray()
        {
            Bitmap GrayImage = new Bitmap(pictureBox1.Image);
            for (int i = 0; i < GrayImage.Height; i++)
                for (int j = 0; j < GrayImage.Width; j++)
                {
                    Color pixel = GrayImage.GetPixel(j, i);
                    int Gray = (pixel.R + pixel.G + pixel.B) / 3;
                    GrayImage.SetPixel(j, i, Color.FromArgb(Gray, Gray, Gray));

                }

            return GrayImage;
        }
 

Don't forget to call the using System.Drawing; library.

In the next article we will write a program to detect a certain color, with a certain color components ranges.
 

Up Next
    Ebook Download
    View all
    Learn
    View all