简体   繁体   English

Matlab颜色图-如何仅更改一个特定值,而不更改一个值范围?

[英]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. 这是我在StackOverflow上遇到的第一个问题,所以请谅解。

I have to visualize multiple single channel images (2D matrices) with MATLAB. 我必须使用MATLAB可视化多个单通道图像(二维矩阵)。 The value of each pixel usually ranges between ~10^-10 and ~10^-6 . 每个像素的值通常在~10^-10~10^-6 I am using a flipped jet colormap (so dark red is low and dark blue is high). 我正在使用翻转的jet (因此深红色较低,深蓝色较高)。

Now, some of these matrices contain also some 0 pixel values. 现在,其中一些矩阵还包含一些0像素值。 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? ): 然后,我尝试根据网上找到的不同答案(包括如何在Matlab中更改颜色图中的单一颜色? )来编辑颜色图的第一行,并将其设置为[1 1 1] (白色):

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. 问题在于,对色图的此编辑将图像的第一个范围/ 64值(我想)设置为白色,而不是仅将0设置为白色。

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 ). 我想我的问题取决于这样一个事实,即使对于具有这几个0值像素的图像,倒数第二个像素也很多而且非常 (大约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. 解决此问题的一种方法是使用所需的颜色映射表对图像进行实际编码,然后使用logical索引将原始图像中的每个位置(在此最终结果中从0设置为白色)。

Therefore, given your 2D image, actually create a version of it that is mapped to the jet colour map with ind2rgb . 因此,给定您的2D图像,请实际创建一个版本,并使用ind2rgb将其映射到jet颜色图。 After that, search for 0 values in the original image, then set these locations in the final coloured result to white. 之后,在原始图像中搜索0值,然后将最终彩色结果中的这些位置设置为白色。 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. 如果您的值介于这么小的范围之间,然后也具有0值,则最终颜色图中的颜色将偏向颜色图的最末端。 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: 假设您的图片存储在im ,请执行以下操作:

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) ). 然后,如果您具有图像处理工具箱,则可以使用imshow ,也可以使用image可视化结果(即imshow(rgb)image(rgb) )。

Minor suggestion 次要建议

I suggest you change cmap = colormap('jet'); 我建议您更改cmap = colormap('jet'); to cmap = jet; cmap = jet; instead because colormap('jet') spawns an empty figure window if you don't already have one open. 相反,因为colormap('jet')生成一个空的图形窗口(如果尚未打开)。 cmap = jet; will give you the same result. 将给您相同的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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