Question

I found that apparently, a mapped version of imshow function does not work in subplot. Is this by design?

The following script

rgb=imread('../FruitSample_small.png');
[ind,map]=rgb2ind(rgb,4);
figure
imshow(ind,map)
figure
subplot(5,1,1);
imshow(ind,map)
subplot(5,1,2);
imshow(ind==0)
subplot(5,1,3);
imshow(ind==1)
subplot(5,1,4);
imshow(ind==2)
subplot(5,1,5);
imshow(ind==3)

produces the following result

enter image description here

i.e. mapped version looks black. If I plot mapped version only (5 times) it looks ok. I.e. subsequent plotting apparently change the palette.

How to plot all these 5 images on same figure then?

Was it helpful?

Solution

Colormap is a property of the figure, not the axis. The second call to imshow resets the Colormap for the entire figure. Here is some more information and a Matlab file exchange solution to the problem. If you download the function described in that link, freezeColors, you can use it in your code like this.

rgb=imread('peppers.png'); % example image included in matlab
[ind,map]=rgb2ind(rgb,4);
figure
imshow(ind,map)

figure
subplot(5,1,1);
imshow(ind,map)
freezeColors % keep colors the same after other subplots are displayed
subplot(5,1,2);
imshow(ind==0)
freezeColors
subplot(5,1,3);
imshow(ind==1)
subplot(5,1,4);
imshow(ind==2)
subplot(5,1,5);
imshow(ind==3)

And the resulting second figure will look like this:

fixed colors subplots example

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top