how to back grat to rgb gray2rgb function not found

3 commentaires

Benoit Espinola
Benoit Espinola le 17 Juin 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.
AliHdr
AliHdr le 30 Oct 2021
I have done this but i can not use imwrite anymore
there is and error
<<Data with 9 components not supported for JPEG files.>>
how can I overwrite new rgb file with old gray file?
Image Analyst
Image Analyst le 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.

Connectez-vous pour commenter.

 Réponse acceptée

Image Analyst
Image Analyst le 14 Mar 2013

9 votes

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.

3 commentaires

Image Analyst
Image Analyst le 9 Mar 2020
Start a new question and attach your image and segmentation code.
Alexandar
Alexandar le 1 Juil 2022
What do you mean by create a colormap and how can you do it within the scales that you'd like?
Image Analyst
Image Analyst le 1 Juil 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.

Connectez-vous pour commenter.

Plus de réponses (3)

Shaun VanWeelden
Shaun VanWeelden le 14 Mar 2013

5 votes

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 commentaires

su mon aung
su mon aung le 10 Mai 2016
rgb=img(:,:,[1 1 1]; this equation still remains my image as grayscale image
Walter Roberson
Walter Roberson le 10 Mai 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 le 18 Nov 2016
rgb=img(:,:,[1 2 3]);
Image Analyst
Image Analyst le 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.

Connectez-vous pour commenter.

Jan
Jan le 14 Mar 2013

4 votes

Asking Google for "Matlab gray2rgb" would be a good idea.
But a general method is:
RGB = cat(3, Gray, Gray, Gray);

4 commentaires

Muhammad Waqas
Muhammad Waqas le 29 Fév 2016
it can work some time
Walter Roberson
Walter Roberson le 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()
john
john le 10 Jan 2022
what do u mean by use double? its giving me error when i use imshow
sample_logical_2d = rand(64,80) > 0.8;
imshow(sample_logical_2d)
sample_logical_3d = cat(3, sample_logical_2d, sample_logical_2d, sample_logical_2d );
try
imshow(sample_logical_3d)
catch ME
fprintf('oooo! imshow did not like 3D logical!')
disp(ME)
end
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);
imshow(sample_double_3d)

Connectez-vous pour commenter.

shehbaz Ali
shehbaz Ali le 14 Mar 2013

1 vote

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.

5 commentaires

Shaun VanWeelden
Shaun VanWeelden le 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
gray = uint8(bw*255);
or
gray = double(bw);
DGM
DGM le 14 Juin 2023
I think it's fairly obvious what @shehbaz Ali meant, and I would agree. The conversion from a grayscale or color image to a logical mask irreversibly discards information.
For more details, see the explanation written on this page:
Walter Roberson
Walter Roberson le 14 Juin 2023
You cannot gain information when you convert black and white to grayscale, or grayscale to color -- but you can create the grayscale or color image with the same amount of information.
DGM
DGM le 15 Juin 2023
That's a good way to put it. :)

Connectez-vous pour commenter.

Catégories

En savoir plus sur Convert Image Type dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by