重叠灰度和 RGB 图像
Overlapping grayscale and RGB Images
我想重叠两张图像,一张灰度图像和一张 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 图像:
明场:
叠加:
pixelvalues
的内容是您在第一张图片中显示的内容吗?如果是这样,则该图像不使用 jet
colormap. It has pink and white values above the red values, whereas jet
stops at dark red at the upper limits. When you take the mean of those values and then generate a new RGB image with ind2rgb
使用 jet
颜色图,您正在创建一个本质上不同的图像。您可能想直接使用 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);
请注意,当 loading/creating 起始图像时,您可能需要进行一些类型转换。我假设 'hello.png'
是 uint8
RGB image and Brightfield
is of type uint8
。如果我将您的第一张图片加载为 pixelvalues
,将您的第二张图片加载为 I1
,当 运行 上面的代码时,我会得到以下结果:
创建一个遮罩并用它来组合图像:
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)
我想重叠两张图像,一张灰度图像和一张 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 图像:
明场:
叠加:
pixelvalues
的内容是您在第一张图片中显示的内容吗?如果是这样,则该图像不使用 jet
colormap. It has pink and white values above the red values, whereas jet
stops at dark red at the upper limits. When you take the mean of those values and then generate a new RGB image with ind2rgb
使用 jet
颜色图,您正在创建一个本质上不同的图像。您可能想直接使用 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);
请注意,当 loading/creating 起始图像时,您可能需要进行一些类型转换。我假设 'hello.png'
是 uint8
RGB image and Brightfield
is of type uint8
。如果我将您的第一张图片加载为 pixelvalues
,将您的第二张图片加载为 I1
,当 运行 上面的代码时,我会得到以下结果:
创建一个遮罩并用它来组合图像:
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)