MATLAB Answers

convert gray image back to rgb

조회 수: 1,727(최근 30일)
zzz
zzz 14 Mar 2013
댓글: Benoit Espinola 17 Jun 2020
how to back grat to rgb gray2rgb function not found

  댓글 수: 1

Benoit Espinola
Benoit Espinola 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.

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 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

Image Analyst
Image Analyst 9 Mar 2020
Start a new question and attach your image and segmentation code.

댓글을 달려면 로그인하십시오.

추가 답변(3개)

Shaun VanWeelden
Shaun VanWeelden 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

표시 이전 댓글 수: 1
Walter Roberson
Walter Roberson 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 18 Nov 2016
rgb=img(:,:,[1 2 3]);
Image Analyst
Image Analyst 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.

댓글을 달려면 로그인하십시오.


Jan
Jan 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

Muhammad Waqas
Muhammad Waqas 29 Feb 2016
it can work some time
Walter Roberson
Walter Roberson 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()

댓글을 달려면 로그인하십시오.


shehbaz Ali
shehbaz Ali 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

Shaun VanWeelden
Shaun VanWeelden 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 18 Nov 2016
gray = uint8(bw*255);
or
gray = double(bw);

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by