Foreground / Background Segmentation with OpenCV

testApp.h

  
#ifndef _TEST_APP  
#define _TEST_APP  
  
#include "ofMain.h"  
  
#include "ofxOpenCv.h"  
#include "cv.h"  
#include "cvaux.h"  
  
  
class testApp : public ofBaseApp{  
  
	public:  
  
		void setup();  
		void update();  
		void draw();  
  
		void keyPressed  (int key);  
		void mouseMoved(int x, int y );  
		void mouseDragged(int x, int y, int button);  
		void mousePressed(int x, int y, int button);  
		void mouseReleased(int x, int y, int button);  
		void resized(int w, int h);  
  
  
		ofVideoGrabber 		vidGrabber;  
  
        ofxCvColorImage		colorImg;  
  
	  
	CvBGStatModel* gauss_bgModel;  
	ofxCvGrayscaleImage		gauss_foregroundImg;  
	ofxCvColorImage		gauss_backgroundImg;  
	  
	CvBGStatModel* fgd_bgModel;  
	ofxCvGrayscaleImage		fgd_foregroundImg;  
	ofxCvColorImage		fgd_backgroundImg;  
  
};  
  
#endif  

testApp.cpp

  
#include "testApp.h"  
  
  
//--------------------------------------------------------------  
void testApp::setup(){  
  
  
	vidGrabber.setVerbose(true);  
	vidGrabber.initGrabber(320,240);  
  
    colorImg.allocate(320,240);  
  
	gauss_foregroundImg.allocate(320,240);  
	gauss_backgroundImg.allocate(320,240);  
	  
	fgd_foregroundImg.allocate(320,240);  
	fgd_backgroundImg.allocate(320,240);  
  
    CvGaussBGStatModelParams* params = new CvGaussBGStatModelParams;						  
    params->win_size=2;	  
    params->n_gauss=5;  
    params->bg_threshold=0.7;  
    params->std_threshold=3.5;  
    params->minArea=15;  
    params->weight_init=0.05;  
    params->variance_init=30;   
	  
    //bgModel = cvCreateGaussianBGModel(colorImg.getCvImage() ,params);  
	gauss_bgModel = cvCreateGaussianBGModel(colorImg.getCvImage());  
	  
	fgd_bgModel = cvCreateFGDStatModel(colorImg.getCvImage());  
}  
  
//--------------------------------------------------------------  
void testApp::update(){  
	ofBackground(100,100,100);  
  
  
	vidGrabber.grabFrame();  
  
  
	if (vidGrabber.isFrameNew()){  
  
		colorImg.setFromPixels(vidGrabber.getPixels(), 320,240);  
		  
		// Update models  
		cvUpdateBGStatModel(colorImg.getCvImage() ,gauss_bgModel);  
		gauss_foregroundImg = gauss_bgModel->foreground;  
		gauss_backgroundImg = gauss_bgModel->background;  
		  
		cvUpdateBGStatModel(colorImg.getCvImage() ,fgd_bgModel);  
		fgd_foregroundImg = fgd_bgModel->foreground;  
		fgd_backgroundImg = fgd_bgModel->background;  
	}	  
}  
  
//--------------------------------------------------------------  
void testApp::draw(){  
  
	ofSetColor(0xffffff);  
	colorImg.draw(20,20);  
  
	gauss_foregroundImg.draw(360,20);  
	gauss_backgroundImg.draw(360,280);  
	  
	fgd_foregroundImg.draw(700,20);  
	fgd_backgroundImg.draw(700,280);  
  
}  
  
  
//--------------------------------------------------------------  
void testApp::keyPressed  (int key){  
  
  
}  
  
//--------------------------------------------------------------  
void testApp::mouseMoved(int x, int y ){  
}  
  
//--------------------------------------------------------------  
void testApp::mouseDragged(int x, int y, int button){  
}  
  
//--------------------------------------------------------------  
void testApp::mousePressed(int x, int y, int button){  
}  
  
//--------------------------------------------------------------  
void testApp::mouseReleased(int x, int y, int button){  
  
}  
  
//--------------------------------------------------------------  
void testApp::resized(int w, int h){  
  
}  

greetings ascorbin

Nice example! Thanks alot. I always do it all manual, so nice to see it can be done more automatic…

I use the same code and try to run, but i have this error: undefined reference to cvCreateGausssianBGModel and cvCreateFGDStatModel,

Anyone can help for this?

I got the same error related to cvCreateGausssianBGModel and cvCreateFGDStatModel. But once I link in the libcvaux.a, it works. I have Windows oF with code::blocks. And it is:
Project -> Build Options -> Linker settings, add …\addons\ofxOpenCv\libs\opencv\lib\win_cb\libcvaux.a

Hope that help.

–Oze