Category Archives: OpenCV

Compiling bgslibrary demo program

Before beginning make sure you have OpenCV >= 2.4 installed.

Copy the following demo code into main.cpp:

#include <iostream>
#include <cv.h>
#include <highgui.h>

#include “package_bgs/FrameDifferenceBGS.h”

int main(int argc, char **argv)
{
CvCapture *capture = 0;
capture = cvCaptureFromCAM(0);

if(!capture){
std::cerr << “Cannot initialize video!” << std::endl;
return -1;
}

IBGS *bgs;
bgs = new FrameDifferenceBGS;

IplImage *frame;
while(1)
{
frame = cvQueryFrame(capture);
if(!frame) break;

cv::Mat img_input(frame);
cv::imshow(“Input”, img_input);

cv::Mat img_mask;
cv::Mat img_bkgmodel;

// by default, it shows automatically the foreground mask image
bgs->process(img_input, img_mask, img_bkgmodel);

//if(!img_mask.empty())
// cv::imshow(“Foreground”, img_mask);
// do something

if(cvWaitKey(33) >= 0)
break;
}

delete bgs;

cvDestroyAllWindows();
cvReleaseCapture(&capture);

return 0;
}

Copy package_bgs directory from bgslibrary into directory containing main.cpp file and create a new folder named config in the same directory.

Compile

g++ -c `pkg-config opencv –cflags` main.cpp package_bgs/FrameDifferenceBGS.cpp

Check library paths

pi@raspberrypi ~ $ cat /etc/ld.so.conf.d/opencv.conf
/usr/local/lib/

pi@raspberrypi ~ $ sudo ldconfig -v

Link

g++ -o out `pkg-config opencv –libs` main.o FrameDifferenceBGS.o

Execute

./out