Click here to Skip to main content
Click here to Skip to main content

Convert RGB to Gray Scale without using Pointers

By , 13 Jan 2012
Rate this:
Please Sign up or sign in to vote.
Ok, so what this technique does not do is, it does not convert a three channel image into a single channel image. All it does is convert the colors using gray scale conversion formula and puts the value in R,G and B component of the image. During processing, you can use any one of R,G or B channels to read the gray scale value.
 
The reason for me to post this method is its simplicity. So if you are looking for something "Fast" and "Efficient", you may try out techniques using pointers. But if you love simplicity and complete control over your image processing unit in simple ways, just have a glance.
 
By the way, Gray scale value corresponding to R,G,B is
GRAY = 0.2989 * RED + 0.5870 * GREEN + 0.1140 * BLUE
 
So let's code it Smile | :)
Let us assume that we have main color image in pictureBox1 and picGray is the image which should have the converted gray scale image.
void GrayConversion() 
{ 
   Bitmap bmp = (Bitmap)pictureBox1.Image;
   int NumRow = pictureBox1.Height; 
   int numCol = pictureBox1.Width;   
   Bitmap GRAY = new Bitmap(pictureBox1.Width, pictureBox1.Height);// GRAY is the resultant matrix 
 
   for (int i = 0; i < NumRow; i++) 
   { 
      for (int j = 0; j < numCol; j++) 
      { 
         Color c = bmp.GetPixel(j, i);// Extract the color of a pixel 
         int rd = c.R; int gr = c.G; int bl = c.B;// extract the red,green, blue components from the color.
         double d1 = 0.2989 * (double)rd + 0.5870 * (double)gr + 0.1140 * (double)bl; 
         int c1 = (int)Math.Round(d1); 
         Color c2 = Color.FromArgb(c1, c1, c1);
         GRAY.SetPixel(j, i, c2);
      }
   }
   picGray.Image = GRAY; 
}

All that we do here is, take red, green and blue of each pixel and use the formula to get the gray scale equivalent. We are constructing a custom color by using this gray value as R,G,B channel. Set the corresponding pixel Value with new color value.
 
For huge images, it might be a slow process ( but still faster than Matlab). The loop here can be used in several image processing application like Binary Image conversion, HSV image conversion and so on. Have fun.
 
[edit]Indented the code block - OriginalGriff[/edit]

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Grasshopper.iics
CEO Integrated Ideas
India India
gasshopper.iics is a group of like minded programmers and learners in codeproject. The basic objective is to keep in touch and be notified while a member contributes an article, to check out with technology and share what we know. We are the "students" of codeproject.
 
This group is managed by Rupam Das, an active author here. Other Notable members include Ranjan who extends his helping hands to invaluable number of authors in their articles and writes some great articles himself.
 
Rupam Das is mentor of Grasshopper Network,founder and CEO of Integrated Ideas Consultancy Services, a research consultancy firm in India. He has been part of projects in several technologies including Matlab, C#, Android, OpenCV, Drupal, Omnet++, legacy C, vb, gcc, NS-2, Arduino, Raspberry-PI. Off late he has made peace with the fact that he loves C# more than anything else but is still struck in legacy style of coding.
Rupam loves algorithm and prefers Image processing, Artificial Intelligence and Bio-medical Engineering over other technologies.
 
He is frustrated with his poor writing and "grammer" skills but happy that coding polishes these frustrations.
Group type: Organisation

54 members

Follow on   Twitter   Google+

Comments and Discussions

 
GeneralClose but no cigar -- the grayscale conversion equation shou... Pinmemberiashdown17-Jan-12 9:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140421.2 | Last Updated 14 Jan 2012
Article Copyright 2012 by Grasshopper.iics
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid