Skip to content

Instantly share code, notes, and snippets.

@stephanschulz
Created October 12, 2023 19:25
Show Gist options
  • Save stephanschulz/ce4326fac903d888e454f412fefc8068 to your computer and use it in GitHub Desktop.
Save stephanschulz/ce4326fac903d888e454f412fefc8068 to your computer and use it in GitHub Desktop.
#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;
myVar = 0;
}
void Glow::update(const cv::Rect& track) {
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() {
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);
myVar++;
ofDrawBitmapString(ofToString(label)+" "+ofToString(myVar), 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();
if(showOther == true){
followers_0 = &tracker.getFollowers();
for(int i = 0; i < followers_0->size(); i++) {
(*followers_0)[i].draw();
}
ofSetColor(255);
ofDrawBitmapString("show pointer", 10, 10);
}else{
followers_1 = tracker.getFollowers();
for(int i = 0; i < followers_1.size(); i++) {
followers_1[i].draw();
}
ofSetColor(255);
ofDrawBitmapString("show vector", 10, 10);
}
}
//--------------------------------------------------------------
void ofApp::keyPressed(int key){
}
//--------------------------------------------------------------
void ofApp::keyReleased(int key){
if(key == 's'){
showOther = !showOther;
}
}
#pragma once
#include "ofMain.h"
#include "ofxCv.h"
class Glow : public ofxCv::RectFollower {
protected:
ofColor color;
ofVec3f cur, smooth;
float startedDying;
ofPolyline all;
public:
Glow()
:startedDying(0) {
}
float myVar;
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();
void keyPressed (int key);
void keyReleased(int key);
ofVideoPlayer movie;
ofxCv::ContourFinder contourFinder;
ofxCv::RectTrackerFollower<Glow> tracker;
vector<Glow>* followers_0;
vector<Glow> followers_1;
bool showOther = false;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment