SIGN UP MEMBER LOGIN:    
ARTICLE

C# Image Proccessing Techniques: Part I

Posted by Tamer Khalil Articles | Algorithms in C# November 10, 2009
In the series of article we will build together our general use image processing class in C#.
Reader Level:

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.
 

Login to add your contents and source code to this article
share this article :
post comment
 

I have been searching image processing articles all over the Internet but your was the easiest and the best explained one. Thank you so much!!

Posted by ethen james Mar 25, 2012

Can anyone help me out with an algorithm in Image Processing using Sobel,SUSAN and SNAKE algorithm in c#

Posted by Yao Ziggah Nov 13, 2011

Thank You...

Posted by amirhossein kalantari Feb 14, 2011

hi dear Tamer i'm ali and today i strart to work with C# image processing. now i want to just open a bitmap file and show it. how can i do? so thanks.

Posted by Arash Feb 02, 2011

Can i use this technique as i am using A forge.net framework and doing my course project

Posted by Fatima Asif Dec 20, 2010
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor