I'm using imagesc to display data in grayscale. I want to be able to set the color for one particular value to red. How do I accomplish this?

205 views (last 30 days)
I have a 2-d matrix of type double. I want to display the data while retaining the values in the image. The image is to be displayed in gray scale, except for a particular value which I'd like to show up in red. This value is not fixed and can be changed later. I want to use imagesc for this (and change the limits, i.e. imagesc(data,[min max]),colormap('gray') and I only want those pixels with the exact value to be red. Is there a way to do this?

Answers (1)

Image Analyst
Image Analyst on 22 Sep 2016
Try using a colormap
cmap = gray(256);
% Set one gray level, 8, to red.
% Remember, row #1 is 0 gray levels.
% so row #9 is 8 gray levels.
cmap(9,:) = [1,0,0];
colormap(cmap); % Apply the custom colormap.
colorbar;
  4 Comments
Jason
Jason on 22 Sep 2016
Oh I'm sorry, I should have included another piece of information. The matrix I want to display can be 128x128 and contain more than 2^14 unique data values. The values are real numbers with many digits to the right of the decimal. Since the gray map you have in your example only contains 256 levels, the likely hood that gray level 8 is the exact value I'm looking for is rare (unless I'm misunderstanding something).
With that in mind, here's a code snippet
a = -10;
b = 10;
data = (b-a).*rand(128,128) + a;
desiredValue = data(33,33); % picking a value to modify to red
I only want values that are equal to desiredValue to show in red.
I thought if I made a custom colormap of the size of the unique values in data, i.e. if there are 16284 unique values, do
uData = unique(data);
cmap = gray(numel(uData);
idx = find(uData == desiredValue);
cmap(idx) = [1, 0, 0];
imagesc(data),colormap(cmap),colorbar
would do what I want, but it didn't highlight in red the pixel at loc 33,33
I hope this makes it better to understand what I'm trying to do.
Image Analyst
Image Analyst on 22 Sep 2016
If you want 8 in a floating point image, it will be very very rare indeed. See the FAQ: http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F
You're going to have to set a tolerance and find pixels in that tolerance band. This might be easier to do if you create an RGB image than use a pseudocolor lookup table (colormap) on an indexed image.
GL1 = 7.995;
GL2 = 8.005;
binaryImage = grayImage >= GL1 & grayImage <= GL2;
% Extract the individual red, green, and blue color channels.
redChannel = grayImage ;
greenChannel = grayImage ;
blueChannel = grayImage ;
maxGL = max(grayImage(:));
redChannel(binaryImage) = maxGL;
greenChannel(binaryImage) = 0;
blueChannel(binaryImage) = 0;
% Recombine separate color channels into a single, true color RGB image.
rgbImage = cat(3, redChannel, greenChannel, blueChannel);
rgbImage = rgbImage / maxGL; % Scale to -1 range for display.
imshow(rgbImage);
That's untested code just off the top of my head.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!