Skip to content

Instantly share code, notes, and snippets.

@m9dfukc
Created April 19, 2016 07:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m9dfukc/88b0a124d6a58dcc6e167067342efcac to your computer and use it in GitHub Desktop.
Save m9dfukc/88b0a124d6a58dcc6e167067342efcac to your computer and use it in GitHub Desktop.
ofxCv example - contours follower
#include "ofApp.h"
using namespace ofxCv;
using namespace cv;
void Glow::setup(const cv::Rect& track) {
color.setHsb(ofRandom(0, 255), 255, 255);
cur = toOf(track).getCenter();
smooth = cur;
}
void Glow::update(const cv::Rect& track) {
cout << "update: " << dyingTime << endl;
cur = toOf(track).getCenter();
smooth.interpolate(cur, .5);
all.addVertex(smooth);
}
void Glow::kill() {
cout << "kill: " << dyingTime << endl;
float curTime = ofGetElapsedTimef();
if(startedDying == 0) {
startedDying = curTime;
} else if(curTime - startedDying > dyingTime) {
dead = true;
}
}
void Glow::draw() {
cout << "draw: " << dyingTime << endl;
ofPushStyle();
float size = 16;
ofSetColor(255);
if(startedDying) {
ofSetColor(ofColor::red);
size = ofMap(ofGetElapsedTimef() - startedDying, 0, dyingTime, size, 0, true);
}
ofNoFill();
ofDrawCircle(cur, size);
ofSetColor(color);
all.draw();
ofSetColor(255);
ofDrawBitmapString(ofToString(label), cur);
ofPopStyle();
}
void ofApp::setup() {
ofSetVerticalSync(true);
ofBackground(0);
movie.load("test.mov");
movie.play();
contourFinder.setMinAreaRadius(1);
contourFinder.setMaxAreaRadius(100);
contourFinder.setThreshold(80);
tracker.setPersistence(15);
tracker.setMaximumDistance(50);
}
void ofApp::update() {
movie.update();
if(movie.isFrameNew()) {
blur(movie, 10);
contourFinder.findContours(movie);
tracker.track(contourFinder.getBoundingRects());
}
vector<Glow> & followers = tracker.getFollowers();
for(int i = 0; i < followers.size(); i++) {
followers[i].dyingTime = 10;
}
}
void ofApp::draw() {
ofSetColor(255);
movie.draw(0, 0);
contourFinder.draw();
vector<Glow> & followers = tracker.getFollowers();
for(int i = 0; i < followers.size(); i++) {
followers[i].draw();
int temp_label = followers[i].getLabel();
}
}
#pragma once
#include "ofMain.h"
#include "ofxCv.h"
class Glow : public ofxCv::RectFollower {
protected:
ofColor color;
ofVec2f cur, smooth;
float startedDying;
ofPolyline all;
public:
float dyingTime;
Glow()
:startedDying(0) {
dyingTime = 0.5;
}
void setup(const cv::Rect& track);
void update(const cv::Rect& track);
void kill();
void draw();
};
class ofApp : public ofBaseApp {
public:
void setup();
void update();
void draw();
ofVideoPlayer movie;
ofxCv::ContourFinder contourFinder;
ofxCv::RectTrackerFollower<Glow> tracker;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment