convert gray image back to rgb

804 views (last 30 days)
zzz on 14 Mar 2013
Commented: Walter Roberson on 2 Jul 2022
how to back grat to rgb gray2rgb function not found
Image Analyst
Image Analyst on 31 Oct 2021
@AliHdr evidently JPG can't store multispectral or volumetric images. Either use a mat file or save each slice as its own image.

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 14 Mar 2013
If you have a color map - a mapping of what intensity should go to what color, like what imread() or rgb2ind() can give you - then you can use ind2rgb.
rgbImage = ind2rgb(grayImage, colormap);
Otherwise use
rgbImage = cat(3, grayImage, grayImage, grayImage);
grayImage needs to be in the range 0-255 uint8 if you want to display it.
Image Analyst
Image Analyst on 1 Jul 2022
@Alexandar There are a bunch of built-in colormap functions such as hsv, turbo, and jet. Or you can make up your own 256 by 3 matrix with values in the range 0-1.
To apply the colormap to the data range you want, you can use the clim or caxis() function.

Sign in to comment.

More Answers (3)

Shaun VanWeelden
Shaun VanWeelden on 14 Mar 2013
rgb=img(:,:,[1 1 1]); does the same thing as Jan's, but is a little shorter maybe and easier to remember. img is your image you want to convert to rgb obviously
Image Analyst
Image Analyst on 18 Nov 2016
No, that throws an error if img is a gray scale image since there is no third dimension for gray scale images.

Sign in to comment.

Jan on 14 Mar 2013
Asking Google for "Matlab gray2rgb" would be a good idea.
But a general method is:
RGB = cat(3, Gray, Gray, Gray);
Walter Roberson
Walter Roberson on 2 Jul 2022
sample_logical_2d = rand(64,80) > 0.8;
sample_logical_3d = cat(3, sample_logical_2d, sample_logical_2d, sample_logical_2d );
catch ME
fprintf('oooo! imshow did not like 3D logical!')
oooo! imshow did not like 3D logical!
MException with properties: identifier: 'images:imageDisplayValidateParams:expected2D' message: 'If input is logical (binary), it must be two-dimensional.' cause: {} stack: [7×1 struct] Correction: []
sample_double_3d = double(sample_logical_3d);

Sign in to comment.

shehbaz Ali
shehbaz Ali on 14 Mar 2013
This is not possible to to convert black and white image to gray image. Because you don't what will be colors of any pixel for rgb picture.
Walter Roberson
Walter Roberson on 18 Nov 2016
gray = uint8(bw*255);
gray = double(bw);

Sign in to comment.


Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!