RGB = imread('peppers.png');imshow(RGB)I = rgb2gray(RGB);figure imshow(I)
MATLAB: How to convert gray image to rgb image after filtering
image processingImage Processing Toolbox
Related Solutions
You need to pass in a colormap, N-by-3 array with values between 0 and 1, to to the colormap() function, NOT an image. And you should probably also pass in a handles to the axes you want to apply it to, otherwise it will apply that colormap to every axes in the figure window, which is probably not what you want. So in short, you want:
myColorMap = hsv(256); % Create a colormap.
colormap(gca, myColorMap); % Now apply the colormap.
You can also pass the colormap directly into imshow() and skip calling the colormap() function if you want.
See this full demo and study it:
grayImage = imread('cameraman.tif');subplot(3, 1, 1);imshow(grayImage);axis('on', 'image');title('Original Image');subplot(3, 1, 2);% Create a colormap.myColorMap = hsv(256);% Display image with that colormap.
imshow(grayImage, 'Colormap', myColorMap);colorbar;axis('on', 'image');title('Overlay Image');% Or equivalently, using the colormap() function:
subplot(3, 1, 3);% Display image with no colormap.
imshow(grayImage);% Create a colormap.myColorMap = hsv(256);% Now apply the colormap. Pass in the axes or else it will apply the colormap
% to ALL images on the figure, which is probably not what you want.
colormap(gca, myColorMap);colorbar;axis('on', 'image');title('Overlay Image');
Now, if you already have a colormap, and you want to produce an RGB image, like to write to a file on disk rather than just pseudocoloring a displayed image, you want to call ind2rgb()
rgbImage = ind2rgb(grayImage, myColorMap);
The missing lines are just being subsampled away when you go to display. They're still in the image, just use the magnifying glass to zoom in and you'll see them.
How about this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;format compact;fontSize = 20;image1 = imread('image1.jpg');% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(image1);if numberOfColorChannels > 1 % It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
image1 = rgb2gray(image1); % ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
% grayImage = grayImage(:, :, 2); % Take green channel.
end% This image is supposed to be binary.
image1 = image1 > 128;imshow(image1);hp = impixelinfo();% Display the image.
subplot(2, 2, 1);imshow(image1, []);axis on;hold on;title('Image1', 'FontSize', fontSize, 'Interpreter', 'None');drawnow;hold onimage2 = imread('image2.jpg');% Display the image.subplot(2, 2, 2);imshow(image2, []);hp = impixelinfo();axis on;hold on;title('Image2', 'FontSize', fontSize, 'Interpreter', 'None');drawnow;h3 = subplot(2, 2, 3:4);% Mask the image using bsxfun() function
% maskedRgbImage = bsxfun(@times, image2, cast(image1, 'like', image2));
imshow(image1, []);hp = impixelinfo();hold on;h=imshow(image2);set(h,'Alphadata', 0.6);axis on;theColorMap = jet(256);theColorMap(1,:) = 0; % First row is black.
theColorMap(end,:) = 1; % last row is white.
colormap(h3, theColorMap);colorbar(h3);caxis([0,0.6]);title('Image1 with Image2 in overlay', 'FontSize', fontSize, 'Interpreter', 'None');% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.05, 1, .95]);% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
Best Answer