Conversion From Gray scale to RGB colormap

15 views (last 30 days)
Hello, I am trying to convert images from gray scale to RGB. Firstly, I tried it for the jet colormap, but I would like to do this for other possible colormaps which MATLAB has. I imported the 'car1.jpg' image and converted via these lines:
indexedImage = imread('car1.jpg');
Gray_Image= indexedImage(:,:,1)*0.299+indexedImage(:,:,2)*0.587+indexedImage(:,:,3)*0.114;
Then I gave "Gray_Image" variable to my function "gray_to_jet" to convert RGB. Although output seemed like "jet", there are differences between MATLAB's jet colormap. How can I exactly map the values like MATLAB? I am new to image processing, so any help including theoritical appreciated. Here is my function:
function [converted_image,Elapsed_time] = gray_to_jet(varargin)
input_image = varargin{1};
[row_number, column_number] = size(input_image);
C = uint8(255*colormap('jet')); % Convert 0-1 numbers in colormap to 0-255 and uint8.
converted_image = zeros(row_number, column_number, 3);
for idx = 1:row_number
for jdx =1:column_number
colormap_row= double(input_image(idx, jdx) ) + 1;
new_red_pixel = C(colormap_row, 1);
new_green_pixel = C(colormap_row, 2);
new_blue_pixel = C(colormap_row, 3);
converted_image(idx, jdx, 1) = new_red_pixel;
converted_image(idx, jdx, 2) = new_green_pixel;
converted_image(idx, jdx, 3) = new_blue_pixel;
end
end
end

Accepted Answer

Image Analyst
Image Analyst on 11 Apr 2021
Don't so it like that. Simply use ind2rgb()
rgbImage = ind2rgb(indexedImage, cmap);
  2 Comments
Image Analyst
Image Analyst on 11 Apr 2021
Since it's no longer a MATLAB question, you'd be better off asking in a C language question and answer forum.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!