Last active
April 18, 2016 22:10
-
-
Save antimodular/9a5f9ab720d3e0993c8fe48d3d5df40a to your computer and use it in GitHub Desktop.
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" | |
int main() { | |
ofSetupOpenGL(320, 240, OF_WINDOW); | |
ofRunApp(new ofApp()); | |
} |
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; | |
//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() { | |
ofLog()<<"dyingTime in kill "<<dyingTime; | |
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); | |
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() { | |
myMat = Mat(500,500,CV_8UC3,Scalar(0,0,0)); | |
cv::circle(myMat, cv::Point(mouseX,mouseY), 10, cvScalar(255,255,255),-1, 8, 0); | |
contourFinder.findContours(myMat); | |
tracker.track(contourFinder.getBoundingRects()); | |
vector<Glow> & followers = tracker.getFollowers(); | |
for(auto v:followers) { | |
v.dyingTime = 5.11; | |
// v.update(); | |
v.kill(); | |
} | |
} | |
void ofApp::draw() { | |
ofSetColor(255); | |
drawMat(myMat, 0, 0); | |
contourFinder.draw(); | |
vector<Glow> & followers = tracker.getFollowers(); | |
for(auto v:followers) { | |
v.dyingTime = 5.11; | |
v.draw(); | |
int temp_label = v.getLabel(); | |
ofLog()<<temp_label<<" : "<<tracker.getAge(temp_label); | |
} | |
} |
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" | |
using namespace ofxCv; | |
using namespace cv; | |
class Glow : public ofxCv::RectFollower { | |
protected: | |
ofColor color; | |
ofVec2f cur, smooth; | |
float startedDying; | |
ofPolyline all; | |
public: | |
float dyingTime = 10; | |
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(); | |
ofxCv::ContourFinder contourFinder; | |
ofxCv::RectTrackerFollower<Glow> tracker; | |
// vector<Glow> & followers; | |
Mat myMat; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment