BackgroundSubtractorMOG2前后背景分离

BackgroundSubtractorMOG2 重要方法

1. BackgroundSubtractorMOG2 的构造函数

    C++: BackgroundSubtractorMOG2::BackgroundSubtractorMOG2()

  采用默认值进行构造BackgroundSubtractorMOG2的对象。

Parameters:
  • image – Next video frame.
  • fgmask – The output foreground mask as an 8-bit binary image.
  • learningRate – The value between 0 and 1 that indicates how fast the background model is learnt. Negative parameter value makes the algorithm to use some automatically chosen learning rate. 0 means that the background model is not updated at all, 1 means that the background model is completely reinitialized from the last frame.

   C++:   BackgroundSubtractorMOG2:: BackgroundSubtractorMOG2(int history, float varThreshold, bool bShadowDetection=true)

    采用指定值进行构造BackgroundSubtractorMOG2的对象。

    参数说明:  

Parameters:
  • history – Length of the history.
  • varThreshold – Threshold on the squared Mahalanobis distance to decide whether it is well described by the background model (see Cthr??). This parameter does not affect the background update. A typical value could be 4 sigma, that is,varThreshold=4*4=16; (see Tb??).
  • bShadowDetection – Parameter defining whether shadow detection should be enabled (true or false).

 

 

2. BackgroundSubtractorMOG2 的operate

    C++: void BackgroundSubtractorMOG2::operator()(InputArray image, OutputArray fgmask, doublelearningRate=-1)

  参数说明:  参数image为待处理的图像;fgmask得到的前景图像(二值化都的);learningRate(0~1)配置背景更新方法,0表示不更新,1表示根据最后一帧更新,负数表示自动更新,(0~1)数字越大,背景更新越快

//--------------------------------------【程序说明】-------------------------------------------
//		程序说明:《OpenCV3编程入门》OpenCV2版书本附赠示例程序20
//		程序描述:前后背景分离
//		测试所用操作系统: Windows 7 64bit
//		测试所用IDE版本:Visual Studio 2010
//		测试所用OpenCV版本:	2.4.9
//		2014年11月 Revised by @浅墨_毛星云
//------------------------------------------------------------------------------------------------


//---------------------------------【头文件、命名空间包含部分】----------------------------
//		描述:包含程序所使用的头文件和命名空间
//------------------------------------------------------------------------------------------------
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/video/background_segm.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdio.h>
using namespace std;
using namespace cv;


//--------------------------------------【help( )函数】--------------------------------------
//		 描述:输出一些帮助信息
//----------------------------------------------------------------------------------------------
static void help()
{
	printf("\n\n\t此程序展示了视频前后背景分离的方法,采用cvUpdateBGStatModel()方法.\n"
		"\n\n\t程序首先会“学习背景”,然后进行分割。\n"
		"\n\n\t可以用过【Space】空格进行功能切换。\n\n");
}


//-----------------------------------【main( )函数】--------------------------------------------
//		描述:控制台应用程序的入口函数,我们的程序从这里开始
//-------------------------------------------------------------------------------------------------
int main(int argc, const char** argv)
{
	help();
	VideoCapture cap;
	bool update_bg_model = true;

	//cap.open(0);
	cap.open("1.avi");

	if( !cap.isOpened() )
	{
		printf("can not open camera or video file\n");
		return -1;
	}

	namedWindow("image", WINDOW_AUTOSIZE);
	namedWindow("foreground mask", WINDOW_AUTOSIZE);
	namedWindow("foreground image", WINDOW_AUTOSIZE);
	namedWindow("mean background image", WINDOW_AUTOSIZE);

	BackgroundSubtractorMOG2 bg_model;//(100, 3, 0.3, 5);

	Mat img, fgmask, fgimg;

	for(;;)
	{
		cap >> img;

		if( img.empty() )
			break;

		//cvtColor(_img, img, COLOR_BGR2GRAY);

		if( fgimg.empty() )
			fgimg.create(img.size(), img.type());

		//更新模型
		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(1);
		if( k == 27 ) break;
		if( k == ' ' )
		{
			update_bg_model = !update_bg_model;
			if(update_bg_model)
				printf("\t>背景更新(Background update)已打开\n");
			else
				printf("\t>背景更新(Background update)已关闭\n");
		}
	}

	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值