如何使用BackgroundSubtractor?

时间:2015-10-18 11:35:40

标签: c++ opencv

我对图像处理和OpenCV都很陌生。我尝试在C ++中使用BackgroundSubtractorMOG来检测对象。

这是代码。

//opencv
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/video/background_segm.hpp>
//C
#include <stdio.h>
//C++
#include <iostream>
#include <sstream>

using namespace cv;
using namespace std;

//global variables
Mat frame; //current frame
Mat resizeF;
Mat fgMaskMOG; //fg mask generated by MOG method
Ptr<BackgroundSubtractor> pMOG; //MOG Background subtractor
int keyboard;

void processVideo(char* videoFilename);

int main() {
    //create GUI windows
    namedWindow("Frame");
    namedWindow("FG Mask MOG");    

    pMOG= new BackgroundSubtractorMOG(); //MOG approach

    VideoCapture capture("F:/FFOutput/CCC- 18AYU1F.flv");

    if(!capture.isOpened()) {
        cerr << "Unable to open video file " << endl;
        exit(EXIT_FAILURE);
    }

    while( (char)keyboard != 'q' && (char)keyboard != 27 ){
        //read the current frame
        if(!capture.read(frame)) {
            cerr << "Unable to read next frame." << endl;
            cerr << "Exiting..." << endl;
            exit(EXIT_FAILURE);
        }

        pMOG->operator()(frame, fgMaskMOG);

        imshow("Frame", frame);
        imshow("FG Mask MOG", fgMaskMOG);

        keyboard = waitKey( 30 );
    }
    capture.release();

    destroyAllWindows();
    return EXIT_SUCCESS;    
}

代码工作正常,但我的fgMaskMOG并没有随着时间的推移而发展,我的意思是,减法器似乎并没有了解背景中的内容。 提供模型的第一帧似乎被视为永久背景。

我该如何解决这个问题?

0 个答案:

没有答案