imagesc: how to set NaN as white color

조회 수: 453 (최근 30일)
Dario
Dario 2017년 12월 18일
댓글: Walter Roberson 2021년 10월 8일
I need to set NaN values as white color using imagesc. I attach the matrix which is a 1200x1200 matrix with different values equal to NaN.

답변 (4개)

William Thielicke
William Thielicke 2021년 10월 6일
편집: Walter Roberson 2021년 10월 8일
As this is still the first hit when searching on Google...
The best solution IMHO:
Setting data that is NaN to transparent
set(h, 'AlphaData', ~isnan(img_data))
  댓글 수: 2
jon erickson
jon erickson 2021년 10월 8일
agree - this is a really clever, quick, easy solution that works in vast majority of use-cases.
Walter Roberson
Walter Roberson 2021년 10월 8일
image objects do not (or historically did not) accept logical data, so you had to convert to double,
set(h, 'AlphaData', 1-isnan(img_data))

댓글을 달려면 로그인하십시오.


Adam
Adam 2017년 12월 18일
편집: Adam 2019년 6월 7일
doc colormap
allows you to define whatever colourmap you want. You can tag a white point on the front of e.g. a Parula colourmap of any size you want, e.g.
cmap = [0 0 0; parula(6)];
In order to make sure only NaNs take the white value though you would likely have to do some messing around with caxis. It isn't easy to define a colourmap for many values where you want precisely one to have one specific colour on the colourmap, unless you map it to true RGB, in which case you can do whatever you want.
  댓글 수: 5
Sophia Salazar
Sophia Salazar 2019년 6월 7일
I think you're right. I ended up using jet(200) so that a smaller portion of the cmap is white, I think this works. Thanks!
Adam
Adam 2019년 6월 7일
Your data will be mapped linearly to your colourmap, so the bigger the colourmap the smaller the range of values mapped into each bin. If you have a 256-element colourmap then the bottom 1/256 ( 0.39% ) of values will be mapped to the first bin of the colourmap. Nans will also get mapped here, whatever size your colourmap and range your data.
This is why I said "It isn't easy to define a colourmap for many values where you want precisely one to have one specific colour on the colourmap". If you are dealing with a uint8 image it is quite easy as you can have 1 bin per pixel value and just add an extra bin for the NaNs.
As also mentioned, you can play around with the caxis limits to achieve the right effect, but it all depends on your data.
e.g.
a = rand( 10 );
a(3,4) = NaN;
figure; imagesc( a );
colourmap( [0 0 0; parula(256)] )
If you do only that then quite likely you will have multiple black pixels instead of just 1 (this is random data though so it depends on the particular randon numbers you get. In my run I ended up with 4 black squares)
Now you can try:
caxis( [-0.001 1] )
and some or all of the non-NaN values will now take on a colour from the true colourmap instead of white. But if you have a very small value you may need to go for more like
caxis( [-0.01 1] )
or
caxis( [-0.1 1] )
by trial and error (or it can be mathematically calculated, I just couldn't be bothered)

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2019년 6월 7일
Have you tried this solution:
yourImage(isnan(yourImage)) = 255;

Emmanuel Atoleya Atindama
Emmanuel Atoleya Atindama 2021년 2월 10일
First, you need to know the scale value of the color. Example white = 1, black = 0.
p = data_array(:,:,:);
p(isnan(data_array))=1; % this assigns white to all the nan values, while all other values maintain their original color
figure; imshow(p)
Say you want to assign white to all nan values and black to the known values.
p = zeros(size(data_array));
p(isnan(data_array))=1; % this assigns white to all the nan values, and black to all other values
figure; imshow(p)

카테고리

Help CenterFile Exchange에서 Colormaps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by