Skip to content

Instantly share code, notes, and snippets.

@antimodular
Created April 18, 2016 17:01
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 antimodular/fc91334f61af3793413c71359a4f0dff to your computer and use it in GitHub Desktop.
Save antimodular/fc91334f61af3793413c71359a4f0dff to your computer and use it in GitHub Desktop.
#include "ofApp.h"
int main() {
ofSetupOpenGL(320, 240, OF_WINDOW);
ofRunApp(new ofApp());
}
#include "ofApp.h"
using namespace ofxCv;
using namespace cv;
//const float dyingTime = 1;
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) {
ofLog()<<"dyingTime in update "<<dyingTime;
cur = toOf(track).getCenter();
smooth.interpolate(cur, .5);
all.addVertex(smooth);
}
void Glow::kill() {
float curTime = ofGetElapsedTimef();
if(startedDying == 0) {
startedDying = curTime;
} else if(curTime - startedDying > dyingTime) {
dead = true;
}
}
void Glow::draw() {
ofLog()<<"dyingTime in draw "<<dyingTime;
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("video.mov");
movie.play();
contourFinder.setMinAreaRadius(1);
contourFinder.setMaxAreaRadius(100);
contourFinder.setThreshold(15);
// wait for half a frame before forgetting something
tracker.setPersistence(15);
// an object can move up to 50 pixels per frame
tracker.setMaximumDistance(50);
}
void ofApp::update() {
movie.update();
if(movie.isFrameNew()) {
blur(movie, 10);
contourFinder.findContours(movie);
tracker.track(contourFinder.getBoundingRects());
}
}
void ofApp::draw() {
ofSetColor(255);
movie.draw(0, 0);
contourFinder.draw();
followers.clear();
followers = tracker.getFollowers();
for(int i = 0; i < followers.size(); i++) {
followers[i].dyingTime = 0.33;
followers[i].draw();
int temp_label = followers[i].getLabel();
ofLog()<<temp_label<<" : "<<tracker.getAge(temp_label);
}
}
#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 = 1;
Glow()
:startedDying(0) {
}
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;
vector<Glow> followers;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment