Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MATLAB: Colors displayed incorrectly?

I have noticed that MATLAB sometimes displays my colors incorrectly. I'm not sure if this is a programming error on my side, or if it is an actual bug in MATLAB. I noticed this behavior with some regularity over the last year or so.

This time, I decided to take a snapshot of a figure with the error in question (taken on MATLAB 2011b on Windows 7, 64 bit):

                                   enter image description here

The code that displays the image in question is the following:

figure;
clf;
cla;
imshow(matrix, []);
colormap(cmap);
set(gca, 'Clim', [0 highest_index]);

where:

  • matrix is of type uint32 (although I have also tried explitly casting matrix as double prior to calling imshow)
  • The values in matrix range between 0 and 900
  • cmap has 901 entries
  • highest_index is 900

The RGB entry for the value 259 in matrix is [1, 0, 0.1] both in the image above and in the colormap array cmap, i.e. cmap(300, :) = [1, 0, 0.1] (notice that the matrix value 259 gets the index 300 in the colormap, since the first entry of the colormap is for the matrix value 0).

Questions:

Why does this happen? Is it an error? Is there anything I am doing wrong?

Update 1:

  1. I tried switching CDataMapping to direct or scaled, but it didn't make a difference.
  2. I also tried using imagesc instead of imshow, but it didn't make a difference.
  3. If I convert the image to RGB first (i.e. transform the indexed image to a true color image; see here for more info on this), i.e. with i_rgb = ind2rgb(i_indexed, cmap), the error goes away and the image is displayed correctly.

    Unfortunately, if I display a true color image the data tip does not reveal the index in the original matrix for each color anymore and instead it just displays the RGB vector (i.e. this is logical, since MATLAB is not aware of the original index anymore).

Update 2:

Here's some sample code:

h_f = figure(1);
clf;
i_spiral = spiral(40);
h_i = image(i_spiral);

% Synthesize a colormap first in HSV and then transform it to RGB:
max_i_spiral = max(i_spiral(:));
m           = max_i_spiral;
h           = (0:m-1)'/max(m,1);
cmap_spiral = hsv2rgb([h ones(m,2)]);  
colormap(cmap_spiral);

% If I comment out the following two lines or use imshow instead of image, 
% it makes no difference (I still get the same error):
set(gca, 'Clim', [1 max_i_spiral]);
set(h_i, 'CDataMapping', 'direct');

The code above results in:

            enter image description here

like image 654
Amelio Vazquez-Reina Avatar asked Oct 14 '11 22:10

Amelio Vazquez-Reina


1 Answers

[Since this answer is totally unrelated to my former answer, I'm not editing the first]

The link you mention (http://www.mathworks.com/help/matlab/creating_plots/image-types.html) says:

Note When using the painters renderer on the Windows platform, you should only use 256 colors when attempting to display an indexed image. Larger colormaps can lead to unexpected colors because the painters algorithm uses the Windows 256 color palette, which graphics drivers and graphics hardware are known to handle differently. To work around this issue, use the Zbuffer or OpenGL renderer, as appropriate. For more information regarding graphics renderers in MATLAB, see Technical Note 1201: The Technical Support Guide to Graphics Rendering and Troubleshooting.

So it seems the problem is that your colormap has more then 256 values. It also explains why the problem goes away if you don't use an indexed image. Try using a different renderer, as suggested in the technical support link from the note:

set(gcf, 'Renderer', 'opengl')

or

set(gcf, 'Renderer', 'Zbuffer')
like image 189
Itamar Katz Avatar answered Oct 01 '22 08:10

Itamar Katz