Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

how to converty gray image to color image?

Asked by Sivakumaran Chandrasekaran on 25 Sep 2012

how to converty gray image to color image?


Sivakumaran Chandrasekaran


1 Answer

Answer by Image Analyst on 25 Sep 2012

Depending on what you want the gray to be....

grayImage = rgb2gray(rgbImage);


grayImage = rgbImage(:, :, 1); % Extract red channel.
grayImage = rgbImage(:, :, 2); % Extract green channel.
grayImage = rgbImage(:, :, 3); % Extract blue channel.


Matt Kindig on 25 Sep 2012

There is no general solution to this problem, as you lose color information when converting from color to gray in the first place. That is, there is no unique RGB triplet (e.g. [123 120 255]) that corresponds to a given gray level (e.g. [70 70 70]).

You can apply a false-color rendering of the image using a colormap, but keep in mind that this will not accurately render the colors of the original object.

Ryan on 25 Sep 2012

To build on Matt's answer, here is a reference for colormaps in Matlab:

Image Analyst on 25 Sep 2012

Sorry. I didn't notice, since this is much much less common than the other way. You can use cat() or ind2rgb():

rgbImage = cat(3, grayImage, grayImage, grayImage);
rgbImage = ind2rgb(grayImage, cmap); % e.g. cmap = gray(256)
Image Analyst

Contact us