Grayscale image can be converted to a RGB image only if what
Grayscale image can be converted to a RGB image only if what?
Solution
Please follow the data and description :
To convert a grayscale image to an RGB image, here there are two issues that have to be checked for and addressed :
a) In general the grayscale images are 2-D, while that the RGB images are 3-D, so we need to replicate the grayscale image data three times and then move on to concatenate the three copies of the image along with a third dimension.
b) This is to be noted that the image data can be stored in various data types. When stored as a double data type, the image pixel values should be floating point numbers in the range of 0 to 1 and when they are stored as a uint8 data type, the image pixel values should be integers in the range of 0 to 255.
The possible conversions are :
a) When we assure to convert a uint8 or double grayscale image to an RGB image of the same data type, we can use the functions available REPMAT or CAT :
For example, rgbImage = repmat(grayImage,[1 1 3]);
rgbImage = cat(3,grayImage,grayImage,grayImage);
b) When we would like to convert a uint8 grayscale image to a double RGB image, we need to convert to double first, then scale by 255 :
As an example, rgbImage = repmat(double(grayImage)./255,[1 1 3]);
c) Now to convert a double grayscale image to a uint8 RGB image, we should scale the data by 255 first, and then convert to uint8 :
Example, rgbImage = repmat(uint8(255.*grayImage),[1 1 3]);
Hope this is helpful.
