Ask Your Question

Error with createBackgroundSubtractor with OpenCV 3.1 and extra module contrib

asked 2016-04-14 07:47:10 -0600

Florence gravatar image

Hello !

I just installed opencv 3.1, and I wanted to use the function createBackgroundSubtractorMOG. I saw on this post that it was moved to an extra module, so I installed it ( post : ), but I still have a problem. Indeed when I compile, I have the following error :
"error: ‘createBackgroundSubtractorMOG’ was not declared in this scope"

And when I try to write : bgsegm::createBackgroundSubtractorMOG, I got : undefined reference tocv::bgsegm::createBackgroundSubtractorMOG(int, int, double, double)

If someone has a idea why I still can't use this function, that would be very helpful !

Here is the code (it is actually the code from , where I changed MOG2 to MOG)

Thank you very much !


    #include "opencv2/imgcodecs.hpp"
    #include "opencv2/imgproc.hpp"
    #include "opencv2/videoio.hpp"
    #include <opencv2/highgui.hpp>
    #include <opencv2/video.hpp>
    #include <opencv2/core/ocl.hpp>
  #include <opencv2/bgsegm.hpp>
  #include <opencv2/core.hpp>
    #include <stdio.h>
    #include <iostream>
    #include <sstream>
    using namespace cv;
    using namespace std;
    // Global variables
    Mat frame; //current frame
    Mat fgMaskMOG; //fg mask fg mask generated by MOG method
    Ptr<BackgroundSubtractor> pMOG; //MOG Background subtractor
    int keyboard; //input from keyboard
    void help();
    void processVideo(char* videoFilename);
    void processImages(char* firstFrameFilename);
    void help()
        << "--------------------------------------------------------------------------" << endl
        << "This program shows how to use background subtraction methods provided by "  << endl
        << " OpenCV. You can process both videos (-vid) and images (-img)."             << endl
                                                                                        << endl
        << "Usage:"                                                                     << endl
        << "./bs {-vid <video filename>|-img <image filename>}"                         << endl
        << "for example: ./bs -vid video.avi"                                           << endl
        << "or: ./bs -img /data/images/1.png"                                           << endl
        << "--------------------------------------------------------------------------" << endl
        << endl;

        int main(int argc, char* argv[])

    //print help information

        // desactivate the parallelism on the gpu

        //check for the input parameter correctness
        if(argc != 3) {
            cerr <<"Incorret input list" << endl;
            cerr <<"exiting..." << endl;
            return EXIT_FAILURE;
        //create GUI windows
        namedWindow("FG Mask MOG 2");
        //create Background Subtractor objects
        pMOG = createBackgroundSubtractorMOG(); //MOG approach
        if(strcmp(argv[1], "-vid") == 0) {
            //input data coming from a video
        else if(strcmp(argv[1], "-img") == 0) {
            //input data coming from a sequence of images
        else {
            //error in reading input parameters
            cerr <<"Please, check the input parameters." << endl;
            cerr <<"Exiting..." << endl;
            return EXIT_FAILURE;
        //destroy GUI windows
        return EXIT_SUCCESS;
    void processVideo(char* videoFilename) {
        //create the capture object
        VideoCapture capture(videoFilename);
            //error in opening the video input
            cerr << "Unable to open video file: " << videoFilename << endl;
        //read input data. ESC or 'q' for quitting
        while( (char)keyboard != 'q' && (char)keyboard != 27 ){
            //read the current frame
            if(! {
                cerr << "Unable to read next frame." << endl;
                cerr << "Exiting..." << endl;
            //update the background model
            pMOG->apply(frame, fgMaskMOG,1);
            //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(CAP_PROP_POS_FRAMES);
            string frameNumberString = ss.str();
            putText(frame, frameNumberString ...
edit retag flag offensive close merge delete

2 answers

Sort by » oldest newest most voted

answered 2016-04-15 00:04:00 -0600

berak gravatar image

bgsegm::createBackgroundSubtractorMOG() is the correct code to use. your "undefined reference" error is from the linker, you will have to add opencv_bgsegm310.lib to your linker libs list

edit flag offensive delete link more


Great it works like a charm ! Thank you very much !

Florence gravatar imageFlorence ( 2016-04-15 03:18:43 -0600 )edit

answered 2016-08-31 15:34:10 -0600

mhaghighat gravatar image

If you want the MOG to work, you should

#include "opencv2/bgsegm.hpp"

and then

pMOG = bgsegm::createBackgroundSubtractorMOG();
edit flag offensive delete link more

Question Tools

1 follower


Asked: 2016-04-14 07:47:10 -0600

Seen: 3,145 times

Last updated: Apr 15 '16