BackgroundSubtractorMOG2 & OpenCV

Victor Lamoine picture Victor Lamoine · Apr 7, 2013 · Viewed 15k times · Source

I'm trying to compile the simple example at http://mateuszstankiewicz.eu/?p=189 I'm running Ubuntu 12.10 64 bits.I use OpenCV 2.4.4a

I compile using a makefile that does this :

g++ background_subtraction.cpp -o background_subtraction -I/usr/local/include/opencv -I/usr/local/include/opencv2 -L /usr/local/lib -lm -lopencv_core -lopencv_highgui -lopencv_imgproc -lcvblob

The errors :

/tmp/cc0ZWnll.o: dans la fonction « main »:
background_subtraction.cpp:(.text+0x96): référence indéfinie vers « cv::BackgroundSubtractorMOG2::BackgroundSubtractorMOG2() »
background_subtraction.cpp:(.text+0x1f0): référence indéfinie vers « cv::BackgroundSubtractorMOG2::operator()(cv::_InputArray const&, cv::_OutputArray const&, double) »
background_subtraction.cpp:(.text+0x222): référence indéfinie vers « cv::BackgroundSubtractorMOG2::getBackgroundImage(cv::_OutputArray const&) const »
background_subtraction.cpp:(.text+0x61a): référence indéfinie vers « cv::BackgroundSubtractorMOG2::~BackgroundSubtractorMOG2() »
background_subtraction.cpp:(.text+0x7a6): référence indéfinie vers « cv::BackgroundSubtractorMOG2::~BackgroundSubtractorMOG2() »
collect2: erreur: ld a retourné 1 code d'état d'exécution

Means : undefined reference to ...

The background_subtraction.cpp file :

#include <opencv2/opencv.hpp>

#include <iostream>
#include <vector>

int
main (int argc, char *argv[])
{
  cv::Mat frame;
  cv::Mat back;
  cv::Mat fore;
  cv::VideoCapture cap (0);
  cv::BackgroundSubtractorMOG2 bg;
  bg.set ("nmixtures", 3);
  //bg.bShadowDetection = false;
  std::vector < std::vector < cv::Point > >contours;

  cv::namedWindow ("Frame");
  cv::namedWindow ("Background");

  for (;;)
    {
      cap >> frame;
      bg.operator()(frame, fore);
      bg.getBackgroundImage (back);
      cv::erode (fore, fore, cv::Mat ());
      cv::dilate (fore, fore, cv::Mat ());
      cv::findContours (fore, contours, CV_RETR_EXTERNAL,
            CV_CHAIN_APPROX_NONE);
      cv::drawContours (frame, contours, -1, cv::Scalar (0, 0, 255), 2);
      cv::imshow ("Frame", frame);
      cv::imshow ("Background", back);
      if (cv::waitKey (30) >= 0)
    break;
    }
  return 0;
}

What am I missing to compile this program succesfully ? Thanks :)

Answer

Andrey Kamaev picture Andrey Kamaev · Apr 7, 2013

You also need to link opencv_video