MATLAB: Gray image to rgb using ind2rgb

gray image to rgb using colormapImage Processing Toolbox

please can someone show me how to convert a rgb image to gray and convert that gray image back to rgb using ind2rgb…. how to obtain map for the function ind2rgb….. Please do reply….

Best Answer

You cannot convert grayscale images back to rgb without loss of information. Many different combinations of RGB values convert to the same grayscale level, so if all you have at a location is the grayscale level, then you cannot know which of the 65536 RGB values that map to the exact same grayscale that the RGB came from.
You can convert RGB to pseudocolor (ind). You could save a copy of the map used for that. You could convert each member of the pseudocolor map into grayscale and display it that way, and later you could convert back the pseudocolor map into RGB. However, if you want to do anything that modifies the grayscale (such as adaptive equalization) then you will have trouble.
[indimg, indmap] = rgb2ind(YourImage);
indasgray = rgb2gray(reshape(indmap,[size(indmap,1),1,3]));
image(indimg);
colromap(indasgray); %image painted gray but multiple ind might have same gray
ReconstructedImage = ind2rgb(indimg, indmap); %not indasgray