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?
198 views (last 30 days)
Show older comments
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
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
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.
See Also
Categories
Find more on Blue in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!