Ander Biguri
Ander Biguri

Reputation: 35525

Colorbar is not showing the colors I want

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

Upvotes: 2

Views: 1947

Answers (2)

Rody Oldenhuis
Rody Oldenhuis

Reputation: 38052

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

Upvotes: 2

anandr
anandr

Reputation: 1672

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.

Upvotes: 5

Related Questions