这里有一些代码可以帮助您找到正确的方向。基本上,您拍摄背景图像和前景图像,并且两者的差异超过容差阈值,这些像素被视为“前景”。将新背景中的这些相同像素替换为网络摄像头上捕获的像素,以获得背景替换图像。使用从视频中拍摄的图像来获得动态背景。
% User Input:
threshVal = 0; % Tolerance for difference between 'webcam' background and foreground images
%%%%%%%%%%%%
% End User input
% Read in some images
% img1 is the webcam background image
img1 = imread('http://i.imgur.com/toD56.jpg');
% bkgdImg is the image to replace the background of img1 with
bkgdImg = imread('http://i.imgur.com/dIMct6u.jpg');
% Resize bkgdImg so it is the same size as img1. With two images from the same webcam this will not be necessary.
bkgdImg = imresize(bkgdImg, [size(img1, 1), size(img1, 2)]);
% Draw on 'webcam' image to make it different
% In actual algorithm, img2 would be a second image captured with the
% webcam with the same background but different foregrounds, such as a
% person now standing in front of camera.
img2 = img1;
% Draw Ncircles number of random-colored circles. Details here are not
% important except that it adds a unique foreground to the second 'webcam'
% image
Ncircles = 10;
X = bsxfun(@plus,(1:size(img1, 1))',zeros(1,size(img1, 2)));
Y = bsxfun(@plus,(1:size(img1, 2)),zeros(size(img1, 1),1));
for k = 1:Ncircles
circleCenter = rand(1,2).*[size(img1, 1), size(img1, 2)];
circleRadii = 50*rand(1);
B = sqrt(sum(bsxfun(@minus,cat(3,X,Y),reshape(circleCenter,1,1,[])).^2,3))<=circleRadii;
Npix = sum(B(:));
img2(repmat(B, 1, 1, 3) == 1) = reshape(255*ones(Npix, 1)*rand(1,3), [], 1);
end
% Take difference of background and foreground images
% If difference is greater than threshVal, then add those pixels to the
% mask.
% The operation has to be done both ways because images are unsigned
% integers and negative values are ignored.
diffMask = ((img1 - img2) > threshVal) | ((img2 - img1) > threshVal);
% Reduce mask by taking any value along 3rd dimension as logical true
diffMask = any(diffMask, 3);
% Replicate replacement background
bkgdImgReplace = bkgdImg;
% In regions where mask is true (ie where background ~= foreground from
% webcam) replace those pixels in the replacement background image with the
% foreground pixels
bkgdImgReplace(repmat(diffMask, 1, 1, 3)) = img2(repmat(diffMask, 1, 1, 3));
% Display
figure()
subplot(2, 2, 1)
imshow(img1)
title('Original Background')
subplot(2,2,2)
imshow(img2)
title('Modified Background w/ New foreground')
subplot(2,2,3)
imshow(bkgdImg)
title('Replacement Background')
subplot(2,2,4)
imshow(bkgdImgReplace);
title('Replacement Background w/ New foreground')