MATLAB: Are colors displayed incorrectly?

I noticed that MATLAB sometimes displays my colors incorrectly. I am not sure if this is a programming error on my side, or if it is a real mistake in MATLAB. I noticed this behavior with some regularity over the last year or so.

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

                                    enter image description here

The code displaying the image in question is as follows:

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

Where:

  • matrixhas a type uint32(although I also tried to explicitly distinguish matrixas doublebefore the call imshow)
  • Values ​​in matrixare between 0and900
  • cmaphas 901records
  • highest_index 900

RGB 259 matrix [1, 0, 0.1] , colormap cmap, cmap(300, :) = [1, 0, 0.1] ( , 259 300 , 0).

:

? ? -, ?

1:

  • CDataMapping direct scaled, .
  • imagesc imshow, .
  • RGB (.. indexed image true color; . ), i_rgb = ind2rgb(i_indexed, cmap), , ​​ .

    , true color, , RGB (.. , MATLAB ).

2:

:

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');

:

            enter image description here

+5
3

[ , ]

, (http://www.mathworks.com/help/matlab/creating_plots/image-types.html) :

Windows 256 . , Windows 256, , , -. , , Zbuffer OpenGL. MATLAB . 1201: .

, , 256 . , , . , :

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

set(gcf, 'Renderer', 'Zbuffer')
+8

IMSHOW:

imshow(img,map)

, :

%# indexed image
I = spiral(40);

%# Synthesize a colormap first in HSV and then transform it to RGB
mx = max(I(:));
cmap = hsv2rgb([(0:mx-1)'./max(mx,1) ones(mx,2)]);         %'

%# show image
imshow(I,cmap)
colorbar
datacursormode on

screenshot


EDIT:

@ItamarKatz, , Windows, 256 , "", .

IMSHOW ( IMAGE ), .

IMAGE/IMAGESC, :

  • : [1 length(cmap)]
  • uint8/uint16: [0 255] uint8 [0 65535] uint16, .

, ( 0 1), .

, , IMAGE ( , uint16):

%# indexed image and colormap
I = spiral(40);
cmap = hsv( max(I(:)) );

%# show indexed image (double)
hFig = figure(2);
hImg = image(I);                          %# one-based index into colormap
colormap(cmap), colorbar
axis off image

%# fix bug on Windows with indexed image of more than 256 colors
if ispc && strcmpi(get(hImg,'CDataMapping'),'direct') && size(cmap,1) > 256
    set(hFig, 'Renderer','zbuffer')       %# opengl renderer also works
end

%# show indexed image (uint16)
hFig = figure(3);
hImg = image( uint16(I-1) );              %# zero-based index into colormap
colormap(cmap), colorbar
axis off image

%# fix bug on Windows with indexed image of more than 256 colors
if ispc && strcmpi(get(hImg,'CDataMapping'),'direct') && size(cmap,1) > 256
    set(hFig, 'Renderer','zbuffer')
end
+3

100% ( ), , /, . , , Edit Text Update Function... - :

function output_txt = dataCursorCallback(obj,event_obj)
% Display the position of the data cursor, and the RGB data to 6 decimal places.

pos = get(event_obj,'Position');
output_txt = {['X: ',num2str(pos(1),4)],...
    ['Y: ',num2str(pos(2),4)]};

h = get(event_obj,'target');
cdata = get (h, 'CData');
cmap = colormap;
rgb = cmap(cdata(pos(2),pos(1)),:);
output_txt{end+1} = ['RGB: ' num2str(rgb,'%.6f')];

, , - . , save and close, , Select Text Update Function...

+1

All Articles