Skip to content

Instantly share code, notes, and snippets.

@genekogan
Created May 14, 2015 00:19
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 genekogan/e780af0a50e1b71d47fd to your computer and use it in GitHub Desktop.
Save genekogan/e780af0a50e1b71d47fd to your computer and use it in GitHub Desktop.
code examples from SFPC 5.13
#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup(){
kinect.setup();
kinect.addDepthGenerator();
kinect.setDepthColoring(COLORING_GREY);
kinect.start();
grayImage.allocate(kinect.getWidth(), kinect.getHeight());
grayThreshNear.allocate(kinect.getWidth(), kinect.getHeight());
grayThreshFar.allocate(kinect.getWidth(), kinect.getHeight());
nearThreshold.set("near thresh", 11, 0, 255);
farThreshold.set("far thresh", 0, 0, 255);
minArea.set("min area", 1000, 0, 5000);
maxArea.set("max area", 70000, 0, 150000);
threshold.set("threshold", 10, 0, 100);
persistence.set("persistence", 10, 0, 100);
maxDistance.set("max distance", 10, 0, 100);
skip.set("skip points", 20, 1, 30);
gui.setup("kinect contours");
gui.add(nearThreshold);
gui.add(farThreshold);
gui.add(minArea);
gui.add(maxArea);
gui.add(threshold);
gui.add(persistence);
gui.add(maxDistance);
gui.add(skip);
}
//--------------------------------------------------------------
void ofApp::update(){
kinect.update();
if (kinect.isNewFrame()) {
// after this, grayImage contains near&far thresholded depth pixels
grayImage.setFromPixels(kinect.getDepthRawPixels());
grayThreshNear = grayImage;
grayThreshFar = grayImage;
grayThreshNear.threshold(nearThreshold, true);
grayThreshFar.threshold(farThreshold);
cvAnd(grayThreshNear.getCvImage(), grayThreshFar.getCvImage(), grayImage.getCvImage(), NULL);
grayImage.flagImageChanged();
// find contours from grayImage
contourFinder.setMinArea(minArea);
contourFinder.setMaxArea(maxArea);
contourFinder.setThreshold(threshold);
contourFinder.getTracker().setPersistence(persistence);
contourFinder.getTracker().setMaximumDistance(maxDistance);
contourFinder.findContours(grayImage);
}
}
//--------------------------------------------------------------
void ofApp::draw(){
//grayImage.draw(0, 0);
ofPushStyle();
ofPushMatrix();
ofBackground(0);
ofScale((float)ofGetWidth()/640.0f, (float)ofGetHeight()/480.0f);
int numContours = contourFinder.size();
for (int i=0; i<numContours; i++) {
vector<cv::Point> points = contourFinder.getContour(i);
for (int k=0; k<5; k++) {
ofNoFill();
ofSetColor(ofRandom(255), ofRandom(255), ofRandom(255));
ofSetLineWidth(4);
ofBeginShape();
for (int j=5*k; j<points.size(); j+=skip) {
ofCurveVertex(points[j].x, points[j].y);
}
ofEndShape();
}
}
ofSetColor(255);
ofPopStyle();
ofPopMatrix();
gui.draw();
}
//--------------------------------------------------------------
void ofApp::keyPressed(int key){
}
//--------------------------------------------------------------
void ofApp::keyReleased(int key){
}
//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::windowResized(int w, int h){
}
//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){
}
//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){
}
#pragma once
#include "ofMain.h"
#include "ofxOpenNI.h"
#include "ofxOpenCv.h"
#include "ofxCv.h"
#include "ofxGui.h"
class ofApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
void keyPressed(int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
ofxOpenNI kinect;
ofxCvGrayscaleImage grayImage;
ofxCvGrayscaleImage grayThreshNear;
ofxCvGrayscaleImage grayThreshFar;
ofxCv::ContourFinder contourFinder;
ofParameter<float> nearThreshold;
ofParameter<float> farThreshold;
ofParameter<float> minArea;
ofParameter<float> maxArea;
ofParameter<float> threshold;
ofParameter<float> persistence;
ofParameter<float> maxDistance;
ofParameter<int> skip;
ofxPanel gui;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment