MATLAB网络摄像头背景替换

时间:2014-11-16 15:52:27

标签: matlab replace background real-time webcam

如何更改此视频的实时网络摄像头背景:

https://www.youtube.com/watch?v=YhJPbeI3vVU

有人可以解释做什么和从哪里开始,我需要chromakey,opencv或类似的东西吗?它不需要是视频背景,图像就可以。

我正在使用matlab并且已经完成了GUI

 %% object initialitation
                caminf = imaqhwinfo;
                mycam = char(caminf.InstalledAdaptors(end));
                mycaminfo = imaqhwinfo(mycam);
                resolution = char(mycaminfo.DeviceInfo.SupportedFormats(end));
                vd = videoinput(mycam, 1, resolution);

  %% Previewing video

  vidRes = get(vd, 'VideoResolution');
  nBands = get(vd, 'NumberOfBands');
  hImage = image(zeros(vidRes(2), vidRes(1), nBands));
  preview(vd, hImage);

THX,

问候。

1 个答案:

答案 0 :(得分:0)

这里有一些代码可以帮助您朝着正确的方向前进。基本上,您拍摄背景图像和前景图像,并且两者的差异超过容差阈值,这些像素被视为前景'。将新背景中的这些相同像素替换为网络摄像头上捕获的像素,以获取背景替换图像。使用从视频中拍摄的图像来获得动态背景。

% 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')