我在黑色背景上有一些形状的图像。我想将这些形状叠加在另一个图像上。这是一个示例:
m = 200; n = m*3/2; p = m / 2;
background = im2double(rgb2gray(imresize(imread('pears.png'), [m, n])));
[x, y] = meshgrid(linspace(-1, 1, 64));
shape1 = imadjust(im2double(imresize(imread('moon.tif'), [m, m])), [.1 .9], [0, 1]);
shape2 = imadjust(im2double(rgb2gray(imresize(imread('saturn.png'), [m, m]))), [.1 .9], [0, 1]);
mask1 = double(shape1>0);
mask2 = double(shape2>0);
I = background;
I(:, (1:m)+0) = (1-mask1).*I(:, (1:m)+0) + mask1.*shape1;
I(:, (1:m)+p) = (1-mask2).*I(:, (1:m)+p) + mask2.*shape2;
结果:
如何删除那些尖锐的黑色边缘?
答案 0 :(得分:4)
您可以使用imerode
轻微侵蚀蒙版以删除黑色边缘,然后使用imfilter
对其进行过滤,以便将图像平滑地混合在一起。以下是半径为2的磁盘过滤器的简单示例:
erode1 = imerode(mask1, strel('disk', 2));
erode2 = imerode(mask2, strel('disk', 2));
blend1 = imfilter(erode1, fspecial('disk', 2));
blend2 = imfilter(erode2, fspecial('disk', 2));
I = background;
I(:, (1:m)+0) = (1-blend1).*I(:, (1:m)+0) + blend1.*shape1;
I(:, (1:m)+p) = (1-blend2).*I(:, (1:m)+p) + blend2.*shape2;