Matlab imshow omit NaN

佐手、 提交于 2019-11-26 14:41:37

问题


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.


回答1:


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

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')

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



来源:https://stackoverflow.com/questions/38851267/matlab-imshow-omit-nan

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!