How to convert grayscale to rgb ?
Mostrar comentarios más antiguos
I used the following script to convert grayscale image to rgb
[im, map] = imread('frame1.jpg');
if(isempty(map)) % image is RGB or grayscale
if(size(im, 3) == 1) % image is grayscale
im = cat(3, im, im, im);
end
else % image is indexed
im = ind2rgb(im, map);
end
with frame1 is grayscale. The problem is that when i execute imshow(im) it still without colors but size(im) is 144 176 3 I'm confused, how can i obtain image with colors ?
16 comentarios
Geoff Hayes
el 16 de En. de 2018
Ynne - but all the code is doing at
im = cat(3, im, im, im)
is creating the 144x176x3 matrix where im(:,:,1) is identical to im(:,:,2) which is identical to im(:,:,3). So the red, green and blue components are identical and so the image will still be in "grayscale".
Ynne
el 16 de En. de 2018
Image Analyst
el 16 de En. de 2018
What's wrong with what you're doing now? You're reading a grayscale image and converting it into a color image where all colors are shades of gray. So you have your RGB image. What's the problem?
halawati cm
el 18 de Feb. de 2020
hi i dont know how to display image back to RGB after convert in grayscale, my steps are
1- upload image from RGB
2- convert to grayscale
3- display result in RGB
im done for step 1 & 2 like this
% Process image to gray
grayImage = rgb2gray(app.RGBImage);
app.finalGrayImage = grayImage+100;
% Process image back to RGB
%show image in RGB
imshow(app.finalGrayImage,'Parent',app.UIAxes);
so, how to solve step 3? help me please.
Image Analyst
el 18 de Feb. de 2020
You can either set the variable back to the original rgb image:
app.finalGrayImage = app.RGBImage;
or you can create an RGB image from the gray scale image, but like I said, there will be no color, just shades of gray even though it's an RGB image.
brightenedImage = grayImage + 100;
% Stack 3 of these on top of each other to create a 3-D RGB image
% even though each color channel is identical and it will still appear as gray scale.
app.finalGrayImage = cat(3, brightenedImage, brightenedImage, brightenedImage);
It just depends on what you want to do (which I can't figure out).
halawati cm
el 15 de Mzo. de 2020
tqvm for your replied.
sorry if my explaination is not clear enough. Actually my project is to read RGB fundus image..like this

my steps:
1- upload image from RGB (fundus image above)
2- convert to grayscale
3- Then, i need to do brightness enhancement for the grayscale image (Step 2). (one of the techniques that i've choosen).
4- lastly, display the result in RGB color
Regarding your solution 1: "app.finalGrayImage = app.RGBImage;"
i cant apply like that since it doesnt changed anything.
the result should be display after i've done the enhancement process but need to display in RGB color.
then solution 2: "app.finalGrayImage = cat(3, brightenedImage, brightenedImage, brightenedImage);"
can you explain how to do this? how to create a 3-D RGB image?
tqvm for your response.
Image Analyst
el 15 de Mzo. de 2020
Doing
app.finalGrayImage = app.RGBImage;
will replace finalGrayImage with RGBImage, which is a color image. If you then display finalGrayImage it will show up as color, unless you overwrote RGBImage with a gray scale version.
The line of code:
app.finalGrayImage = cat(3, brightenedImage, brightenedImage, brightenedImage);
is, in itself, an explanation of how to do that. brightenedImage is a 2-D gray scale image. When you call cat(3....) it makes a 3-D image, which is interpreted as an RGB images. RGB images are 3-D arrays. In the case above, each of the red channel, green channel, and blue channel are all the same so the the image will appear gray scale, as I said in the comment above that line. Believe it or not RGB images can still appear gray if all color channels are the same. The only way to get back to the original color image is if you save the original color image and reassign it back to whatever variable you want it to be in.
halawati cm
el 22 de Mzo. de 2020
Editada: Image Analyst
el 22 de Mzo. de 2020
If I have grayscale image and colored image..like this
oriImage = imread('oriImage.jpg'); % read original image (colored image)
grayImage = rgb2gray(oriImage); % convert colored image to grayscale
imwrite(grayImage, 'gray.jpg'); % save grayscale image
rgbImage = ?
So, by showing me the code, how do I display the grayscale image that maps with the colored image as a final output (rgbImage)?
tqvm for your response.
Image Analyst
el 22 de Mzo. de 2020
Not sure what you mean. You said oriImage was an RGB image, so that's your color image. If you want a copy of it then just do
rgbImage = oriImage;
or
oriImage = imread('oriImage.jpg'); % read original image (colored image)
hFig = figure;
subplot(2, 2, 1);
hFig.WindowState = 'maximized';
imshow(oriImage);
title('oriImage', 'FontSize', 16);
drawnow;
grayImage = rgb2gray(oriImage); % convert colored image to grayscale
subplot(2, 2, 2);
imshow(grayImage);
title('grayImage', 'FontSize', 16);
imwrite(grayImage, 'gray.jpg'); % save grayscale image
rgbImage = oriImage;
subplot(2, 2, 3);
imshow(rgbImage);
title('rgbImage', 'FontSize', 16);
halawati cm
el 30 de Mzo. de 2020
Editada: halawati cm
el 30 de Mzo. de 2020
tqvm for your respon. sorry for missing one line of code and make you unclear.
actually after covert rgb image to grayscale, i need to enhance the image using CLAHE technique like this:
oriImage = imread('oriImage.jpg'); % read original image (colored image)
grayImage = rgb2gray(oriImage); % convert RGB image to grayscale
imwrite(grayImage, 'gray.jpg'); % save grayscale image
CLAHEImage = adapthisteq(grayImage); % enhance image using CLAHE
imwrite(CLAHEImage, 'grayCLAHE.jpg');% save CLAHE image
then, how to display the CLAHE image (grayscale) that map color with the original image (RGB color)? so the result could display in RGB color?
Walter Roberson
el 30 de Mzo. de 2020
Editada: Walter Roberson
el 7 de Jul. de 2021
then, how to display the CLAHE image that map color with the original image?
You cannot do that.
Suppose for example you have
M = reshape([0.836299348997876 0 0 0.459932178320044 0 0],[2 1 3]);
image(M)
This is a relatively bright red above a half-intensity green.
rgb2gray(M)
Those are the grayscale intensities; the bright red is brightness .25 and the half-intensity green is brightness 0.27.
Now suppose you do histogram equalization and push the two intensities together marginally, so that they both become 0.26. Now use your hypothetical grayscale to RGB convertor. Both are 0.26 so both have to translate to the same color, but which color? To something that is bright red, or something that is dull green? or perhaps it should be brown?
P = reshape([0.434875661478895 0.22144882659854 0],[1 1 3]);
image(P)
rgb2gray(P)
Brightness information is not enough to go back to color.
Even if your adaptive equalization did not end up changing the values at all, knowing the brightness is not enough to reliably map back to color. You can find the brightness of all of the original colors, but on average 65536 different colors map to the same brightness, so with images of any complexity you will probably have ended up with two different tones that mapped to the same brightness.
halawati cm
el 30 de Mzo. de 2020
oh i see..so i cannot display back to RGB from grayscale image. tqvm for your explaination. you're really helpful!!
Umer Sajid
el 7 de Jul. de 2021
Can you please show me the code to show the rgb image Please
Walter Roberson
el 7 de Jul. de 2021
Which RGB image should be showed?
Umer Sajid
el 7 de Jul. de 2021
Hellow , Hope you are fine. I am looking to convert grayscale image dataset folder to RGB image and then store it into another folder.
My Question is How can i perform this operation Please
Image Analyst
el 8 de Jul. de 2021
@Umer Sajid, use the FAQ:
Inside the loop, create input and output filenames, then use cat() and imwrite():
rgbImage = cat(3, grayImage, grayImage, grayImage);
imwrite(rgbImage, outputFileName);
See full demo attached.
Respuestas (2)
Image Analyst
el 16 de En. de 2018
You can apply various colormaps with ind2rgb():
map = hsv(256); % Or whatever colormap you want.
rgbImage = ind2rgb(im, map); % im is a grayscale or indexed image.
9 comentarios
nazli melek
el 3 de Ag. de 2018
hello thanks for your idea. But when I use this command for a grayscale image all I get is a uni-color sheet for example in black.
Image Analyst
el 3 de Ag. de 2018
Is your grayscale image uint8, or double?
Dinesh PS
el 13 de Nov. de 2018
I'm facing the same problem and my image is double
Image Analyst
el 22 de Mzo. de 2020
Make it uint8 before you call ind2rgb():
image8 = uint8(255 * mat2gray(doubleImage));
map = hsv(256); % Or whatever colormap you want.
rgbImage = ind2rgb(image8, map); % im is a grayscale or indexed image.
KAARMUKILAN S.P.
el 5 de Abr. de 2020
I am getting error on using this code.
I have a gray scale image. and i want to convert it to color image. help me with this.
Image Analyst
el 5 de Abr. de 2020
Editada: Image Analyst
el 5 de Abr. de 2020
That code should work, but unfortunately you forgot to include the error message, and forgot to include your code. Please see the FAQ for how we can help you. Simply saying you got an error without showing us your code, or even the error, does not provide any way for us to help you.
% Create some double-valued image.
doubleImage = 2.3445 * double(imread('cameraman.tif'));
subplot(2, 1, 1);
imshow(doubleImage, []);
% Cast to uint8
image8 = uint8(255 * mat2gray(doubleImage));
map = hsv(256); % Or whatever colormap you want.
rgbImage = ind2rgb(image8, map); % im is a grayscale or indexed image.
subplot(2, 1, 2);
imshow(rgbImage, []);

Hadi Ghahremannezhad
el 8 de Oct. de 2020
Hi,
I tried this code to convert a grayscale image to RGB, but it shows an error like this:
Index in position 2 exceeds array bounds (must not exceed 1).
for the command:
rgbImage = ind2rgb(image8, map);
Walter Roberson
el 8 de Oct. de 2020
you accidentally created a variable named ind2rgb
Image Analyst
el 8 de Oct. de 2020
Set a breakpoint on that line then when it stops there, type this into the command window and tell us what it says
>> which -all ind2rgb
You should see this:
>> which -all ind2rgb
C:\Program Files\MATLAB\R2020b\toolbox\matlab\images\ind2rgb.m
Or for whatever version of MATLAB you're using. You should not see any other lines that indicate that ind2rgb is a variable. If there are, you did something wrong - something to overwrite the built in ind2rgb() function with your own function or variable.
The Anh Vuong
el 1 de Oct. de 2021
Editada: The Anh Vuong
el 1 de Oct. de 2021
Hi,
by using of imges to trained of Network , I have this problem. So I would like to share you a converter of converting automaic gray image to color and working later with im resize function.
Have Fun!
GRAY SCALE TO RGB IMAGE Structure
%filename = "Testimage_gray.PNG" or "Testimage_gray.jpg"
%filename = "Testimage_color.PNG" or "Testimage_color.jpg"
im = imread(filename);
colorscale ="-- color picture"
if(size(im, 3) == 1) % image is grayscale
colorscale ="-- gray picture"
[rgbgray,cmap] = gray2ind(im,256)
im = cat (3,rgbgray,rgbgray, rgbgray)
end
imshow(im)
title(string(filename) + colorscale );
Resize the test image to match the ggogleNET network input size for example [224 224].
I = imresize(im, [224 224]);
imshow(I)
4 comentarios
Walter Roberson
el 1 de Oct. de 2021
Is there a particular reason you used gray2ind(im,256) instead of using im2uint8(im) ?
Is there a particular reason you tested size(im,3) instead of checking ndims(im) ?
The Anh Vuong
el 2 de Oct. de 2021
Editada: Walter Roberson
el 2 de Oct. de 2021
1) No particular reason you could use im2uint8(im)
I have testet , it 's getting the same resulat.
rgbtest = im2uint8(im)
im1 = cat (3,rgbtest,rgbtest,rgbtest)
2) Yes , I make a rize to test if the to coverting "Gray image " is a "color Gray Image", because I need to use with trained Googlenet, which is working with color image [224 224]
Walter Roberson
el 2 de Oct. de 2021
[im, cmap] = imread(filename);
if ~isempty(cmap)
im = ind2rgb(im, cmap);
end
if ndims(im) < 3
im = im(:,:,[1 1 1]);
end
The Anh Vuong
el 2 de Oct. de 2021
Editada: The Anh Vuong
el 2 de Oct. de 2021
I have tested your code. It is working too.
We have now 3 methods to convert gray image to "color gray imager" for processing
Have a fun by programming!
Categorías
Más información sobre Blue en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

