Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab imshow omit NaN

I am using imshow() to visualize data obtained from the difference of two grayscale images. The images are masked, i.e. each pixel 'laying under' the mask has the value NaN. The data are represented by the parula colormap. The problem is that imshow() treates NaN as zero and therefore the masked pixels are represented as blue. Is there an easy way to omit the masked pixels or to display them in a color that is not part of the colormap (e.g. white, gray, or black)?

I would prefer the solution to base on imshow() since it would be easiest to include into my code. However, solutions using pcolor, imagesc or the like will also be appreciated.

like image 912
Dave Avatar asked Aug 09 '16 12:08

Dave


1 Answers

You can set the AlphaData of the image object to be equal to ~isnan(data) such that NaN's will be shown as transparent values.

R = rand(10);
R(R < 0.25) = NaN;

him = imshow(R, 'InitialMagnification', 10000);
colormap parula
set(him, 'AlphaData', ~isnan(R))

enter image description here

If you want a specific color, you could turn on the axes and set the color of the axes to be whatever color you want the NaN values to be.

axis on;

% Make a red axis
set(gca, 'XColor', 'none', 'yColor', 'none', 'xtick', [], 'ytick', [], 'Color', 'r')

enter image description here

If you use pcolor, then NaN values are already treated as transparent.

like image 77
Suever Avatar answered Sep 18 '22 09:09

Suever