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
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]