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
picGray is the image which should have the converted gray scale image.
Bitmap bmp = (Bitmap)pictureBox1.Image;
int NumRow = pictureBox1.Height;
int numCol = pictureBox1.Width;
Bitmap GRAY = new Bitmap(pictureBox1.Width, pictureBox1.Height);
for (int i = 0; i < NumRow; i++)
for (int j = 0; j < numCol; j++)
Color c = bmp.GetPixel(j, i);
int rd = c.R; int gr = c.G; int bl = c.B;
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]
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.