2
votes

I have a similar question than the one in this post.

I have a grayscale image and I plot points on it. Fro plotting the points I use colormap('jet') but as I want the image to be grayscale, after plotting the points I reset the colormap, colormap('gray').

But I want to show a colorbar! And the colorbar is plotted in grayscale, not 'jet'. How can I do that?

EDIT: I want a Colorbar showing the color of the points!

enter image description here

2
so you want a jet colorbar in an image using the gray colormap??? - Dan
@Dan , the points plotted over the grayscale image are In jet, but the image per se not. So, how can I use two colormaps in an image, and use a colorbar for the map I want? - Ander Biguri
So, just to clarify: you have a greyscale image, onto which you plot colored points. You want the colorbar to show the colors of the points, while keeping the (background) image greyscale. Is this correct? - Rody Oldenhuis
@RodyOldenhuis Exactly. see the Image I just added. - Ander Biguri
Can we see your code as well? - Dan

2 Answers

5
votes

You should convert your image to RGB by putting the same data into R-, G-, and B-channels (this will be grayscale RGB image). Colormap in MatLab is not applied to RGB images, only to indexed ones. Then plot your points over the image with colormap you like.

2
votes

As discussed here, there's a few ways:

  1. If you have the image processing toolbox, use subimage to create an independent image with a separate colormap. Then plot the image, your points, and join them into one using linkaxes.

  2. Use freezeColors from the file exchange (or multiple colormaps, which I haven't ever tested personally). This is a very easy way to create a larger colormap, and automatically selecting the right portion of the colormap for display of images and colorbars.

  3. As answered by anandr, convert your greyscale image to RGB; Matlab doesn't use colormaps on RGB images, which leaves you freedom to plot your points and show their colorbar independent of the image.

Example code for (3):

I = imread('cameraman.tif');
imshow(cat(3,I,I,I))

hold on

x = @() round(size(I,1) * rand(50,1));
y = @() round(size(I,2) * rand(50,1));

plot(x(), y(), 'r.')
plot(x(), y(), 'g.')
plot(x(), y(), 'b.')

colormap('jet')
colorbar

result:

pic w/ points & colorbar