grayscale to RGB in matlab

35 views (last 30 days)
sina baniasadazad
sina baniasadazad on 24 Apr 2021
Edited: DGM on 27 May 2022
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 27 May 2022
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
colormapped1 = ind2rgb(inpict,parula(256));
colormapped2 = ind2rgb(inpict,hot(256));
There are several built-in colormaps, and there are many more on the File Exchange.
The functions joinalpha(), gray2rgb(), imtweak(), lingrad(), colorpict(), gcolorize(), imsize() and imblend() are part of the MIMT:

More Answers (0)

Community Treasure Hunt

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

Start Hunting!