MATLAB: Convert grayscale to RGB

grayscale to colorgrayscale to rgbImage Processing Toolbox

I wish to convert a grayscale image to RGB where I would have color-to-black scale instead of white-to-black scale. Let's say I have a specific color given by [r g b] components. I want the image to present the shades of this color, not the shades of gray. My pO2values will range from 1 to 128, so I use jet(128) to create a gamma of 128 colors from blue all the way to red. Then, I determine what rgb components represent that color. For example, if my pO2value is 60, it will correspond to some green color specified by [r g b] components. Now, I want my grayscale image to be 'greenscale', that is green-to-black gradient instead of original white-to-black gradient.
  • generating r g b is equivalent of me selecting a color pixel using your demo
  • don't see cat() in your script
im=zeros(512,512,3);
im(:,:,1)=I; %where I is 512x512 uint8 grayscale image
im(:,:,2)=I;
im(:,:,3)=I;
% I create a desired color using values from jet spectrum
c = 128;
pO2value=60;
map = jet(c);
pO2value=double(pO2value);
r = ceil(map(pO2value,1)*128);
g = ceil(map(pO2value,2)*128);
b = ceil(map(pO2value,3)*128);
% here's what I got from Image Analyst's demo code
hsv = rgb2hsv(im);
hueImage = hsv(:, :, 1);
selectedPixelRGB = [r g b];
hsvOfThisPixel = rgb2hsv(selectedPixelRGB);
% Make the hue plane of the hsv image this same hue
hsv(:, :, 1) = hsvOfThisPixel(1);
hueImage2 = hsv(:, :, 1);
% Convert back to rgb space.
singleHueRGBImage = hsv2rgb(hsv);
imshow(singleHueRGBImage);
However, I get an error:
??? Attempted to access r(:,2); index out of bounds because size(r)=[1,1,1].
Error in ==> rgb2hsv at 74 g = r(:,2); b = r(:,3); r = r(:,1);
Here is an example of what I am trying to do:

Best Answer

Try it like this if you want to pick some color out of the jet colormap:
workspace; % Make sure the workspace panel is showing.
format longg;
format compact;
fontSize = 20;
% Create a jet colormap
c = 128;
map = jet(c);
pO2value=double(109); % 109 is an arbitrary number out of 1:128
r = ceil(map(pO2value,1)*c)
g = ceil(map(pO2value,2)*c)
b = ceil(map(pO2value,3)*c)
% Now let's do it to a gray scale image.
% Read in a standard MATLAB gray scale demo image.
grayImage = imread('cameraman.tif');
handleToFigure2 = figure;
subplot(2,2,1);
imshow(grayImage);
title('Gray Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Convert the gray image into RGB so we can get it into hsv space.
rgbImage = cat(3, grayImage, grayImage, grayImage);
hsv = rgb2hsv(rgbImage);
% Make the hue plane of the hsv image this same hue
hsvOfThisPixel = rgb2hsv([r g b])
hsv(:, :, 1) = hsvOfThisPixel(1);
% Make the saturation 1 so we'll see color.
% Originally it was zero which meant that the image would be gray.
hsv(:, :, 2) = 1;
% Convert back to rgb space.
singleHueRGBImage = uint8(255*hsv2rgb(hsv));
% Display the new, single hue color image.
subplot(2, 2, 2);
imshow(singleHueRGBImage);
title('New, Single Hue Color Image', 'FontSize', fontSize);
message = sprintf('Done with demo.');
uiwait(msgbox(message));
Related Question