Object of abstract class type “cv::BackgroundSubtr

2019-05-15 12:55发布

问题:

I develop the code in VS2015 + OpenCV3.0 in Windows 7 64bit. This is a demo code that I want have a try. And I have tried many demo but I was coming across the same problem:

object of abstract class type "cv::BackgroundSubtractorMOG2" is not allowed. all the methods are pure virtual function.

The demo code is:

using namespace cv;
using namespace std;
int main() {
    VideoCapture video("1.avi");
    Mat frame, mask, thresholdImage, output;
    //video>>frame;
    Ptr<BackgroundSubtractor> pMOG2;
    pMOG2 = new BackgroundSubtractorMOG2();
    BackgroundSubtractorMOG2 bgSubtractor(20, 16, true);
    while (true) {
        video >> frame;
        ++frameNum;
        bgSubtractor(frame, mask, 0.001);
        cout << frameNum << endl;
        //imshow("mask",mask);
        //waitKey(10);
    }
    return 0;
}

I include a lot of heaerd files but I still can not use the class BackgroundSubtractorMOG2 and what is worse, the class of BackgroundSubtractorMOG is shown undeclared.

回答1:

Syntax has changed from OpenCV 2.9.X. This will work in OpenCV 3.0.0:

#include <opencv2\opencv.hpp>

using namespace cv;
using namespace std;
int main() {
    VideoCapture video("1.avi");
    Mat frame, mask, thresholdImage, output;
    int frameNum = 0;

    Ptr<BackgroundSubtractor> pMOG2 = createBackgroundSubtractorMOG2(20, 16, true);
    while (true) {
        video >> frame;
        ++frameNum;
        pMOG2->apply(frame, mask, 0.001);

        cout << frameNum << endl;
        imshow("mask",mask);
        waitKey(10);
    }
    return 0;
}