BackgroundSubtractorGMG 背景建模

#include <opencv2/bgsegm.hpp>
#include <opencv2/video.hpp>
#include <opencv2/opencv.hpp>
#include <iostream>
#include <sstream>
using namespace cv;
using namespace std;
using namespace bgsegm;
// GMG目标建模检测
void detectBackground(Ptr<BackgroundSubtractorGMG> pBackgroundGMG,
	string videoFilename) 
{
	cv::Mat frame,FGMask; 
	int keyboard=0;
	// 视频获取
	VideoCapture capture(videoFilename);
	if(!capture.isOpened())
		exit(EXIT_FAILURE);
	// 按下q键和esc退出
	while( (char)keyboard != 'q' && (char)keyboard != 27 )
	{
		// 读取当前帧
		if(!capture.read(frame)) 
			exit(EXIT_FAILURE);
		// 图像尺寸缩小
		cv::resize(frame, frame,cv::Size(), 0.2,0.2);
		// 背景建模参数设定
		pBackgroundGMG->setDefaultLearningRate(0.05);
		pBackgroundGMG->setDecisionThreshold(0.9);
		pBackgroundGMG->setSmoothingRadius(9);
		pBackgroundGMG->setNumFrames(100);
		//  背景模型生成
		pBackgroundGMG->apply(frame, FGMask);
		// 输出当前帧号
		stringstream ss;
		rectangle(frame, cv::Point(10, 2), cv::Point(100,20),
			cv::Scalar(255,255,255), -1);
		ss << capture.get(CAP_PROP_POS_FRAMES);
		string frameNumberString = ss.str();
		// 左上角显示帧号
		putText(frame, frameNumberString.c_str(),
			cv::Point(15, 15),
			FONT_HERSHEY_SIMPLEX, 0.5 , cv::Scalar(0,0,0));
		// 输出结果
		imshow("Frame", frame);
		imshow("FGMask", FGMask);
		keyboard = waitKey(30);
	}
	// 视频流释放
	capture.release();
}
int main(int argc, char* argv[])
{
    // 创建GMG背景建模类
    Ptr<BackgroundSubtractorGMG> pBackgroundGMG = 
		createBackgroundSubtractorGMG(); 
	string inputPath = "..\\images\\car.avi";
	// 背景建模检测
    detectBackground(pBackgroundGMG, inputPath);
    return 0;
}




转载:http://blog.csdn.net/zhuwei1988

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值