Matlab imagesc displaying nanom values

When using imagesc to render data containing NaN values, Matlab treats them as the minimum value (in gray, it paints them black, blue, etc.). the inkjet card contains neither black nor white. can nano values ​​be displayed as black / white? eg:

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

      

should give blue-green-red-black stripes.

thank

+3


source to share


3 answers


I wrote a custom function to display values NaN

as pass-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

      

This NaN

will display the same color as the background color of the picture. If you delete the line axis image off

, it NaN

will display the same color as the background color of the axis. The function assumes that the input images are in size n x m

(one channel) or n x m x 3

(three channels), so some changes may be required 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

+8


source


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

      



+1


source


I see two possible ways to achieve this:

a) Change the batch jet map so that its minimum value is black or white, for example:

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

      

The disadvantage is that if you use this color map to display data without values nan

, the lowest value will also be displayed in black and white.

b) Use sc from File Exchange, which maps the graph imagesc

to an RGB image instead . It's more flexible because once you have an RGB image, you can manipulate it however you want, including changing all values nan

. eg:.

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)

      

0


source







All Articles