一直没搞懂CvBGStatModel和CvFGDStatModel有什么区别。CvBGStatModel模型的创建用cvCreateGaussianBGModel,CvFGDStatModel模型的创建用cvCreateFGDStatModel,它们的更新都用cvUpdateBGStatModel。
#include<iostream> #include<opencv2/opencv.hpp> #include<opencv/cvaux.hpp> using namespace std; int main() { IplImage *pFrame=NULL; IplImage *pFrImg=NULL; IplImage *pBkImg=NULL; CvCapture *pCapture=NULL; pCapture= cvCreateFileCapture("video.avi"); cvNamedWindow("video",1); cvNamedWindow("background",1); cvNamedWindow("foreground",1); cvMoveWindow("video",30,0); cvMoveWindow("background",450,0); cvMoveWindow("foreground",900,0); CvFGDStatModel* fg_model=NULL; int nFrmNum=0; 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,3); fg_model = (CvFGDStatModel*)cvCreateFGDStatModel(pFrame, 0); } else { cvUpdateBGStatModel(pFrame, (CvBGStatModel *)fg_model ); pFrImg=fg_model->foreground; cvDilate(pFrImg,pFrImg); // cvErode(pFrImg,pFrImg);//腐蚀 pBkImg=fg_model->background; cvShowImage("video",pFrame); cvShowImage("background",pBkImg); cvShowImage("foreground",pFrImg); } if(cvWaitKey(20)>=0) break; } cvReleaseBGStatModel((CvBGStatModel**)&fg_model); cvDestroyAllWindows(); cvReleaseImage(&pFrame); cvReleaseImage(&pFrImg); cvReleaseImage(&pBkImg); cvReleaseCapture(&pCapture); return 0; }
程序运行结果: