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

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)

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

Hi Jan Simon, Thanks for your reply ... But I want to convert to a RGB image into grayscale image.and i want to get back the original image. How should I get back the original image from the grayscale image. For example, A=imread('bird.png'); B=rgb2gray(A);
Now i want to get back the the image A from B.
This is not possible. converting an RGB image to grayscale deletes information and there is no way to obtain these information back again.
What happened to A? Why is it no longer available in your code? Did you use "clear" to get rid of it? If so, just don't do that and you'll have the original A.
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
@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.

Nirmala , try the following , its a very simple and efficient code to convert grayscale images to RGB using a specified colormap.

4 Comments

It will be color, but not the original color of course.
@Image Analyst: Ya not the original color, that's impossible , but using a specified colormap.
@Image Analyst / Jan Simon : I have posted a queston about principle component and closest axis. It would be really great if you would help me out.
So how they did retrieve old Black and White movies , made them Colored like they were recorded in RGB?
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.

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.
[X,map] = imread('trees.tif');
gmap = rgb2gray(map);
figure, imshow(X,map), figure, imshow(X,gmap);
figure; imshow(X,gmap); colormap(map);

10 Comments

Hi, I tried your code. Am getting the following errors.What i have to do? ??? Error using ==> rgb2gray>parse_inputs at 82 MAP must be a m x 3 array.
Error in ==> rgb2gray at 35 X = parse_inputs(varargin{:});
Error in ==> gray2col at 17 gmap = rgb2gray(map);
which matlab version you are using. Code is working fine with me.
Nirmala: Don't worry about it - that code (from the help for rgb2gray) does not turn a grayscale image into a color image as you requested. It displays an indexed image in color, and then displays the indexed image in grayscale. No color image is produced and stored in a variable like you wanted. (Actually no image variable of any type is created.)
Hi. Do you guys know how to convert Grayscale image into temperature in Matlab? Its the grayscale feature of thermal image. Please explain the details. Thanks a lot.
You need to have a mapping between greyscale level and temperature. The mapping will not necessarily be linear.
IR images are often shown as greyscale, and generally brighter means hotter. But you have to worry about band for IR because we cannot assume blackbody radiation from thermal equilibrium for a number of situations of interest. For example in near infrared, different biological molecules vibrate at different frequencies and have preferred radiation wavelengths, so bright in one IR image might reflect compounds that radiate on the sensitive frequency rather than continuous spectra due to heat
Thanks for answering. Really appreciate that. What we're going to do is to capture the thermal image of a plant. Can you give me some recommendations on how can measure the temperature of the plant itself?By using Matlab? What exactly are the considerations and parameters involved? Can you help me?. Thank you so much. :)
The appearance of vegetation is pretty sensitive to which IR band (and filtering) is being used, so you cannot assume that brightness is due entirely to temperature differences.
"The pigment in plant leaves, chlorophyll, strongly absorbs visible light (from 0.4 to 0.7 µm) for use in photosynthesis. The cell structure of the leaves, on the other hand, strongly reflects near-infrared light (from 0.7 to 1.1 µm). The more leaves a plant has, the more these wavelengths of light are affected, respectively."
So for that particular range of IR, "bright" can just mean that it has a lot of leaves reflecting ambient infrared preferably. See also http://www.nconemap.com/Portals/7/documents/Using_Color_Infrared_Imagery_20110810.pdf for more on how that property is used.
The situation is different if you measure in the far infrared; see https://en.wikipedia.org/wiki/Thermal_radiation
And notice, "The total amount of radiation of all frequencies increases steeply as the temperature rises; it grows as T^4, where T is the absolute temperature of the body."
Therefore if you had a way of measuring the total amount of radiation on all frequencies then you could take the fourth root to determine the proportional temperature. But you do not have a way to get that total radiation, and at the moment you do not know whether your sensors are linear in measuring that T^4.
So what do you need? Well, you can look for the frequency of peak radiation in the far infrared, under a black-body assumption. But to study peak frequency your equipment needs to be able to measure at multiple frequencies. If you have a greyscale image, chances are high that it is being measured over a band of frequencies in which all that can be registered is the total energy over that band, without being able to distinguish where the peak frequency was.
You need to find out a lot more about your available equipment.
Hi we're gonna use the Flir one camera for iOs
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.

Categories

Asked:

on 11 Sep 2013

Commented:

on 2 Apr 2023

Community Treasure Hunt

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

Start Hunting!