Created
April 19, 2016 07:47
-
-
Save m9dfukc/88b0a124d6a58dcc6e167067342efcac to your computer and use it in GitHub Desktop.
ofxCv example - contours follower
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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