我正在处理视频处理项目以检测前景物体。下面是我用来分离前景和背景的代码的一部分。改善背景减法
#include "opencv2/core/core.hpp"
#include "opencv2/video/background_segm.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdio.h>
using namespace std;
using namespace cv;
//this is a sample for foreground detection functions
int main(int argc, const char** argv)
{
VideoCapture cap;
bool update_bg_model = true;
cap.open(0);
if(!cap.isOpened())
{
printf("can not open camera or video file\n");
return -1;
}
namedWindow("image", CV_WINDOW_NORMAL);
namedWindow("foreground mask", CV_WINDOW_NORMAL);
namedWindow("foreground image", CV_WINDOW_NORMAL);
namedWindow("mean background image", CV_WINDOW_NORMAL);
BackgroundSubtractorMOG2 bg_model;
Mat img, fgmask, fgimg;
for(;;)
{
cap >> img;
if(img.empty())
break;
if(fgimg.empty())
fgimg.create(img.size(), img.type());
//update the model
bg_model(img, fgmask, update_bg_model ? -1 : 0);
fgimg = Scalar::all(0);
img.copyTo(fgimg, fgmask);
Mat bgimg;
bg_model.getBackgroundImage(bgimg);
imshow("image", img);
imshow("foreground mask", fgmask);
imshow("foreground image", fgimg);
if(!bgimg.empty())
imshow("mean background image", bgimg);
char k = (char)waitKey(30);
if(k == 27) break;
if(k == ' ')
{
update_bg_model = !update_bg_model;
if(update_bg_model)
printf("Background update is on\n");
else
printf("Background update is off\n");
}
}
return 0;
}
在前景蒙板窗口中,我收到很多噪音以及实际的前景物体。另外,fulll对象被识别为前景。我还需要用矩形限制前景地物。 Wil BoundRect()如果我在前景蒙板中绘制轮廓,还能做什么工作?...还有什么是在查找轮廓(findcontour())和BoundRect函数时要传递的最推荐的参数......预先感谢
如果图像中有噪声(如每个视频中),最简单的解决方案是运行某种高斯模糊。 我不明白这个部分:“另外fulll对象被认为是前景”,你是什么意思..你需要稍微更清楚一点。 – OopsUser 2013-03-03 20:57:47
我的意思是这样的:http://answers.opencv.org/upfiles/13623024413088434.png – hunter 2013-03-08 01:48:12