BackgroundSubstract

asked 2014-05-23 07:30:15 -0500

Junglee gravatar image

updated 2014-05-23 07:36:34 -0500

Hi All,

i have tried below example to subtract Image's background, its working well and updates position of the object but for the first time i mean when camera starts if i move an object from its initial position to some other position, its initial position Blob is not getting erased. i have attached image for your reference, what am i missing to clear that Initial Position of Original object. Here's my code:

//opencv
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/video/background_segm.hpp>
#include <opencv2/video/video.hpp>
//C++
#include <iostream>
#include <sstream>
//namespace
using namespace cv;
using namespace std;

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

//function declarations
void processVideo();

int main(int argc, char* argv[])
{
//create GUI windows
namedWindow("Frame");
namedWindow("FG Mask MOG");
namedWindow("FG Mask MOG 2");

//create Background Subtractor objects
pMOG = new BackgroundSubtractorMOG(); //MOG approach
pMOG2 = new BackgroundSubtractorMOG2(); //MOG2 approach

//input data coming from a video
processVideo();

//destroy GUI windows
destroyAllWindows();
return EXIT_SUCCESS;
}

void processVideo() 
{
//create the capture object
VideoCapture capture;
capture.open(0);
if(!capture.isOpened()){
    //error in opening the video input
    cerr << "Unable to open video Camera " << endl;
    exit(EXIT_FAILURE);
}
//read input data. ESC or 'q' for quitting
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);
    }
    //update the background model
    pMOG->operator()(frame, fgMaskMOG);
    pMOG2->operator()(frame, fgMaskMOG2);
    //get the frame number and write it on the current frame
    stringstream ss;
    rectangle(frame, cv::Point(10, 2), cv::Point(100,20),cv::Scalar(255,255,255), -1);
    ss << capture.get(CV_CAP_PROP_POS_FRAMES);
    string frameNumberString = ss.str();
    putText(frame, frameNumberString.c_str(), cv::Point(15, 15),
                    FONT_HERSHEY_SIMPLEX, 0.5 , cv::Scalar(0,0,0));
    //show the current frame and the fg masks
    imshow("Frame", frame);
    imshow("FG Mask MOG", fgMaskMOG);
    imshow("FG Mask MOG 2", fgMaskMOG2);
    //get the input from the keyboard
    keyboard = waitKey( 30 );
}
//delete capture object
capture.release();
}

Here's my Image:

image description

edit retag flag offensive close merge delete

Comments

You probably need to wait until the background model adapts to the new appearance there. Or you may want to use a different background modeling approach. Perhaps a simple thresholded absolute difference with an empty scene is enough.

isarandi gravatar imageisarandi ( 2014-05-23 12:09:43 -0500 )edit

@isarandi, I didn't get you can you please elaborate.

Junglee gravatar imageJunglee ( 2014-05-25 23:46:31 -0500 )edit

@Junglee What is your application about, what is your goal? How long did you wait for it to disappear?

isarandi gravatar imageisarandi ( 2014-05-26 06:17:04 -0500 )edit