简体   繁体   中英

Matlab colormap - How can I change only ONE specific value, not a range of values?

This is my first question on StackOverflow so forgive me if I make some mistakes.

I have to visualize multiple single channel images (2D matrices) with MATLAB. The value of each pixel usually ranges between ~10^-10 and ~10^-6 . I am using a flipped jet colormap (so dark red is low and dark blue is high).

Now, some of these matrices contain also some 0 pixel values. I would like to set a specific color (say white) only for these pixels. What i did for now is:

cmap = colormap('jet'); % standard 64 colors jet colormap 
cmap = flipud(cmap);

Then I tried to edit the first row of the colormap and setting it to [1 1 1] (white) following different answers I found online (including How do I change a single color in a colormap in Matlab? ):

cmap(1,:) = [1 1 1];
colormap(cmap);

The problem is that this edit of the colormap sets the first range/64 values (I guess) of the image to white, instead of setting only the 0 ones to white.

I was wondering: is it possible to set only those pixels to white?

I guess that my problem depends on the fact that even for the images with these few 0 valued pixels, the second lowest pixels are many and really small (in the order of 10^-10 ).

Thank you very much in advance! Best wishes!

UJIN

One way around this would be to actually encode your image using the desired colour map, then use logical indexing to set each location in the original image that is 0 to white in this final result.

Therefore, given your 2D image, actually create a version of it that is mapped to the jet colour map with ind2rgb . After that, search for 0 values in the original image, then set these locations in the final coloured result to white. Your last point in your question is very meaningful. If you have values that range between such a small range, and then having values of 0 as well, the colours in the final colour map will be biased towards the very end of the colour map. As such, another thing I can suggest is to set the values of the original image that were originally zero to a value that is within the non-zero range so that it won't saturate the colour mapping. Once you finally convert the image with your proposed colour map, we can then manually set those pixels to white.

Supposing your image was stored in im , do something like this:

cmap = colormap('jet'); % standard 64 colors jet colormap 
cmap = flipud(cmap);
im2 = im;
ind = im == 0; %// Find locations that are zero in the original image
im2(ind) = max(im(:)); %// Make a copy of the original image where 0 pixels are set to the maximum of the image
rgb = ind2rgb(im2, cmap); %// Create pseudo-coloured image
rgb(repmat(ind, [1 1 3])) = 1; %// Set corresponding locations to white

You can then use either imshow if you have the Image Processing Toolbox, or you can use image to visualize the results (ie imshow(rgb) or image(rgb) ).

Minor suggestion

I suggest you change cmap = colormap('jet'); to cmap = jet; instead because colormap('jet') spawns an empty figure window if you don't already have one open. cmap = jet; will give you the same result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM