【发布时间】:2013-02-17 01:22:40
【问题描述】:
我正在从事检测前景物体的视频处理项目。下面是我的一部分代码,用于分离前景和背景。
#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;
}
在前景蒙版窗口中,我收到很多噪点以及实际的前景对象。完整的对象也被识别为前景。我还需要用矩形绑定前景对象。如果我在前景蒙版中绘制轮廓,Wil BoundRect() 会完成这项工作吗?......还有什么是最推荐在查找轮廓时传递的参数(findcontour())和 BoundRect 函数......提前谢谢
【问题讨论】:
-
如果图像中有噪点(就像每个视频中一样),最简单的解决方案是运行某种高斯模糊。我不明白这部分:“同样完整的对象被识别为前景”,你的意思是什么......你需要更清楚一点。
标签: c++ opencv image-processing