Skip to content

Commit

Permalink
added a basic blobtracker
Browse files Browse the repository at this point in the history
  • Loading branch information
ben committed Nov 16, 2010
1 parent c96d231 commit 144c26b
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 24 deletions.
4 changes: 0 additions & 4 deletions src/SimpleThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,22 @@ class SimpleThread:public ofxRuiThread{
ofxCvGrayscaleImage grayDiff;
ofxCvContourFinder contourFinder;
bool bhasNewImage;
bool bhasFinishedFinding;

SimpleThread(){
grayDiff.setUseTexture(false);
grayDiff.allocate(320,240);
bhasNewImage = false;
bhasFinishedFinding = false;
}

void updateThread(){
if(bhasNewImage) {
contourFinder.findContours(grayDiff, 20, (340*240)/3, 10, true);
bhasFinishedFinding = true;
bhasNewImage = false;
}
}

void setImage(ofxCvGrayscaleImage _grayDiff) {
grayDiff = _grayDiff;
//bhasFinishedFinding = false;
bhasNewImage = true;
}

Expand Down
83 changes: 66 additions & 17 deletions src/testApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
//--------------------------------------------------------------
void testApp::setup(){

cam_1.loadMovie("fingers.mov");
cam_1.loadMovie("fingers.mpg");
cam_1.play();

cam_2.loadMovie("fingers.mov");
cam_2.loadMovie("fingers.mpg");
cam_2.play();

colorImg_1.allocate(320,240);
Expand All @@ -23,8 +23,53 @@ void testApp::setup(){
bLearnBakground = true;
threshold = 80;

thread_1.initAndSleep();
thread_1.start();
thread_2.start();

ID = 0;
}

void testApp::trackBlobs(vector<ofxCvBlob> _blobs) {

ofxVec2f _b;
ofxVec2f b;
for (int i = 0; i < _blobs.size(); i++){
bool bIsNewBlob = true;
for (int j = 0; j < blobs.size(); j++){
_b.set( _blobs[i].centroid );
b.set(blobs[j].x, blobs[j].y);
//cout << b.distance(_b) << endl;
if(b.distance(_b) < 30) {
bIsNewBlob = false;
blobs[i].frame = ofGetFrameNum();
blobs[i].pX = blobs[i].x;
blobs[i].pY = blobs[i].y;
blobs[i].x = _b.x;
blobs[i].y = _b.y;
}
}

if(bIsNewBlob) {
trackedBlob tB;
tB.ID = ID; ID++;
tB.frame = ofGetFrameNum();
tB.x = _blobs[i].centroid.x;
tB.y = _blobs[i].centroid.y;
tB.pX = _blobs[i].centroid.x;
tB.pY = _blobs[i].centroid.y;
tB.framesAlive = 0;
blobs.push_back(tB);
}
}

//kill all blobs which weren't updated since the last 5 frames
for (int j = 0; j < blobs.size(); j++){
if(blobs[j].frame < ofGetFrameNum() - 5 ) {
blobs.erase(blobs.begin() + j);
} else {
blobs[j].framesAlive += 1;
}
}
}

//--------------------------------------------------------------
Expand Down Expand Up @@ -58,11 +103,11 @@ void testApp::update(){
// also, find holes is set to true so we will get interior contours as well....
//contourFinder.findContours(grayDiff, 20, (340*240)/3, 10, true); // find holes
thread_1.setImage(grayDiff_1);

blobs_1 = thread_1.getBlobs();

}

trackBlobs(blobs_1);

if (bNewFrame_2){

colorImg_2.setFromPixels(cam_2.getPixels(), 320,240);
Expand All @@ -85,7 +130,6 @@ void testApp::update(){
blobs_2 = thread_2.getBlobs();

}

}

//--------------------------------------------------------------
Expand Down Expand Up @@ -118,19 +162,30 @@ void testApp::draw(){

// or, instead we can draw each blob individually,
// this is how to get access to them:
for (int i = 0; i < blobs_1.size(); i++){
blobs_1[i].draw(360,540);
}
//for (int i = 0; i < blobs_1.size(); i++){
// blobs_1[i].draw(360,540);
// ofCircle(blobs_1[i].centroid.x + 360,blobs_1[i].centroid.y +540,5);
//}

for (int i = 0; i < blobs_2.size(); i++){
blobs_2[i].draw(1040,540);
}

ofTranslate(360,540);
for (int i = 0; i < blobs.size(); i++){
// pos += (targetPos - pos) * SPEED;
ofSetHexColor(0xffffff);
cout << blobs[i].ID << endl;
if(blobs[i].framesAlive > 10) {
ofLine(blobs[i].x,blobs[i].y,blobs[i].pX,blobs[i].pY);
ofCircle(blobs[i].x,blobs[i].y,5);
}
}
ofTranslate(-360,-540);
// finally, a report:

ofSetHexColor(0xffffff);
char reportStr[1024];
sprintf(reportStr, "bg subtraction and blob detection\npress ' ' to capture bg\nthreshold %i (press: +/-)\nfps: %f", threshold, ofGetFrameRate());
sprintf(reportStr, "bg subtraction and blob detection\npress ' ' to capture bg\nthreshold %i (press: +/-)\ntracked Blob count: %i\nfps: %f", threshold, blobs.size(), ofGetFrameRate());
ofDrawBitmapString(reportStr, 20, 600);
}

Expand All @@ -150,12 +205,6 @@ void testApp::keyPressed (int key){
threshold --;
if (threshold < 0) threshold = 0;
break;
case 't':
thread_1.beginUpdate();
break;
case 'z':
thread_1.pauseUpdate();
break;
}
}

Expand Down
21 changes: 18 additions & 3 deletions src/testApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,33 @@

#include "ofxOpenCv.h"
#include "SimpleThread.h"
#include "ofxVectorMath.h"

//#define _USE_LIVE_VIDEO // uncomment this to use a live camera
// otherwise, we'll use a movie file


struct trackedBlob
{
int frame;
int framesAlive;
float height;
int ID;
float pX;
float pY;
float width;
float x;
float y;
};

class testApp : public ofBaseApp{

public:

void setup();
void update();
void draw();
void trackBlobs(vector<ofxCvBlob> _blobs);

void keyPressed (int key);
void mouseMoved(int x, int y );
Expand All @@ -38,9 +53,9 @@ class testApp : public ofBaseApp{

SimpleThread thread_1, thread_2;

vector<ofxCvBlob> blobs, blobs_1, blobs_2;


vector<ofxCvBlob> blobs_1, blobs_2;
vector<trackedBlob> blobs;
int ID;
};

#endif

0 comments on commit 144c26b

Please sign in to comment.