how to convert a gray scale image to color image using matlab

176 views (last 30 days)
Hi all, Is there any possibility to convert ab gray scale image into color image.If so, kindly suggest me.
Thanks in advance

Answers (5)

Jan
Jan on 11 Sep 2013
It depends on how the converted image should look like, so please explain any details. Examples:
Gray = rand(200, 200);
RGB1 = cat(3, Gray, Gray, Gray); % information stored in intensity
RGB2 = Gray;
RGB2(end, end, 3) = 0; % All information in red channel
GrayIndex = uint8(floor(Gray * 255));
Map = jet(255);
RGB3 = ind2rgb(GrayIndex, Map);
And there are many many other possible solutions.
  5 Comments
Jule
Jule on 2 Apr 2023
Edited: Jule on 2 Apr 2023
tryin' to convert my unit8 image to green channel or blue channel, respectivel.
What should I change in:
RGB2 = Gray;
RGB2(end, end, 3) = 0; % All information in red channel
? Thnx
Image Analyst
Image Analyst on 2 Apr 2023
@Jule to split up into channels, you can use
[r, g, b] = imsplit(rgbImage);
r, g, and b will be gray scale versions of each color channel.
If you want to keep it as RGB you can blacken specific channels in place, like
% Make it look red by blackening the green and blue channels.
rgbCopy = rgbImage;
rgbCopy(:, :, 2) = 0; % Erase green channel.
rgbCopy(:, :, 3) = 0; % Erase blue channel.
% Make it look green by blackening the red and blue channels.
rgbCopy = rgbImage;
rgbCopy(:, :, 1) = 0; % Erase red channel.
rgbCopy(:, :, 3) = 0; % Erase blue channel.
% Make it look blue by blackening the red and green channels.
rgbCopy = rgbImage;
rgbCopy(:, :, 1) = 0; % Erase red channel.
rgbCopy(:, :, 2) = 0; % Erase green channel.
You can also use cat(3,...):
[r, g, b] = imsplit(rgbImage);
% Create an image of all zeros
blackImage = zeros(size(r, 1), size(r, 2), class(rgbImage));
% Make an RGB image look red by combining
% the red channel with two black channels.
rgbCopy = cat(3, r, blackImage, blackImage);
% Make an RGB image look green by combining
% the green channel with two black channels.
rgbCopy = cat(3, blackImage, g, blackImage);
% Make an RGB image look blue by combining
% the blue channel with two black channels.
rgbCopy = cat(3, blackImage, blackImage, b);

Sign in to comment.


sidra
sidra on 11 Sep 2013
Nirmala , try the following , its a very simple and efficient code to convert grayscale images to RGB using a specified colormap.
  4 Comments
Youssef  Khmou
Youssef Khmou on 12 Sep 2013
Edited: Youssef Khmou on 12 Sep 2013
So how they did retrieve old Black and White movies , made them Colored like they were recorded in RGB?
Image Analyst
Image Analyst on 12 Sep 2013
I believe I heard that most of that was done "by hand" - by artists - with some help from digital computers to continue colors onto the next frame. See https://en.wikipedia.org/wiki/Colorizing

Sign in to comment.


DGM
DGM on 2 May 2022
Edited: DGM on 2 May 2022
To answer the original intent of the question, there is no trivial way to return a grayscale image back to its original color. That information is gone. Your options are:
  • Go back to the source image before it was converted to grayscale
  • Manually colorize it by some means
  • Train an AI to guess what color a banana is
Only one of those options will get you back to the original image.
Otherwise, consider the conspicuously low-effort example of manual colorization:
A = imread('cameraman.tif');
B = imread('cmancolorize.png');
% i bet you didn't know cameraman was a smurf in a salmon shirt
Bycc = rgb2ycbcr(B);
Bycc(:,:,1) = 219*(im2double(A).^0.8) + 16;
C = ycbcr2rgb(Bycc);
imshow(C)
Honestly, I think it should be obvious that color information has been permanently lost. So I have to assume that the hundreds of people looking at this question every month are surely thinking of a less-than-obvious interpretation of the question.
With that in mind, there are a number of ways to add color information to a grayscale image. The following link covers a handful of ways, including simple colorization methods and basic application of colormaps.

Tallha Akram
Tallha Akram on 11 Sep 2013
[X,map] = imread('trees.tif');
gmap = rgb2gray(map);
figure, imshow(X,map), figure, imshow(X,gmap);
figure; imshow(X,gmap); colormap(map);
  10 Comments
Image Analyst
Image Analyst on 31 Oct 2016
Ask FLIR how to import the temperature matrix into MATLAB. I'm sure they must have code for doing that. Now you will have a matrix that has a spatial record of what temperature is where in your image, assuming you inputted the correct emissivity value. Ask FLIR because they might know what an accurate emissivity is for the type of plants you're looking at.
If you meant how to measure temperature directly, then look into thermocouples that interface with MATLAB. This will give you a "second opinion" on the temperature of the plants that you can check your camera against. Perhaps you can then adjust the emissivity of the camera until its values match the thermocouple's values.

Sign in to comment.


David Mills
David Mills on 22 Feb 2017

Community Treasure Hunt

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

Start Hunting!