12,998,863 members (69,679 online)
Add your own
alternative version

#### Stats

56.8K views
5 bookmarked
Posted 13 Jan 2012

# Convert RGB to Gray Scale without using Pointers

, 13 Jan 2012
 Rate this:
Please Sign up or sign in to vote.
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]

## License

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

## About the Author

 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

116 members

 Pro

## Comments and Discussions

 First Prev Next
 Close but no cigar -- the grayscale conversion equation shou... iashdown17-Jan-12 9:38 iashdown 17-Jan-12 9:38
 Last Visit: 31-Dec-99 18:00     Last Update: 23-Jun-17 18:53 Refresh 1

General    News    Suggestion    Question    Bug    Answer    Joke    Praise    Rant    Admin

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

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