//2.0教材代码
#include <iostream>
#include <cv.h>
#include <highgui.h>
#include "cvaux.h"
using namespace std;
using namespace cv;
int main( int argc, char** argv )
{
VideoCapture cap(0);
if(!cap.isOpened())return 0;
Mat frame,foreground;
namedWindow("提取的前景",CV_WINDOW_AUTOSIZE);
BackgroundSubtractorMOG mog;
bool stop(false);
while(!stop)
{
if(!cap.read(frame))break;
mog(frame,foreground,0.09);//更新背景并返回前景
threshold(foreground,foreground,128,255,THRESH_BINARY_INV);
imshow("原视频", frame);
imshow("提取的前景", foreground);
if(waitKey(10) >= 0) stop=true;
}
return 0;
}
/*
//中文网站的代码
#include <stdio.h>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include <cvaux.h>//必须引此头文件
int main( int argc, char** argv )
{
IplImage* pFrame = NULL;
IplImage* pFrImg = NULL;
IplImage* pBkImg = NULL;
CvCapture* pCapture = cvCaptureFromAVI("2.avi");
int nFrmNum = 0;
cvNamedWindow("video", 1);
cvNamedWindow("background",1);
cvNamedWindow("foreground",1);
cvMoveWindow("video", 30, 0);
cvMoveWindow("background", 360, 0);
cvMoveWindow("foreground", 690, 0);
//初始化高斯混合模型参数
CvGaussBGModel* bg_model=NULL;
while(pFrame = cvQueryFrame( pCapture ))
{
nFrmNum++;
if(nFrmNum == 1)
{
pBkImg = cvCreateImage(cvSize(pFrame->width, pFrame->height), IPL_DEPTH_8U,3);
pFrImg = cvCreateImage(cvSize(pFrame->width, pFrame->height), IPL_DEPTH_8U,1);
//高斯背景建模,pFrame可以是多通道图像也可以是单通道图像
//cvCreateGaussianBGModel函数返回值为 CvBGStatModel*,
//需要强制转换成CvGaussBGModel*
bg_model = (CvGaussBGModel*)cvCreateGaussianBGModel(pFrame, 0);
}
else
{
//更新高斯模型
cvUpdateBGStatModel(pFrame, (CvBGStatModel *)bg_model );
//pFrImg为前景图像,只能为单通道
//pBkImg为背景图像,可以为单通道或与pFrame通道数相同
cvCopy(bg_model->foreground,pFrImg,0);
cvCopy(bg_model->background,pBkImg,0);
//把图像正过来
pBkImg->origin=1;
pFrImg->origin=1;
cvShowImage("video", pFrame);
cvShowImage("background", pBkImg);
cvShowImage("foreground", pFrImg);
if( cvWaitKey(2) >= 0 )
break;
}
}
//释放高斯模型参数占用内存
cvReleaseBGStatModel((CvBGStatModel**)&bg_model);
cvDestroyWindow("video");
cvDestroyWindow("background");
cvDestroyWindow("foreground");
cvReleaseImage(&pFrImg);
cvReleaseImage(&pBkImg);
cvReleaseCapture(&pCapture);
return 0;
}
*/
opencv实践程序7——混合高斯算法
最新推荐文章于 2024-09-30 19:51:52 发布