将MATLAB缩放图像叠加到灰度图像以选择像素

时间:2016-09-12 04:21:08

标签: image matlab overlay grayscale

我是MATLAB图像处理的新手,目前我有两个图像 - 一个是我的对象的灰度图像,第二个是使用imagesc函数从MATLAB生成的缩放图像。我试图将这个缩放的图像叠加在我的灰度图像上,以获得空间分辨率,以便于观察。附上两张图片:

A)灰度图像:

grayscale image

B)缩放图像: scaled image

我遇到了一些困难。首先,缩放后的图像不会保存在相同的像素尺寸中,但我可以使用imwrite函数来解决这个问题:

im = imagesc(ScaledDiff);
imwrite(get(im,'cdata'),'scaleddiff.tif')

然而,这样做会导致色条和色彩图丢失。其次,即使我设法将缩放图像缩小到灰度图像的大小,覆盖它仍然是一个挑战。理想情况下,我希望将透明度(或“alpha”)设置为0,以使用<缩放图像值为0.02。

任何关于如何做到这一点的想法将不胜感激!对不起,如果我不清楚的话!

更新: 感谢Rotem,我设法覆盖了灰度图像和热图的特定区域:

overlay

但是,我需要显示与热图值对应的颜色条,否则信息会丢失并且叠加层将无用。我该怎么做?下面是我的代码片段,其中ScaledIntDiff包含热图上显示的0到0.25的值:

Brightfield = imread('gray.jpg'); % read background image
I1 = ind2rgb(gray2ind(Brightfield), gray); % convert indices into RGB scale

scale = 1000;
ScaledIntDiff2 = round(ScaledIntDiff.*scale); 
I2 = ind2rgb(ScaledIntDiff2, jet(256)); % this is for the heatmap

threshold = 0.02;
I2R = I2(:,:,1); I2G = I2(:,:,2); I2B = I2(:,:,3);
I1R = I1(:,:,1); I1G = I1(:,:,2); I1B = I1(:,:,3);
% Replace pixels in I2 with pixels in I1 if the value of ScaledIntDiff of those pixels is below the threshold
I2R(ScaledIntDiff<threshold) = I1R([ScaledIntDiff<threshold]);
I2G(ScaledIntDiff<threshold) = I1G([ScaledIntDiff<threshold]);
I2B(ScaledIntDiff<threshold) = I1B([ScaledIntDiff<threshold]);
I2(:,:,1) = I2R; I2(:,:,2) = I2G; I2(:,:,3) = I2B;

figure
imshow(I2) 

我知道上面的代码非常低效,因此非常欢迎有关如何改进它的建议。谢谢!

1 个答案:

答案 0 :(得分:1)

检查以下内容:

<div id="acc-construct" class="hidden">
    <div class="acc-group">
        <div class="acc-head">
            <a class="acc-toggle collapsed acc-default" data-toggle="collapse"
               data-parent="#acc" href="#collapse-divsInContainer">
                <i data-arrow="" class="pull-right fa fa-caret-down"></i>
            </a>
        </div>
        <div id="collapse-divInContainer" class="acc-body collapse">
            <div class="acc-inner">
                <dl class="dl-horizontal"></dl>
                <div class="separator"></div>
            </div>
        </div>
    </div>
</div>

enter image description here

添加I = imread('CKbi2Ll.jpg'); %Load input. %Convert I to true color RGB image (all pixels R=G=B are gray color). I1 = ind2rgb(I, gray(256)); %Convert I to true color RGB image with color map parula (instead of using imagesc) I2 = ind2rgb(I, parula(256)); %Set the transparency (or 'alpha') to 0 for those pixels with < 0.02 in scaled image value. %Instead of setting transparency, replace pixels with values from I1. threshold = 0.02; %Set threshold to 0.02 I2(I1 < threshold) = I1(I1 < threshold); %Blend I1 and I2 into J. alpha = 0.3; %Take 0.3 from I2 and 0.7 from I1. J = I2*alpha + I1*(1-alpha); %Display output imshow(J); ,其中包含从0到0.25的刻度标签:

  • 将颜色映射设置为colorbar - 它不会影响显示的图像,因为图像格式是真彩色RGB。
  • 从0到250添加6个刻度的Crate数组。
  • 从0到0.25创建6 parula的单元格数组。
  • 使用先前创建的TickLabelscolorbar属性添加Ticks

TickLabels之后添加以下代码:

imshow(J);

enter image description here

colormap(parula(256));
TLabels = cellstr(num2str((linspace(0, 0.25, 6))'));
T = linspace(1, 250, 6);
colorbar('Ticks', T', 'TickLabels', TLabels);

enter image description here