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;
}



  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: `BackgroundSubtractorMOG2` 是 OpenCV 库中一个用于背景建模的算法,可以实现前景对象的检测。 该算法通过对连续帧之间的像素值进行建模,来计算当前帧中的前景物体。该算法可以处理光照变化和阴影等复杂情况,是常用的背景建模算法之一。在使用该算法时,需要通过对一定数量的帧进行建模来获取背景图像,然后再对新的帧进行处理以获取前景物体。 在使用该算法时,需要设置一些参数,包括高斯混合模型数量、背景建模的学习率、前景阈值等,以达到最佳的效果。 ### 回答2: BackgroundSubtractorMOG2是一种常用于视频分析和目标检测的背景建模算法。该算法是基于混合高斯模型(GMM)的背景建模方法的改进版本。 在开始运行时,BackgroundSubtractorMOG2需要通过调用createBackgroundSubtractorMOG2()函数来创建一个BackgroundSubtractorMOG2对象。然后,该对象可以用来对输入视频序列进行背景建模和前景检测。 Algorithm类中的几个重要参数可以用来控制算法的性能和结果。首先,history参数用于定义背景模型更新的时间,即算法会使用过去几帧的图像来更新背景模型。接下来,varThreshold参数用于控制像素变化的阈值,即像素与背景模型之间的差异超过这个阈值才会被视为前景。另外,varThresholdGen参数用于控制背景建模中的模型是否需要进行更新。这些参数可以根据具体的场景和需求进行调整,以获得更好的效果。 使用BackgroundSubtractorMOG2进行背景建模和前景检测的步骤如下:首先,将图像序列的每一帧输入给BackgroundSubtractorMOG2对象的apply()函数进行处理。然后,该函数会返回一个二值图像,其中前景像素为白色,背景像素为黑色。可以使用opencv等图像处理工具来进一步处理和提取前景。 需要注意的是,BackgroundSubtractorMOG2算法对于光照变化较大的场景可能不够稳健,而且在处理高动态范围图像时可能会出现一些问题。因此,在实际应用中,我们可能需要进一步调整算法的参数或者结合其他算法来获得更准确的结果。 总结来说,BackgroundSubtractorMOG2是一种常用的背景建模算法,可以用于视频分析和目标检测。它基于混合高斯模型,通过对图像序列进行建模和前景检测来实现目标的提取。 ### 回答3: BackgroundSubtractorMOG2是一个用于可变背景建模的算法,它是OpenCV计算机视觉库中的一部分。 它是基于高斯混合模型(GMM)的背景建模方法。通过对每个像素的颜色建模,它可以检测出与背景不同的前景对象。该算法适用于静态摄像头下的背景建模和目标检测,如视频监控、行人检测等领域。 BackgroundSubtractorMOG2算法的原理是对像素颜色的变化进行建模,将每个像素分解为多个高斯分布,从而建立一个高斯混合模型。在该模型中,每个分量都表示背景的一种颜色。通过不断更新模型的参数,它可以逐渐适应变化的背景。 当新的帧输入进来时,算法会将每个像素与其对应的背景模型进行比较。如果像素的颜色与任何一个分量的均值差异超过一个阈值,那么它将被认为是前景。通过这种方式,算法能够检测出视频序列中的前景对象。 BackgroundSubtractorMOG2算法通过调整模型的学习率和阈值等参数,可以适应不同场景和光照条件的变化。同时,它还提供了一些额外的功能,如运动检测、帧间背景差分等。 需要注意的是,BackgroundSubtractorMOG2算法对于复杂的场景和快速运动的目标可能存在一定的误检率和漏检率。因此,在实际应用中需要根据具体情况进行参数调整和算法优化,以提高准确性和稳定性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值