重叠灰度和RGB图像

时间:2017-09-05 19:52:01

标签: image matlab colors rgb colormap

我想重叠两个图像,一个灰度和一个RGB图像。我想将RGB图像强加在灰度图像的顶部,但仅限于大于某个值的像素。我尝试在MATLAB中使用double函数,但这似乎改变了颜色方案,我无法恢复原始的RGB颜色。我应该怎么做才能保留原始RGB图像而不是将其映射到其中一个MATLAB色彩图?以下是我尝试叠加:

pixelvalues = double(imread('hello.png'));
PixelInt = mean(pixelvalues,3);
I1 = ind2rgb(Brightfield(:,:,1), gray(256)); %Brightfield
I2 = ind2rgb(PixelInt, jet(256)); %RGB Image
imshow(I2,[])
[r,c,d] = size(I2);
I1 = I1(1:r,1:c,1:d);
% Replacing those pixels below threshold with Brightfield Image
threshold = 70;

I2R = I2(:,:,1); I2G = I2(:,:,2); I2B = I2(:,:,3);
I1R = I1(:,:,1); I1G = I1(:,:,2); I1B = I1(:,:,3);

I2R(PixelInt<threshold) = I1R(PixelInt<threshold);
I2G(PixelInt<threshold) = I1G(PixelInt<threshold);
I2B(PixelInt<threshold) = I1B(PixelInt<threshold);
I2(:,:,1) = I2R; I2(:,:,2) = I2G; I2(:,:,3) = I2B;

h = figure;
imshow(I2,[])

原始RGB图像:

Original RGB Image

明场:

enter image description here

重叠

Overlay

2 个答案:

答案 0 :(得分:2)

您在第一张图片中显示的pixelvalues内容是什么?如果是这样,该图像不使用jet色彩映射。它具有高于红色值的粉红色和白色值,而jet在上限处停在深红色处。当您获取这些值的平均值,然后使用jet色彩图生成ind2rgb的新RGB图像时,您将创建一个固有的不同图像。您可能希望直接使用pixelvalues生成叠加层,如下所示:

% Load/create your starting images:
pixelvalues = imread('hello.png');           % Color overlay
I1 = repmat(Brightfield(:, :, 1), [1 1 3]);  % Grayscale underlay
[r, c, d] = size(pixelvalues);
I1 = I1(1:r, 1:c, 1:d);

% Create image mask:
PixelInt = mean(double(pixelvalues), 3);
threshold = 70;
mask = repmat((PixelInt > threshold), [1 1 3]);

% Combine images:
I1(mask) = pixelvalues(mask);
imshow(I1);

请注意,在加载/创建起始图像时,您可能需要进行一些类型转换。我假设'hello.png'uint8 RGB imageBrightfield属于uint8类型。如果我将第一张图片加载为pixelvalues而第二张图片加载为I1,则在运行上述代码时会收到以下内容:

enter image description here

答案 1 :(得分:0)

创建一个遮罩并用它来组合图像:

onionOrig = imread('onion.png'); onionGray = rgb2gray(onionOrig); onionMask = ~(onionOrig(:,:,1)<100 & onionOrig(:,:,2)<100 & onionOrig(:,:,3)<100); onionMasked(:,:,1) = double(onionOrig(:,:,1)) .* onionMask + double(onionGray) .* ~onionMask; onionMasked(:,:,2) = double(onionOrig(:,:,2)) .* onionMask + double(onionGray) .* ~onionMask; onionMasked(:,:,3) = double(onionOrig(:,:,3)) .* onionMask + double(onionGray) .* ~onionMask; onionFinal = uint8(onionMasked); imshow(onionFinal)