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);
colormap(gca, myColorMap);
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);
myColorMap = hsv(256);
imshow(grayImage, 'Colormap', myColorMap);
colorbar;
axis('on', 'image');
title('Overlay Image');
subplot(3, 1, 3);
imshow(grayImage);
myColorMap = hsv(256);
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);
Best Answer