Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do I convert a gray image to RGB image so as it can retain its original colors ?
I tried few things but it does not give the perfect recovery
Posted

It is impossible to give the perfect recovery without knowing something else about the image. Think about this equation:
y = r + g + b;

Easy enough to figure out "y" given the values of "r", "g", and "b"... you just plug them in and you get the value of "y". However, assuming you only have "y", how would you figure out the values of "r", "g", and "b"? You can't because there are multiple solutions. Assuming y = 10, here are some possible solutions:
r = 2, g = 6, b = 2
r = 6, g = 2, b = 2
r = 3, g = 3, b = 4
...

Converting between grayscale (the "y") and color (the "r", "g", and "b") has the same problem. You can convert to grayscale, but you can't convert back to color perfectly.
 
Share this answer
 
Actually, now that I think about it, you can sort of do this if you have control over converting the images to grayscale. If you have a grayscale image that is a higher color depth than the color image, then you can store the color information in the least significant bits of the grayscale image.

So, if you have a 24-bit color image you want to convert to a 32-bit grayscale image, you can store the grayscale information in the highest order byte (8-bits) and store the red, green, and blue I the lowest order 3 bytes (24-bits). When you convert back to color, you just get the color information from the 3 lowest order bytes. This might make the grayscale image look a little grainy though... not sure, you'd have to test it.

Likewise, if you have a 16-bit color image you want to convert to a 24-bit grayscale image, you can use 1 byte for the grayscale information and the other 2 bytes for the color information.

Some image formats allow you to store additional information with the image. So, you could store the color information in the hidden information for the image but the real image information would store the grayscale information. For example, I'm pretty sure JPEG's can store a thumbnail different than the actual image... you could make a full-size thumbnail that is in color. Also, BMP's have header information that indicate how much of the file is going to be dedicated to the actual image. If you store information at the end of the file (e.g., the color information), that might get ignored by some image viewing programs (others, unfortunately, might think the BMP is corrupt and not display it).

But if you don't have control over converting the images to grayscale, you're pretty much out of luck.
 
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