1
votes

I need to display a true color image, got with imread and shown with imshow, along with a (apparently) unrelated colorbar. I'm calling:

figure
imshow(my_image)
colorbar
colormap(cool(200));
caxis([0 15]);

Using a test image, it looks like this:

enter image description here

As you can see caxis has no effect on the colorbar. Colorbar max value will stay 200, which is the number of rows in my colormap. That's properly described by the documentation of caxis, that says:

It does not affect surfaces, patches, or images with true color CData or with CDataMapping set to direct.

Is there any workaround? I tried some weird stuff like subplotting the image on one side and the colorbar on the other (but I get a blank plot on this second side, and so much blank space out); I tried plotting zeros, then colorbar and caxis, then replacing the zeros plot with imshow (but colorbar gets refreshed accordingly). Any help?

1

1 Answers

0
votes

This is a normal behavior, as you noticed in the documentation.

True color images have three channels, and a colormap only one channel ... so what you want to do is similar to mapping 3d data on a single axis. There is an indetermination and Matlab doesn't know how to handle this, so it simply does not handle it at all.

The only workaround I see is to reduce your data to a single channel, for instance with rgb2gray. Colormaps work great with grayscale images.

Edit

If you want to keep the image in true color and an unrelated colormap, there are a few solutions.

Please note that on my computer (R2015a, Ubuntu) the code you posted works, i.e. the colorbar axis is mapped to [0 15] after caxis([0 15]). Maybe an upgrade to a more recent release would fix your problem.

This being said, as you say that it does not work with your version, you can still modify the labels of the colorbar ticks to make them look like what you want:

hcb = colorbar;
set(hcb,'YTick',linspace(1,200,4));
set(hcb,'YTickLabel',linspace(0,15,4));

... but this is both a very ugly workaround and a quite dangerous practice.

Best,