【OpenCV学习】前后背景分离

作者:gnuhpc 
出处:http://www.cnblogs.com/gnuhpc/

  1. /* Demo of the background/foreground detection algorithme */  
  2.   
  3. #include "cv.h"  
  4. #include "cvaux.h"  
  5. #include "highgui.h"  
  6. #include <ctype.h>  
  7. #include <stdio.h>  
  8.   
  9. int main(int argc, char** argv)  
  10. {  
  11.   
  12.     /* Start capturing */  
  13.     CvCapture* capture = 0;  
  14.   
  15.     if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])))  
  16.         capture = cvCaptureFromCAM( argc == 2 ? argv[1][0] - '0' : 0 );   //从摄像头获取
  17.     else if( argc == 2 )  
  18.         capture = cvCaptureFromAVI( argv[1] );  //从磁盘中的avi视频获取
  19.   
  20.     if( !capture )  
  21.     {  
  22.         fprintf(stderr,"Could not initialize.../n");  
  23.         return -1;  
  24.     }  
  25.   
  26.     /* print a welcome message, and the OpenCV version */  
  27.     printf ("Demo of the background classification using CvGaussBGModel %s (%d.%d.%d)/n",  
  28.         CV_VERSION,  
  29.         CV_MAJOR_VERSION, CV_MINOR_VERSION, CV_SUBMINOR_VERSION);  
  30.   
  31.     /* Capture 1 video frame for initialization */  
  32.     IplImage* videoFrame = NULL;  
  33.     videoFrame = cvQueryFrame(capture);  
  34.   
  35.     if(!videoFrame)  
  36.     {  
  37.         printf("Bad frame /n");  
  38.         exit(0);  
  39.     }  
  40.   
  41.     // Create windows  
  42.     cvNamedWindow("BG", 1);  
  43.     cvNamedWindow("FG", 1);  
  44.   
  45.     // Select parameters for Gaussian model.  
  46.     CvGaussBGStatModelParams* params = new CvGaussBGStatModelParams;                          
  47.     params->win_size=50;     //修改为:50  
  48.     params->n_gauss=3;       //修改为:3
  49.     params->bg_threshold=0.7;  
  50.     params->std_threshold=3.5;  
  51.     params->minArea=15;  
  52.     params->weight_init=0.333;  //修改为:1/3
  53.     params->variance_init=30;   
  54.   
  55.     // Creat CvBGStatModel  
  56.     // cvCreateGaussianBGModel( IplImage* first_frame, CvGaussBGStatModelParams* parameters )  
  57.     // or  
  58.     // cvCreateGaussianBGModel( IplImage* first_frame )  
  59.     CvBGStatModel* bgModel = cvCreateGaussianBGModel(videoFrame ,params);  
  60.   
  61.     int key=-1;  
  62.     while(key != 'q')  
  63.     {  
  64.         // Grab a fram  
  65.         videoFrame = cvQueryFrame(capture);  
  66.         if( !videoFrame )  
  67.             break;  
  68.           
  69.         // Update model  
  70.         cvUpdateBGStatModel(videoFrame,bgModel);  
  71.           
  72.         // Display results  
  73.            cvShowImage("BG", bgModel->background);  
  74.            cvShowImage("FG", bgModel->foreground);      
  75.            key = cvWaitKey(10);  
  76.     }  
  77.   
  78.     cvDestroyWindow("BG");  
  79.     cvDestroyWindow("FG");  
  80.     cvReleaseBGStatModel( &bgModel );  
  81.     cvReleaseCapture(&capture);  
  82.     return 0;  
  83. }  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值