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?