【问题标题】:MATLAB webcam background replacementMATLAB 网络摄像头背景替换
【发布时间】:2014-11-16 15:52:26
【问题描述】:

如何像这个视频一样更改实时网络摄像头背景:

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);

谢谢,

问候。

【问题讨论】:

    标签: matlab replace background real-time webcam


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢,它工作得很好。但是,我如何用网络摄像头实现这个实时?
    • 您使用网络摄像头捕获的背景,而不是使用从互联网上提取的img1。您使用网络摄像头的当前流,而不是此处使用的 img2。而不是bkgdImg,您可以使用任何背景图像或您想要替换网络摄像头背景的视频中的静止图像。然后将所有内容循环播放,并使用来自网络摄像头的新捕获和您选择的替换背景不断更新。
    猜你喜欢
    • 1970-01-01
    • 2012-05-11
    • 1970-01-01
    • 1970-01-01
    • 2016-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多