BackgroundSubtractorMOG2 in OpenCV 3.4.1

asked 2018-05-31 01:58:30 -0600

DavidS gravatar image

updated 2018-05-31 02:02:26 -0600

LBerger gravatar image

I am working on existing project which was written in VisualStudio 2012 (C++) and OpenCV 2.4.9. Now I am rewriting it to Studio 2017 and OpenCV 3.4.1. I have the problem with class BackgroundSubtractorMOG2. In previous version of OpenCV this class was not abstract and has several member variables declared in header file. In version 3.4.1 it is abstract class and has only pure virtual methods. There exists a derived class in our project

class MotionEstimator:public BackgroundSubtractorMOG2

{
    cv::Mat bgmask;
public:
    MotionEstimator();
    virtual void SetReference(cv::Mat &frame);
    Mat process(cv::Mat &frame,vector<Rect> objects);
    void bkgdetect(cv::InputArray image, cv::OutputArray fgmask, cv::Mat mask,double learningRateBG=-1,double learningRateFG=-1);
    int width,height;
};

and it cannot be compiled, because the instance of abstract class cannot be created. I tried to rewrite this code such way :

class MotionEstimator
{
    cv::Ptr<cv::BackgroundSubtractorMOG2> pBase;
    cv::Mat bgmask;
public:
    .....
};

but there is another problem in internal methods process and bkgdetect. These methods use several member variables from old version of BackgroundSubtractorMOG2 which are not present in version 3.4.1. So it cannot be compiled again.

Old version of BackgroundSubtractorMOG2:

class CV_EXPORTS_W BackgroundSubtractorMOG2 : public BackgroundSubtractor
{
public:
    //! the default constructor
    CV_WRAP BackgroundSubtractorMOG2();
    //! the full constructor that takes the length of the history, the number of gaussian mixtures, the background ratio parameter and the noise strength
    CV_WRAP BackgroundSubtractorMOG2(int history,  float varThreshold, bool bShadowDetection=true);
    //! the destructor
    .....

protected:
    Size frameSize;
    int frameType;
    Mat bgmodel;
    Mat bgmodelUsedModes;//keep track of number of modes per pixel
    int nframes;
    int history;
    int nmixtures;
    .....

New version:

class CV_EXPORTS_W BackgroundSubtractorMOG2 : public BackgroundSubtractor
{
public:
    /** @brief Returns the number of last frames that affect the background model
    */
    CV_WRAP virtual int getHistory() const = 0;
    /** @brief Sets the number of last frames that affect the background model
    */
    CV_WRAP virtual void setHistory(int history) = 0;

    /** @brief Returns the number of gaussian components in the background model
    */
    CV_WRAP virtual int getNMixtures() const = 0;
    ...... and many other pure virtual methods ........

Have you any idea how to rewrite the code to new OpenCV 3.4.1.?

edit retag flag offensive close merge delete