15,905,420 members
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

## Solution 1

`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.

## Solution 2

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.