OpenCv自带的前景检测方法

1.opencv2.3版本内自带的基于混合高斯背景建模的目标跟踪代码


#include "opencv2/video/background_segm.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdio.h>
#include <highgui.h>
#include <cv.h>
using namespace cv;

void help()
{
	printf("\nDo background segmentation, especially demonstrating the use of cvUpdateBGStatModel().\n"
		"Learns the background at the start and then segments.\n"
		"Learning is togged by the space key. Will read from file or camera\n"
		"Call:\n"
		"./  bgfg_segm [file name -- if no name, read from camera]\n\n");
}

//this is a sample for foreground detection functions
int main(int argc, char** argv)
{
	VideoCapture cap;
	bool update_bg_model = true;

	if( argc < 2 )
		cap.open(0);
	else
		cap.open(argv[1]);
	help();

	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);
		//erode(fgimg,fgimg,0);
		//dilate(fgimg,fgimg,0);
		
	    int nl = fgmask.rows;
		int nc = fgmask.cols * fgmask.channels();
		 
		//遍历图像的每个像素
		for(int j=0; j<nl ;++j)
		{
			uchar *data = fgmask.ptr<uchar>(j);
			for(int i=0; i<nc; ++i)
			{
				data[i] = 255-data[i];
			}
		}



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

2.OpenCV1.0的代码为


#include "cvaux.h"
#include "highgui.h"
#include <stdio.h>

//this is a sample for foreground detection functions
int main(int argc, char** argv)
{
	IplImage*       tmp_frame = NULL;
	CvCapture*      cap = NULL;
	
	if( argc < 2 )
	{
		printf("please specify video file name \n");
		exit(0);
	}

	//cap = cvCaptureFromFile(argv[1]);
	cap=cvCreateCameraCapture(0);
	tmp_frame = cvQueryFrame(cap);
	if(!tmp_frame)
	{
		printf("bad video \n");
		exit(0);
	}

	cvNamedWindow("BG", 1);
	cvNamedWindow("FG", 1);

	//create BG model
	CvBGStatModel* bg_model = cvCreateFGDStatModel( tmp_frame );

	for( int fr = 1;tmp_frame; tmp_frame = cvQueryFrame(cap), fr++ )
	{
		double t = (double)cvGetTickCount();
		cvUpdateBGStatModel( tmp_frame, bg_model );
		t = (double)cvGetTickCount() - t;
		printf( "%.1f\n", t/(cvGetTickFrequency()*1000.) );
		cvShowImage("BG", bg_model->background);
		cvShowImage("FG", bg_model->foreground);
		char k = cvWaitKey(5);
		if( k == 27 ) break;
		//printf("frame# %d \r", fr);
	}


	cvReleaseBGStatModel( &bg_model );
	cvReleaseCapture(&cap);
	
	return 0;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值