Object of abstract class type “cv::BackgroundSubtractorMOG2” is not allowed. all the methods are pure virtual

坚强是说给别人听的谎言 提交于 2019-12-07 14:02:59

问题


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;
}


来源:https://stackoverflow.com/questions/32530024/object-of-abstract-class-type-cvbackgroundsubtractormog2-is-not-allowed-all

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!