Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
2.50/5 (4 votes)
See more:
how can i convert a colored bitmap image to grayscale image?
Posted
Comments
OriginalGriff 11-Mar-11 15:15pm    
Reason for my vote of one: too lazy to Google.

Google is your friend: Be nice and visit him often. He can answer questions a lot more quickly than posting them here...

A quick search using "convert a colored bitmap image to grayscale image" pasted from your subject line gave 3/4 million hits. The top hit is a tutorial on exactly how to do it in c#...

http://www.switchonthecode.com/tutorials/csharp-tutorial-convert-a-color-image-to-grayscale[^]

Learn to use Google: it can be a lot quicker than posting a question here.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 12-Mar-11 0:09am    
Griff, this is not as trivial. Please see my Answer. What's good about the work you reference is that the author mentioned that there are more than one way.
--SA
Use the color matrix and set it to the image attributes.

The color matrix for gray scale is

C#
//Gilles Khouzams colour corrected grayscale shear
        ColorMatrix cm = new ColorMatrix(new float[][]{   new float[]{0.3f,0.3f,0.3f,0,0},
                                  new float[]{0.59f,0.59f,0.59f,0,0},
                                  new float[]{0.11f,0.11f,0.11f,0,0},
                                  new float[]{0,0,0,1,0,0},
                                  new float[]{0,0,0,0,1,0},
                                  new float[]{0,0,0,0,0,1}});


refer

http://www.bobpowell.net/grayscale.htm[^]

About 582,000 results (0.14 seconds) results in google.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 12-Mar-11 0:06am    
Albin, I up-voted your answer by my 5.
However, the problem is much more non-trivial.
Please see my Answers and my comment to a comment by Toli.
--SA
First, you must load the image into memory. As you know, there are quite a few different formats around. Depending on which format the original image is, you may need an external library to load it.

Once you have loaded it, you must go through the pixels of the image and convert them to grayscale. Grayscale means, that all components (R, G and B) of the colors have the same value. This way you get 256 colors from black (R = 0, G = 0, B = 0) to white (R = 255, G = 255, B = 255).

For each pixel you must find a grayscale color value with similar brightness and replace the pixel's color value with it. I would simply try to calculate the average of the original R, G and B and use this value for all three components of the grayscale pixel:

R2 = G2 = B2 = (R1 + G1 + B1) / 3

After modifying each pixel, you simply must save the image again. Sorry, but without knowing the image's format and what you use to load it, it's hard to explain more precisely.
 
Share this answer
 
Comments
Toli Cuturicu 11-Mar-11 18:26pm    
False.
Sergey Alexandrovich Kryukov 12-Mar-11 0:05am    
Toli... sigh... You see, I understand the method shown in this Answer is quite naive, and something tells me CDP1802 may be illiterate in this topic to some extent. However, I cannot agree or even tolerate the vote of "1" in this case, because by this vote you say "you lie!". I vote "1" myself from time to time when the Answer is a lie.

This answer is not the case. It will show some gray-scale picture (probably two slow), which is in certain sense is not worse then others.

You would be quite right is some absolute ideal solution existed, which could be taken for standard. Unfortunately (or fortunately), such standard does not and cannot exits. Why? I try to explain it in my Answer.

I up-voted this Answer quite a bit by this reason.
Thank you for understanding.

--SA
[no name] 12-Mar-11 4:07am    
Thanks, I guess. And I will do my best to be less naive in the future. Next time I might write a little about how to set up a vertex shader and let the GPU do the work. Depending on what he is actually trying to do (and on what framework / platform), this may be totally useless, but it will be very cool and fast.
OriginalGriff 12-Mar-11 9:14am    
I agree with SA - it doesn't deserve a 1. Compensated.
Toli Cuturicu 14-Mar-11 17:59pm    
The problem was that Red, Green and Blue should not have the same 1/3 factor, since do not have the same luminosity. Just look at solution 1 and see the right factors: 0.30, 0.59 and 0.11, quite different from 0.33, 0.33 and 0.33.
All the Answers presented here are correct in certain sense, but I want to add my notes.

How good these methods are depends on the purpose. It this is just to convert the image to gray-scale the way it still can be recognized, all the method would pass in most cases. There is a different point of view of the people who go in for digital black-and-white art photography. This days, only art photography can be black-and-white (and also in some industrial/scientific applications). They are somewhat different people, all of them use computers, usually don't know anything except Photoshop and most of them are amazingly illiterate in computer technology (I used to make fun of whole staff of the Popular Photography Magazine: every one who delivered their "technical expert advice" gave their advices based on their misconception that lostless compression does not exist). Nevertheless, they know something which is missing in the given answers. I know for sure what they say if then see the gray-scale picture converted using the shown methods: what a crap!

A real expert advice from a good photographer (they do it exactly in the same way as we do it at CodeProject) usually goes like that: don't use embedded "convert to gray-scale" method! Depending on the picture, you need to use separate color channel and convert them to gray-scale separately. For example, for a landscape with blue sky, start with red channel only. This is because the most difficult problem is over-exposing of the very bright sky, but the red channel is the least exposed by the sky, so you can reduce unwanted contrast. Then you may want to add some of green channel. Very often you need to add dynamic range to green forest, do the following. Blah-blah…

Anyway, we should always take into account, that there are many different conversions from color to gray-scale; in all cases it is done with loss of information. None of the methods is a priori better then others. Anyway, the considerations presented above should discourage any arguments on which method is really "kosher", and which is really "orthodox". :-)

—SA
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900