MATLAB Answers

convert gray image back to rgb

1,060 views (last 30 days)
zzz
zzz on 14 Mar 2013
Commented: Benoit Espinola on 17 Jun 2020
how to back grat to rgb gray2rgb function not found
  1 Comment
Benoit Espinola
Benoit Espinola on 17 Jun 2020
What do you mean by "back to rgb"?
Do you mean to put original colors back into the image? If so, that is impossible, you lost the information when saving the image in black and white.
If you mean "RGB data format" then you just need to repeat the grey value 3 times such as: RGB = [G G G] where G is a column vector.

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.
  1 Comment
Image Analyst
Image Analyst on 9 Mar 2020
Start a new question and attach your image and segmentation code.

Sign in to comment.

More Answers (3)

Shaun VanWeelden
Shaun VanWeelden on 14 Mar 2013
Alternatively
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
  4 Comments
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
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);
  2 Comments
Walter Roberson
Walter Roberson on 18 Nov 2016
No, this always works for gray images.
What it does not work for is
  • pseudocolor images: use ind2rgb for those
  • black and white images represented as datatype logical and for which the converted image is to be displayed using imshow() instead of image(): for those either use image() or use double() on the result of the cat()

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.
  2 Comments
Walter Roberson
Walter Roberson on 18 Nov 2016
gray = uint8(bw*255);
or
gray = double(bw);

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!