MATLAB:在RGB图像上应用透明蒙版并与其他图像混合

Bri*_*dmg 2 matlab alpha-transparency

我有2张图片:前景和背景.前景是一个数字矩阵,范围从-50到300.我通过imagesc显示它.(即这不是RGB图像).背景是RGB图像.

我想先在前景上应用透明蒙版来改变它的外观.这很容易使用

altered_foreground = imagesc(foreground, 'AlphaData', Alphamask)
Run Code Online (Sandbox Code Playgroud)

现在,我想将changed_foreground叠加在背景之上.问题是因为我已经在前台使用了Alphamask,所以我无法通过以下方式叠加它:

imagesc(background)
hold on
bimage = imagesc(altered_foreground)
set(bimage, 'AlphaData', altered_foreground)
Run Code Online (Sandbox Code Playgroud)

(如果我只是想在我要使用的背景上叠加一个未改变的前景,那就不起作用了:

imagesc(background)
hold on
bimage = imagesc(foreground)
set(bimage, 'AlphaData', foreground)
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

编辑

以下是数据示例:

前景:

下载图片; 键入以下代码来处理它:

Foreground = im2double(imread('500x_54.jpg'));
Foreground = Foreground + 50*randn(101,1);
Run Code Online (Sandbox Code Playgroud)

我改变的前景可以是一些简单的事情,就像让图像的前100列完全透明(实际上,它有点复杂,我对这些值进行阈值处理)

背景:

同样,下载图像并输入:

Background = imread('2-effect1-500x225.jpg');
Run Code Online (Sandbox Code Playgroud)

Yvo*_*von 6

编辑:很抱歉昨天误解了你的问题,所以这是你的全彩版:P


基本思想与MATLAB几乎相同:将两个不同Alpha的灰度图像组合在一起,但在你的情况下,需要更多的动作来获得正确的东西.

首先,使用提供的样本重新创建您描述良好的情况

% load foreground image, and scale to [-50, 300]
Foreground = imread('500x_54.jpg');
figure(1)
imshow(Foreground)
Foreground = im2double(Foreground)*350-50;

% load background image
Background = im2double(imread('2-effect1-500x225.jpg'));
figure(2)
imshow(Background)
Run Code Online (Sandbox Code Playgroud)

然后从头开始制作一个alpha通道.请注意,我不是在使用,imagesc而是编写一个普通的双数组.这确实是一个alpha通道!不需要那么多的谜团.

% build alpha layer for Foreground
alpha = bsxfun(@times, ones(size(Foreground,1), size(Foreground,2)), .6);
alpha(:,[1:53,149:203,290:352,447:end])=0;
alpha([1:58,170:end],:)=0;
figure(3)
imshow(alpha)
Run Code Online (Sandbox Code Playgroud)

在混合之前,我想把前面的"后面"缩放成[0,1].由于背景图像是从常规图片中新加载的,因此不需要标准化; 只有前景范围从-50到300.

问题是有时你有疯狂的数据,如-1001000.我不知道你想怎么解释它们.如果你把它[-50. 300]作为常规的,典型的,应该是的范围,那么你如何映射-1001000进入颜色级别?

有两种选择/方法可以处理这种情况:1)[-100, 1000]用作新的比例.所以-100将是纯黑色和1000纯色; 2)继续使用[-50, 300]作为比例范围,因此超出此范围的所有内容将被映射(强制)到最近的边界.

在这里,我选择第一个,使用自适应机制至少限制范围[-50, 300].因此,如果您的数据如此[-10,200],您仍然可以获得规模[-50, 300].我认为这更有意义.

% find a scale dynamically with some limit
Foreground_min = min( min(Foreground(:)), -50 );
Foreground_max = max( max(Foreground(:)), 300 );
Run Code Online (Sandbox Code Playgroud)

混合程序与该帖几乎相同.但是你正在使用RGB图像,所以你需要add所有3个颜色层的数字; bsxfun用于替换较慢的+.*操作.

% overlay the image by blending
Background_blending = bsxfun(@times, Background, bsxfun(@minus,1,alpha));
% Background_blending = Background.*repmat((1-alpha), 1, 1, 3);
Foreground_blending = bsxfun( @times, bsxfun( @rdivide, ...
    bsxfun(@minus, Foreground, Foreground_min), ... 
    Foreground_max-Foreground_min ), alpha );
% Foreground_blending = (Foreground-Foreground_min) / ...
%     (Foreground_max-Foreground_min).*repmat(alpha, 1, 1, 3);
% out = bsxfun(@plus, Background_blending, Foreground_blending);
out = Background_blending + Foreground_blending;
figure(4)
imshow(out)
Run Code Online (Sandbox Code Playgroud)

除第一个之外的注释行是"常规"分配命令而不使用bsxfun,但执行相同的工作,并且更容易理解:)

结果

结果