How can I scale a grayscale image to efficiently use a colormap?

6 views (last 30 days)
If there are outliers in an image, MATLAB by default may not make efficient use of the colors available in a colormap. I would like to learn some strategies for identifying and tackling this issue.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jan 2017
The issue can happen when there are outliers in an indexed image. The "imagesc" function uses the minimum value  and the maximum value in the image as the default limits of the "color axes", with all the colors of the colormap being used to represent values between these limits. If the minimum or maximum value is an outlier, the image will be displayed with lower contrast because most of the colors from the colormap will be underutilized, while a few colors will be used to represent the majority of the data.
 
% the following is an example image that ships with MATLAB
close all
load durer.mat
 
% the following image is scaled the way that it should be. there are no outliers
figure(1)
imagesc(X)
colormap(pink)
colorbar
 
figure(2)
% plotting the histogram
nbin=max(X(:))-min(X(:));
[hst,pos]=hist(X(:),nbin);
% notice how all the colors are close to evenly distributed along the X-axis? this is making efficient use of the colormap
bar(pos,hst);
 
% add an outlier to the data above
Y=X;
Y(2,2)=5000;
figure(3)
imagesc(Y)
colormap(pink);
colorbar
 
figure(4)
[hst,pos]=hist(Y(:),nbin);
% notice how all the colors between 128 and 5000 are being wasted, ans so all the pixels have to be crowed in to the colors just at the bottom. this may be what is happening with you.
bar(pos,hst);
The solution to this problem is to discard or ignore the outliers. One way to do that is to visually inspect the histograms generated by the code above and find a color interval where most of the image data is located. You can then use the "caxis" function to limit the color mapping to this interval. For example, the image in figure 3 is badly scaled. I know from looking at the histogram that the maximum interesting data is at 127, even though there is an outlier at 5000. I could therefore use the following code:
 
figure(3)
caxis([0, 127]);
This will resolve the scaling issue. Note that it may be useful to explore automated ways to scale your data. You can use statistical methods to identify the range where most of the image data is located. You can also look at the following functions for this:

More Answers (0)

Categories

Find more on Colormaps in Help Center and File Exchange

Products


Release

R2008b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!