在MATLAB中减法后腿上的背景停留

时间:2016-11-06 05:15:34

标签: image matlab image-processing computer-vision computer-science

左边的图片是我的背景图片,右边的图片是原始图片:

enter image description here

做减法后,我在去除脚踝背景时遇到了麻烦:

enter image description here

目的是只获取前景,是否有人有想法?下面显示的是关于如何执行背景减法以及显示它的代码。谢谢。

 I = (imread('frame9.jpg'));
 figure(1),imshow(I);
 bg =(imread('background.jpg'));
 figure(2),imshow(bg);
 Ip = bg-I;
 figure(3),imshow(Ip);

1 个答案:

答案 0 :(得分:0)

以下解决方案不完整,但它可以为您提供领导:

我使用以下操作来创建一个掩码:

  • 每个轴缩小x2倍的图像(减少一些噪点)。
  • 计算缩小图像的绝对差异。
  • 使用阈值转换为二进制图像(使用graythresh找到阈值)。
  • 扩张二进制图像。
  • 将二进制文件调整为完整大小。
  • 通过将其与二进制掩码相乘来掩盖前景图像中的像素。

检查以下代码:

%Read two images from hosting site.
twoImages = imread('https://i.stack.imgur.com/5dN5O.png');

%Convet from uint8 to double (pixels range [0, 1])
twoImages = im2double(twoImages);

B = twoImages(7:332, 23:599, :);
I = twoImages(7:332, 706:1282, :);

%Shrink images by factor of x2 in each axis.
sB = imresize(B, 0.5);
sI = imresize(I, 0.5);

%Diference of shrank images.
sD = abs(sB - sI);

%Convert to binary image, using threshold.
level = graythresh(sD);
sBW = im2bw(sD, level);

%Dilate binary image
se = strel('disk', 6);
sBW = imdilate(sBW, se);

%Resize binary image to size of I (create binary mask).
BW = imresize(sBW, [size(I,1), size(I,2)]);

%Mask elements with BW = 0 (BW is used as mask).
I = I .* double(cat(3, BW, BW, BW));
figure;imshow(I);

结果:
enter image description here