zoukankan      html  css  js  c++  java
  • 【OpenCV学习】前后背景分离

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

    /* Demo of the background/foreground detection algorithme */
    
    #include "cv.h"
    #include "cvaux.h"
    #include "highgui.h"
    #include <ctype.h>
    #include <stdio.h>
    
    int main(int argc, char** argv)
    {
    
        /* Start capturing */
        CvCapture* capture = 0;
    
        if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])))
            capture = cvCaptureFromCAM( argc == 2 ? argv[1][0] - '0' : 0 );
        else if( argc == 2 )
            capture = cvCaptureFromAVI( argv[1] );
    
        if( !capture )
        {
            fprintf(stderr,"Could not initialize.../n");
            return -1;
        }
    
        /* print a welcome message, and the OpenCV version */
        printf ("Demo of the background classification using CvGaussBGModel %s (%d.%d.%d)/n",
            CV_VERSION,
            CV_MAJOR_VERSION, CV_MINOR_VERSION, CV_SUBMINOR_VERSION);
    
        /* Capture 1 video frame for initialization */
        IplImage* videoFrame = NULL;
        videoFrame = cvQueryFrame(capture);
    
        if(!videoFrame)
        {
            printf("Bad frame /n");
            exit(0);
        }
    
        // Create windows
        cvNamedWindow("BG", 1);
        cvNamedWindow("FG", 1);
    
        // Select parameters for Gaussian model.
        CvGaussBGStatModelParams* params = new CvGaussBGStatModelParams;                        
        params->win_size=2;    
        params->n_gauss=5;
        params->bg_threshold=0.7;
        params->std_threshold=3.5;
        params->minArea=15;
        params->weight_init=0.05;
        params->variance_init=30; 
    
        // Creat CvBGStatModel
        // cvCreateGaussianBGModel( IplImage* first_frame, CvGaussBGStatModelParams* parameters )
        // or
        // cvCreateGaussianBGModel( IplImage* first_frame )
        CvBGStatModel* bgModel = cvCreateGaussianBGModel(videoFrame ,params);
    
        int key=-1;
        while(key != 'q')
        {
            // Grab a fram
            videoFrame = cvQueryFrame(capture);
            if( !videoFrame )
                break;
            
            // Update model
            cvUpdateBGStatModel(videoFrame,bgModel);
            
            // Display results
               cvShowImage("BG", bgModel->background);
               cvShowImage("FG", bgModel->foreground);    
               key = cvWaitKey(10);
        }
    
        cvDestroyWindow("BG");
        cvDestroyWindow("FG");
        cvReleaseBGStatModel( &bgModel );
        cvReleaseCapture(&capture);
        return 0;
    }
    
    

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


                   作者:gnuhpc
                   出处:http://www.cnblogs.com/gnuhpc/
                   除非另有声明,本网站采用知识共享“署名 2.5 中国大陆”许可协议授权。


    分享到:

  • 相关阅读:
    Springboot 中AOP的使用
    ElasticSearch 聚合查询百分比
    ElasticSearch 入门
    elasticsearch 关联查询
    spring data elasticsearch多索引查询
    elasticsearch 拼音+ik分词,spring data elasticsearch 拼音分词
    es同步mysql同步-logstash
    jpa Specification复杂查询
    java Spring boot使用spring反射
    BeautifulSoup学习记录
  • 原文地址:https://www.cnblogs.com/gnuhpc/p/2807215.html
Copyright © 2011-2022 走看看