MATLAB Answers

convert gray image back to rgb

1.322 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

Show 1 older comment
Walter Roberson
Walter Roberson on 10 May 2016
It will become an RGB image whose colors all happen to be gray.
My tests show that if you take all possible RGB values and convert them to gray, that on average 65536 different combinations map to each value. 7 combinations map to complete black and 7 map to complete white; there are four shades that are created by 111642 different combinations. Clearly it is not possible to take a grayscale image and map it back to "the" original color.
SHAIKH TAUSEEF HASAN
SHAIKH TAUSEEF HASAN on 18 Nov 2016
rgb=img(:,:,[1 2 3]);
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

Muhammad Waqas
Muhammad Waqas on 29 Feb 2016
it can work some time
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

Shaun VanWeelden
Shaun VanWeelden on 14 Mar 2013
black and white to grayscale is easy, gray=uint8(bw.*254+1), and to convert that to rgb just do one of the other answers
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!

Translated by