imagesc make all zeros white?

I have the following code. The output is a matrix and I want all of the zeros to be displayed as white. How can I do this?
figure (1)
imagesc(testNSE);
title ('Model vs Observations All NSE')
caxis ([0,1])
colormap(jet)
colorbar
Thank you in advance for any help

 Accepted Answer

Image Analyst
Image Analyst on 25 Sep 2015
Edited: Image Analyst on 25 Sep 2015
Set the first row of the colormap, which will represent 0 since you set up caxis to go from 0 to 1, to be [1,1,1];
myColorMap = jet(256);
myColorMap(1,:) = 1;
colormap(myColorMap);
colorbar

13 Comments

Thanks, I have tried your suggestion but now get a new error which I am not familiar with,
figure(1)
imagesc(testNSE);
title ('Model vs Observations All NSE')
caxis([0, 1])
myColorMap = get(256);
myColorMap(1,:) = 1;
colormap(myColorMap);
colorbar
Error using handle.handle/get
Invalid or deleted object.
Try getting rid of caxis() - you don't need it since imagesc() scales it. Also change get to jet:
% Create sample data:
testNSE = randi(255, 240, 320);
% Other code
figure(1)
imagesc(testNSE);
title ('Model vs Observations All NSE')
% caxis([0, 1])
myColorMap = jet(256);
myColorMap(1,:) = 1;
colormap(myColorMap);
colorbar
All sorted, thanks for your help!
Image Analyst
Image Analyst on 25 Sep 2015
Edited: Image Analyst on 8 Mar 2018
You're welcome.
I have just realised that I actually need a slight modification. I want all zeros to be grey, and all values <0 to be white.
I can change everything <=0 to grey by changing one small part of the code
myColorMap(1,:) = .8;
But obviously this doesn't differentiate the zeros and values less than zero. Can you suggest anything? Apologies for the additional question
This works really well, thanks Image Analyst.
myColorMap(1,:) = 1;
I was just wondering what exactly does the (1,:) correspond to here? I edited this to be myColorMap(1:50,:) = 1; so everything with counts < 50 would be white in the attached image, but this does not line up with the 50 in the colorbar?
The (1,:) means "row 1, all columns elements". So I'm setting the first row of the colormap to be all 1's. 1 is the max so r=1, g=1, and b=1. Since all of red, green, and blue are 1, that is the definition of white. The first row corresponds to the darkest data of what is displayed. So doing this will set the darkest pixels (pixels with value of 0 for Robert, the original poster) to white, which is what he wanted.
For recent versions of MATLAB, if you have a GUI with multiple axes on the figure window, you will have to pass the axes handle into colormap(). I don't believe it will operate on the last axes you used like old versions.
cmap(1,:) = .999;
Gave me clear white for me.
Thank you Image Analyst for your answers related to Matlab.
Whenever I search for any doubt, I see you have already answered similar queries somewhere!!
Is there a way to change the 0 pixels to white without changing the colormap? Like if X=0 then white?
I am trying to set my zeroes to white in a series of images but the axis values change for each (-3 to 400, -200 to 40 etc... ) i would like to be able to apply the zeros as white in my code rather than using the edit colormap option on the figure.
@I.H How many rows do you have in your colormap? What class is X? (double, uint8, uint16??) What range shows up as white for you now? You could try
X(X==0) = xOfWhite;
So let's say that right now, a value of 1000 is white. Then the above code would set all values of X that are zero to 1000 and would thus appear as white with the existing colormap.
Have you tried the caxis() function to make end end values of your colormap consistent?
X is a 5751x9951 double. What i have done so far is :
my_color_map = jet(256);
my_color_map(128,:) = 1;
my_color_map(129,:) = 1;
This works when i set my caxis limits to be symetrical but if i change the limits to be non symetrical then the white will be offset.
I'm guessing that what i want to do might not be possible. I was hoping there was a command such as
0 = setcolor(1);
@I.H, if you set a different top and bottom value with caxis(), you'll have to find out where the 0 value lies. So if you have the top value be 1000, and the bottom value be -200, then you can find out where zero is by fitting a line and evaluating it at zero.
coefficients = polyfit([-200, 1000], [1, 256], 1);
% y = coefficients(2) * x + coefficients(1)
zeroIndexRow = round(coefficients(1)); % Evaluate at x = 0 to get the row of the colormap
my_color_map(zeroIndexRow, :) = 1; % Set this row to all 1's to get white.
Okay, thank you very much

Sign in to comment.

More Answers (0)

Products

Tags

Asked:

on 25 Sep 2015

Commented:

I.H
on 2 Jun 2021

Community Treasure Hunt

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

Start Hunting!