Matlab imshow omits NaN

I use imshow() to visualize data obtained from the difference of two grayscale images. Images are masked, that is, each pixel under the β€œmask” has a value of NaN . Data are presented in the parula color palette. The problem is that imshow() changes NaN as zero, and therefore masked pixels appear blue. Is there an easy way to omit masked pixels or display them in a color that is not part of the color map (e.g. white, gray, or black)?

I would prefer the solution to be based on imshow() , as it would be easier to include in my code. However, decisions will also be made using pcolor , imagesc or the like.

+3
source share
1 answer

You can set the AlphaData of the image object to ~isnan(data) so that NaN displays 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 need a specific color, you can turn on the axes and set the color of the axes like any color that should have NaN values.

 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 considered transparent.

+8
source

All Articles