根据附件图像,如何获得输出图像而不受输入图像中的黑色影响?如果我使用imfuse(input, background, 'blend'),
它,它将混合输入颜色和背景颜色。我不知道是否为此目的而建立了方法或算法。(请注意,输入图像中的背景色为浅紫色和深紫色+黑色)
input, background, output image
让我们创建两个示例图像,类似于您显示的图像。请注意,这只是创建两个不太乏味的图像进行测试的简单方法。
bg = bsxfun(@times,permute([0.9, 0.8, 1.0], [1,3,2]), ones(100,100));
obj = bsxfun(@times,permute([0.3, 0.1, 0.4], [1,3,2]), triu(ones(100,100)));
现在,通过使用imfuse(bg, obj, 'blend')
,您可以应用Alpha混合,这是根据Wikipedia进行的
the process of combining an image with a background to create the appearance of partial or full transparency.
This appears not to be what you want. What you probably want is to take take all non-black parts of obj
and place them over bg
without any transparency effects. Do do that, we first have to find the black and non-black parts in obj
. Secondly, we remove the background wherever we want our object to be. Finally, we can just add obj
to it, as black = 0.
% Find and save all parts of obj which are not black
mask = sum(obj,3) > 0;
% Remove background from locations where the object will be placed, then add object
result = bsxfun(@times, bg, ~mask) + obj;
Some more explanation:
In the first line, we use sum(obj,3)
to sum R, G, and B for each pixel. By comparing that pixel-wise to zero, we get a matrix mask
, which contains a 0
for each pixel that is black in obj
, and a 1
otherwise.
然后,使用bsxfun(@times, bg, ~mask)
,我们将背景图像bg
的每个通道R,G和B乘以掩模的倒数,即乘以1
像素是否为黑色obj
(即我们要使用背景),并乘以0
是否要替换该像素由对象决定。这样做的结果将是背景图像,用零只要obj
不为零,并用其原始值的地方obj
是零。因此,我们可以简单地添加obj
它并获得我们的结果。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句