Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
851 views
in Technique[技术] by (71.8m points)

matlab - Contrasting color for NaNs in imagesc

I fill unused elements in a matrix with NaNs, and I would like to assign a contrasting color to those elements with the NaN value when displaying the data using imagesc.

Below is a link to a possible solution, but I don't quite understand it.

http://www.mathworks.com/matlabcentral/newsreader/view_thread/19985

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

NaN values get the first color from the axes colormap, which by default corresponds to the minimum value (other than NaN). You can change the color for minimum value setting axes color limits with CAXIS function. To assign a contrast color to NaN values you can add a special color for NaN values as a first color (1x3 vector).

I take your example and made a function (with some comments):

function [h hcb] = imagescwithnan(a,cm,nanclr)
% IMAGESC with NaNs assigning a specific color to NaNs

%# find minimum and maximum
amin=min(a(:));
amax=max(a(:));
%# size of colormap
n = size(cm,1);
%# color step
dmap=(amax-amin)/n;

%# standard imagesc
him = imagesc(a);
%# add nan color to colormap
colormap([nanclr; cm]);
%# changing color limits
caxis([amin-dmap amax]);
%# place a colorbar
hcb = colorbar;
%# change Y limit for colorbar to avoid showing NaN color
ylim(hcb,[amin amax])

if nargout > 0
    h = him;
end

Here caxis statement assigns the first color of the color map not to the minimum value amin, but to the amin-dmap. So the first color get assigned specifically to NaNs.


Try this function with:

a=peaks;
a(a < 0.5) = nan;
imagescwithnan(a,hot,[0 1 1]) %# [0 1 1] is cyan

test image - NaN color is hidden

If you comment the ylim statement in the function (can be control with additional parameter) this NaN color will be on the colormap.

test image - NaN color is shown on the colorbar


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...