Mercury
Mercury

Reputation: 1965

Matlab imagesc plotting nan values

When using imagesc to visualize data that contains NaN values, Matlab treats them as the minimum value(in gray it paints them black, jet - blue, etc). the jet colormap doesn't contain neither black nor white. is it possible to display the nan values as black/white? for example:

data = [0 0 0 ; .5 .5 .5 ;1 1 1;nan nan nan];

should yield blue-green-red-black strips.

Thanks

Upvotes: 5

Views: 15117

Answers (4)

Philip Binner
Philip Binner

Reputation: 11

Like Bill Cheatham mentioned, you can use,

set(h, 'AlphaData', ~isnan(img_data(:, :, 1)))

For something more generalisable, I would change this to

  set(h, 'AlphaData', ~isnan(h.CData))

This uses the image handle so that you don't have to worry about indexing your input image.

Upvotes: 1

AY Spunde
AY Spunde

Reputation: 11

function imagescnan(IM)
% function imagescnan(IM)
% -- to display NaNs in imagesc as white/black

% white
nanjet = [ 1,1,1; jet  ];
nanjetLen = length(nanjet); 
pctDataSlotStart = 2/nanjetLen;
pctDataSlotEnd   = 1;
pctCmRange = pctDataSlotEnd - pctDataSlotStart;

dmin = nanmin(IM(:));
dmax = nanmax(IM(:));
dRange = dmax - dmin;   % data range, excluding NaN

cLimRange = dRange / pctCmRange;
cmin = dmin - (pctDataSlotStart * cLimRange);
cmax = dmax;
imagesc(IM);
set(gcf,'colormap',nanjet);
caxis([cmin cmax]);

Upvotes: 1

Bill Cheatham
Bill Cheatham

Reputation: 11937

I wrote a custom function to display NaN values as see-through, i.e. with an alpha value of 0.

function h = imagesc2 ( img_data )
% a wrapper for imagesc, with some formatting going on for nans

% plotting data. Removing and scaling axes (this is for image plotting)
h = imagesc(img_data);
axis image off

% setting alpha values
if ndims( img_data ) == 2
  set(h, 'AlphaData', ~isnan(img_data))
elseif ndims( img_data ) == 3
  set(h, 'AlphaData', ~isnan(img_data(:, :, 1)))
end

if nargout < 1
  clear h
end

The NaNs will thus be displayed the same colour as the figure background colour. If you remove the line axis image off, then the NaNs will be shown the same colour as the axis background colour. The function assumes the input images are of size n x m (single channel) or n x m x 3 (three channels), so may need some modification depending on your use.

With your data:

data = [0 0 0 ; .5 .5 .5 ;1 1 1;nan nan nan];
imagesc2(data);
axis on
set(gca, 'Color', [0, 0, 0])

enter image description here

Upvotes: 14

devrobf
devrobf

Reputation: 7213

I can see two possible ways of achieving this:

a) Modify the jet colormap so that its minimum value is black or white, e.g.:

cmap = jet;
cmap = [ 0 0 0 ; cmap ];  % Add black as the first color

The disadvantage is that if you use this colormap to display data with no nan values, the smallest value will also appear in black/white.

b) Use sc from the File Exchange, which renders an imagesc plot to an RGB image instead. This is more flexible, because once you have the RGB image you can manipulate it however you wish, including changing all the nan values. e.g.:

im = sc(myData);
im(repmat(isnan(myData), [ 1 1 3 ]) ) = 0;  % Set all the nan values in all three 
                                            % color channels to zero (i.e. black)

Upvotes: 0

Related Questions