1

I am quite new to image processing and OpenCV. I've tried using BackgroundSubtractorMOG in C++ to detect objects.

Here is the code.

//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;    
}

The code works fine but my fgMaskMOG do not evolve through time, I mean, the subtractor doesnt seem to learn what is in the background. The first frame which feed the model seems to be taken as a permanent background.

How could I fix this problem?

Andrew Komiagin
  • 6,446
  • 1
  • 13
  • 23
Kani
  • 1,735
  • 2
  • 11
  • 12
  • 1
    What OpenCV version are you using? Things are different from OpenCV 2.X and OpenCV 3.x – Miki Oct 18 '15 at 13:25
  • Possible duplicate of [Object of abstract class type "cv::BackgroundSubtractorMOG2" is not allowed. all the methods are pure virual](http://stackoverflow.com/questions/32530024/object-of-abstract-class-type-cvbackgroundsubtractormog2-is-not-allowed-all) – Miki Oct 18 '15 at 13:29
  • I'm using OpenCV 2.4.11 – Kani Oct 18 '15 at 14:32
  • 1
    Then check the example [here](http://stackoverflow.com/a/31432220/5008845) – Miki Oct 18 '15 at 14:35
  • Now it is working thank you very much – Kani Oct 18 '15 at 14:59
  • can you tell me when we use BackgroundSubtractorMOG for moving objects, if that object stop for few second then it also consider as a background – Kani Oct 18 '15 at 15:08
  • 1
    You con modify (increase) the history parameters used to update the model, or if the object is still for a while you should follow updates to [this](http://stackoverflow.com/q/33018856/5008845) question – Miki Oct 18 '15 at 15:16

0 Answers0