原文:http://blogs.mathworks.com/steve/2006/02/10/all-about-pixel-colors-part-3/
the relevant MATLAB image display function is imagesc.
Suppose, for example, we use a small magic square:
A = magic(5)
Now let's display A using image and a 256-color grayscale colormap:
map = gray(256);
image(A)
colormap(map)
title('Displayed using image')
The displayed image is very dark. That's because the element values of A vary between 1 and 25, so only the first 25 entries of the colormap are being used. All these values are dark.
Compare that with using imagesc:
imagesc(A)
colormap(map)
title('Displayed using imagesc')
You can switch colormaps on the fly, and the values of A will be mapped onto the new colormap.
colormap(jet)
title('Scaled image using jet colormap')
Let's dig into the low-level Handle Graphics properties that are controlling these behaviors. Image objects have a property called
CDataMapping.
close all
h = image(A);
get(h, 'CDataMapping')
close
ans =
direct
You can see that its default value is 'direct'. This means that values of
A are used
directly as indices into the colormap. Compare that with using
imagesc:
h = imagesc(A);
get(h, 'CDataMapping')
close
ans =
scaled
The
imagesc function creates an image whose CDataMapping property is 'scaled'. Values of
A are
scaled to form indices into the colormap. The specific formula is:
C is a value in A, and and
come from the CLim property of the axes object.
h = imagesc(A);
get(gca, 'CLim')
close
It's not a coincidence that the CLim (color limits) vector contains the minimum and maximum values of A. The
imagesc function does that by default. But you can also specify your own color limits using an optional second argument to
imagesc:
imagesc(A, [10 15])
colormap(gray)
title('imagesc(A, [10 15])')