Processing + OpenCV背景减法:保存前景图像 [英] Processing + OpenCV Background subtraction: save foreground images

查看:151
本文介绍了Processing + OpenCV背景减法:保存前景图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用实时视频片段构建交互式安装。英语不是我的母语,对不起可能的错误。



我想使用现场网络摄像头镜头。该程序应具有背景的参考图像,并应从移动的前景中减去该图像。每隔几帧它就会捕捉到前景并将这些快照堆叠在一起(以不透明度为单位),这样当人们走过时就会形成透明的痕迹。



它应如下所示: http://postimg.org/image/ebse5tt5v/



我不是一个熟练的程序员,但我有一些处理经验。我正在使用OpenCV库,我得到了背景减法,但它没有使用参考图像。这在Processing + OpenCV中甚至可能吗?有谁知道怎么样?



感谢任何帮助,谢谢!



处理草图:

I am building an interactive installation using live video footage. English isn't my native language, sorry for possible mistakes.

I want to use live webcam footage. The program should have a reference image of the background and should subtract that from the moving foreground. Every few frames it should capture the foreground and stack those snaps on top of each other (in an opacity), so that it forms a transparent trail when people walk by.

It should look like this: http://postimg.org/image/ebse5tt5v/

I'm not a skilled programmer, but I have some experience in Processing. I'm using the OpenCV library and I got the background subtraction working, however it isn't using a reference image. Is this even possible in Processing + OpenCV? And does anyone know how?

Any help is appreciated, thanks!

Processing sketch:

import gab.opencv.*;
import processing.video.*;

Capture cam;
Movie video;
OpenCV opencv;

void setup() {
  size(640, 480);
  cam = new Capture(this, 640, 480, 30);
  cam.start();
  
  opencv = new OpenCV(this, 640, 480);
  // opencv.capture(640, 480);
  opencv.startBackgroundSubtraction(5, 3, 0.1);
  
}

void draw() {
  if(cam.available()){
    cam.read(); 
  }
  
  image(cam, 0, 0);  
  opencv.loadImage(cam); 
  
  opencv.updateBackground();  
  opencv.dilate();
  opencv.erode();

  noFill();
  stroke(255, 0, 0);
  strokeWeight(4);
  for (Contour contour : opencv.findContours()) {
    contour.draw();
  }
}

void movieEvent(Capture m) {
  m.read();
}

推荐答案

我的回答的第一部分可能会出乎意料,因为它甚至没有在Open CV文档中讨论过:即使基于参考背景图像(称为背景模型),严格的背景减法理论上也是不可能的。这是因为即使两个图像也提供了关于场景的不完整信息,例如,没有描述前景中背景物体的反射,反之亦然。以下是说明这个想法的简单示例:背景对象可以在某些背景对象上投射阴影。由于您的参考图片没有这个阴影,阴影区域可能会被误认为是前景对象的一部分,实际情况并非如此。问题的根源是:一系列2D图像不能提供构建3D模型的完整信息,同时考虑到所有表面上的所有光线跟踪和反射/耗散,留出体积耗散。因此,背景分离的所有方法都是近似的,可以根据场景给出非常纯净的结果。



Open CV中的背景减法,基于前期录制的背景模型,在此解释:

http://docs.opencv。 org / master / db / d5c / tutorial_py_bg_subtraction.html [ ^ ],

http:// docs。 opencv.org/master/d1/dc5/tutorial_background_subtraction.html [ ^ ]。



请注意,演示的操作不是故事的结尾。你只有背景分离的黑白模型。您必须保留前景图像并将其放在白点上(乘法可以工作),并将黑色背景替换为零不透明像素。你仍然会有一些误报,前景像素被错误地识别为背景。



-SA
First part of my answer may come at surprise, because it's not even discussed in the Open CV documentation: strict background subtraction, even based on referenced background image (called background model), is theoretically impossible. This is because even two images provides incomplete information on the scene, which does not describe, for example, reflexes of background objects on foreground and visa versa. Here is the simple example illustrating the idea: a background object can cast a shadow on some background object. As your reference picture does not have this shadow, the shadowed area can be mistaken for the part of the foreground object, which is not really the case. The root of the problem is: a sequence of "2D" images does not provide full information to build a 3D model taking into account all ray tracing and reflection/dissipation on all surfaces, set aside dissipation in volume. So, all the methods of background separation are approximate and can give you quite pure results, depending on the scene.

The background subtraction in Open CV, based on pre-recorded background model, is explained here:
http://docs.opencv.org/master/db/d5c/tutorial_py_bg_subtraction.html[^],
http://docs.opencv.org/master/d1/dc5/tutorial_background_subtraction.html[^].

Note that the demonstrated operations is not the end of story. You got only the black-and-white model of background separation. You have to preserve the foreground image and put it on the white spots (multiplication can work), and replace black background with, say, zero-opacity pixels. You still can have some false-positives, foreground pixels mistakenly recognized as background.

—SA


这篇关于Processing + OpenCV背景减法:保存前景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆