grayscale to RGB in matlab

26 views (last 30 days)
sina baniasadazad
sina baniasadazad on 24 Apr 2021
Edited: DGM on 29 Dec 2023
How do I change from grayscale to RGB in matlab?
  1 Comment
Clayton Gotberg
Clayton Gotberg on 24 Apr 2021
Do you just need to change the format of the image or do you need to colorize the image?

Sign in to comment.

Accepted Answer

DGM
DGM on 24 Apr 2021
Edited: DGM on 29 Dec 2023
It depends what you mean when you say you want an RGB image. Consider the following image:
You can make a single-channel gray image into a multichannel gray image:
% this is a 256x256x1 grayscale image
inpict = imread('cameraman.tif');
% this is a 256x256x3 grayscale image
gray3pict = repmat(inpict,[1 1 3]);
The same can also be done using MIMT gray2rgb(), with the benefit of also handling images with alpha content.
% this is a 256x256x2 (IA) grayscale image
[ipict,~,alpha] = imread('cameramanwithalpha.png');
iapict = joinalpha(ipict,alpha);
% this is a 256x256x4 (RGBA) grayscale image
rgbapict = gray2rgb(iapict);
IA RGBA
You can also do simple colorization by filling/zeroing channels in a similar fashion:
% it's possible to make simple primary or secondary color copies like so
zeropict = zeros(size(inpict),class(inpict));
redpict = cat(3,inpict,zeropict,zeropict);
magentapict = cat(3,inpict,zeropict,inpict);
% it's also possible to fill channels with constant values other than zero
onespict = ones(size(inpict),class(inpict));
redbluepict = cat(3,inpict,zeropict,onespict*100);
% similarly, colors other than primary/secondary colors
% can be obtained by adjusting the weights of the channels
aquapict = cat(3,0.2*inpict,0.7*inpict,inpict);
Adjusting the hue is another simple way to do the same thing.
% it's also possible to just adjust the color continously using some color model
adjustedpict = imtweak(redpict,'hsl',[-0.08,1,1]);
There are other ways to colorize that may do a better job of retaining contrast. MIMT has a very basic HSL colorization tool.
% it's possible to colorize the image using a simple purpose-built tool
gcolorized = gcolorize(repmat(inpict,[1 1 3]),[-80 100 0]);
Very similar things can be done using simple image blending techniques.
% or the same can be done using image blending
cpict = colorpict([imsize(inpict,2) 3],[150 50 255],'uint8');
blendcolorized = imblend(inpict,cpict,1,'lightness');
If you want the color to vary over the image, you can create a colored overlay (or underlay) and combine them using image blending techniques as well.
% it's also possible to colorize using some form of image blending
gradpict = lingrad([256 256 3],[0 1; 1 0],[0 0 1; 1 0 0]*255,'cosine','uint8');
blendpict = imblend(gradpict,inpict,1,'overlay');
You could also simply treat the grayscale values as indices into any arbitrary colormap.
% basic colormapping
% this simplified example only works because inpict is uint8
% and the length of the colormap corresponds to its nominal range
colormapped1 = ind2rgb(inpict,parula(256));
colormapped2 = ind2rgb(inpict,hot(256));
However, that only works so simply if your image is an integer-valued image whose values correspond to the indices of your selected colormap. If your image values are normalized or on some arbitrary scale, or if your colormap length doesn't correspond to the number of gray levels in your image, you'll need to address that before using ind2rgb().
% let's say your image is on some arbitrary scale
% or that its dynamic range does not correspond
% to the length of your favorite colormap
arbitrarypict = rescale(inpict,-29,537); % image is non-integer data between [-29 537]
% rescale and quantize the image to match the map length
maplength = 64;
myCT = magma(maplength); % not a built-in map (see FEX #62729)
colormapped3 = mat2gray(arbitrarypict); % normalize to data extrema
colormapped3 = gray2ind(colormapped3,maplength); % quantize to map length
colormapped3 = ind2rgb(colormapped3,myCT); % apply colormap
Of course, if you have MIMT, you can do the same thing in one line using gray2pcolor()
% default normalizes WRT data extrema, but that's configurable
colormapped4 = gray2pcolor(arbitrarypict,myCT); % default quant mimics gray2ind()
... and it can even mimic the quantization used by imagesc()/pcolor(), which is something you can't get using gray2ind().
colormapped4 = gray2pcolor(arbitrarypict,myCT,'cdscale'); % mimic imagesc() quant
it can even emulate imagesc() right down to the requantization involved in on-screen rendering:
colormapped4 = gray2pcolor(arbitrarypict,myCT,'cdscale_display'); % display emulation
There are several built-in colormaps, and there are many more on the File Exchange.
The functions joinalpha(), gray2rgb(), gray2pcolor(), imtweak(), lingrad(), colorpict(), gcolorize(), imsize() and imblend() are part of the MIMT:

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!