フィルターのクリア

How to convert values of 0 to NaN

16 ビュー (過去 30 日間)
Eric
Eric 2023 年 12 月 5 日
コメント済み: Walter Roberson 2023 年 12 月 8 日
When I plot my graphs using imagesc, how do I conver the values of 0 to NaN?
Currently, the colorbar displays 10^0 as the lowest, but if the value is 0, it still displays this blue color. I want the graph to display white if the value is equal to 0.

回答 (2 件)

Matt J
Matt J 2023 年 12 月 5 日
編集済み: Matt J 2023 年 12 月 5 日
NaNs will not appear bright on an image display. You would have to replace them with a large value.
Image(Image==0)=brightValue;

Walter Roberson
Walter Roberson 2023 年 12 月 5 日
編集済み: Walter Roberson 2023 年 12 月 5 日
Leave them the way they are now, but set the axes color to white. Tell imagesc() tol make NaN values transparent, so those locations would show the white background underneath.
im = imread('cameraman.tif');
figure
imagesc(im);
colormap(gray);
title('original');
nim = im2double(im);
nim(randi(numel(nim), 1, 3000)) = nan;
figure
imagesc(nim, 'alphadata', 0+~isnan(nim));
colormap(gray)
set(gca, 'color', 'r');
title('nan shows color underneath')
  4 件のコメント
Eric
Eric 2023 年 12 月 8 日
Is 0+~isnan(nim) what converts the 0 squares to NaN?
Walter Roberson
Walter Roberson 2023 年 12 月 8 日
Your original question involved having data that has NaN inside it.
isnan(im) would respond with a logical array that is true (==1) for all NaN values and false (== 0) for non-nan values.
~isnan(im) would then be a logical array that is false (== 0) for all NaN values and true (== 1) for non-nan values.
0+~isnan(im) leaves the numeric values the same but converts to double precision. So you would have 0 (double) for all NaN values and 1 (double) for all non-NaN values.
Those 0 and 1 (double) are used as AlphaData values. Alpha values of 1 (in this case, meaning was not NaN) tell image() objects to be fully opaque -- so the color implied by the (non-NaN) data will be used at that location. And alpha values of 0 (in this case meaning the data was NaN) tell image() objects to make the pixel fully transparent, allowing whatever is underneath to be seen through at that location.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeImages についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by