12,550,449 members (43,744 online)
alternative version

54.7K views
5 bookmarked
Posted

Convert RGB to Gray Scale without using Pointers

, 13 Jan 2012 CPOL
 Rate this:
Simple way of Converting RGB Image to Gray Scale Image without using any pointers
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 :)
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.

Indented the code block - OriginalGriff[/edit]

Share

 CEO Integrated Ideas 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

118 members

You may also be interested in...

 Pro Pro

 First Prev Next
 Close but no cigar -- the grayscale conversion equation shou... iashdown17-Jan-12 9:38 iashdown 17-Jan-12 9:38
 Close but no cigar -- the grayscale conversion equation should be: GRAY = 0.2125 * RED + 0.7154 * GREEN + 0.0721 * BLUE to comply with ITU-R Recommendation BT.709, "Basic Parameter Values for the Studio and for International Programme Exchnage." This standard defines the chromaticities (colors) of virtually all modern color display monitors. The constants presented in this article are for NTSC CRT phosphors, which have been out of date for decades.
 Last Visit: 31-Dec-99 18:00     Last Update: 23-Oct-16 11:29 Refresh 1