OpenCV通过帧平均的背景减法 [英] OpenCV Background subtraction by frame averaging
问题描述
我如何实现背景扣除(通过平均firsts.say 50帧..平均获得的背景模型)在opencv
我试图寻找一些代码,但发现他们是在python..im工作在c + +(视觉工作室2013)
一个小的工作代码片段将帮助... thanks!
How do i implement background subtraction(background model obtained by averaging first..say 50 frames..) in opencv I tried looking for some codes but found they were in python..im working in c++(visual studio 2013) A small working code snippet will help..thanks!
推荐答案
OpenCV提供背景扣除功能。请参见 BackgroundSubtractorMOG2 ,它使用高斯混合模型构建背景,因此对后台更改非常稳健。
OpenCV provide background subtraction capabilities. See BackgroundSubtractorMOG2, that models the background with a Mixture of Gaussians, and is therefore quite robust to background changes.
参数 history
是用于构建背景的帧数
The parameters history
is the numbers of frame used to build the background model.
#include <opencv2\opencv.hpp>
using namespace cv;
int main(int argc, char *argv[])
{
int history = 50;
float varThreshold = 16.f; // default value
BackgroundSubtractorMOG2 bg = BackgroundSubtractorMOG2(history, varThreshold);
VideoCapture cap(0);
Mat3b frame;
Mat1b fmask;
for (;;)
{
cap >> frame;
bg(frame, fmask, -1);
imshow("frame", frame);
imshow("mask", fmask);
if (cv::waitKey(30) >= 0) break;
}
return 0;
}
这篇关于OpenCV通过帧平均的背景减法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!