Ask Your Question
5

In BackgroundSubtractorMOG2 set background fixed(static) like BackgroundSubtractorMOG

asked 2012-12-13 11:14:15 -0600

Aravind Nambissan gravatar image

updated 2012-12-13 11:31:51 -0600

AlexanderShishkov gravatar image

Is there any way to fix the background set in BackgroundSubtractorMOG2 as in BackgroundSubtractorMOG.

heres my code to detect hand ... It does not have a fixed background

    #include<opencv2/opencv.hpp>
    #include<iostream>
    #include<vector> 
    using namespace cv; 
    using namespace std;

    const int nmixtures=10; 
    const bool shadow=false;  
    const int history =1000;

    BackgroundSubtractorMOG2 back_obj(history,nmixtures,shadow); 
    //BackgroundSubtractorMOG back_obj;  this gives me a first frame as fixed background. 
    int main(int argc,char **argv) { 

     Mat original; 
     Mat background,foreground; 
     VideoCapture cap(0); 
     std::vector<std::vector<cv::Point> >contours; 
     namedWindow("ORIGINAL"); 
     namedWindow("BACKGROUND"); 
     namedWindow("FOREGROUND"); 
     while(1)
     {
            cap>>original;  
            back_obj.operator()(original,foreground);
            back_obj.getBackgroundImage(background);    
            erode(foreground,foreground,Mat());         
            dilate(foreground,foreground,Mat());                         
            findContours(foreground,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);      
            drawContours(original,contours,-1,Scalar(255,255,0),4);     
            imshow("ORIGINAL",original);    
            imshow("BACKGROUND",background);
            imshow("FOREGROUND",foreground);    
            if(waitKey(10)>=0) 
            {
           break;
          }
     }
     return 0;
    }
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2020-11-15 04:14:32 -0600

an old question but i recently tried something like

#include "opencv2/videoio.hpp"
#include "opencv2/video.hpp"
#include "opencv2/highgui.hpp"

using namespace cv;
using namespace std;

const int nmixtures = 10;
const bool shadow = false;
const int history = 1000;

Ptr<BackgroundSubtractor> back_obj = createBackgroundSubtractorMOG2(history, nmixtures, shadow);

int main(int argc, char** argv) {

    Mat original, background, foreground;
    VideoCapture cap(0);
    vector<vector<Point> >contours;

    int counter = 0;
    double learningRate = -1.0;

    while (1)
    {
        counter++;
        if (counter < 5)
            learningRate = 1.0;
        else
            learningRate = counter > 30 ? 0.0 : -1.0;

        cap >> original;

        back_obj->apply(original, foreground, learningRate);
        back_obj->getBackgroundImage(background);

        findContours(foreground, contours, RETR_EXTERNAL, CHAIN_APPROX_NONE);
        drawContours(original, contours, -1, Scalar(255, 255, 0), 3);

        imshow("ORIGINAL", original);
        imshow("BACKGROUND", background);
        imshow("FOREGROUND", foreground);

        int key = waitKey(1);

        if (key == 32)
        {
            counter = 0;
        }

        if(key == 27)
            break;
    }
    return 0;
}
edit flag offensive delete link more

Question Tools

Stats

Asked: 2012-12-13 11:14:15 -0600

Seen: 2,367 times

Last updated: Nov 15 '20