summaryrefslogtreecommitdiff
path: root/test-kinect
diff options
context:
space:
mode:
authorTim Redfern <tim@eclectronics.org>2014-03-11 13:15:31 +0000
committerTim Redfern <tim@eclectronics.org>2014-03-11 13:15:31 +0000
commitc81d13d3c2d4a89d9cac3ca54cca69650ec3a52d (patch)
treed6c2145c440e5bcd202126b722b8f8cd6be4af81 /test-kinect
parentac1e8968254cf7810e84c0d90c80c94bc9dcd827 (diff)
sizing image library
Diffstat (limited to 'test-kinect')
-rw-r--r--test-kinect/Makefile13
-rw-r--r--test-kinect/addons.make2
-rw-r--r--test-kinect/config.make1
-rw-r--r--test-kinect/src/main.cpp6
-rw-r--r--test-kinect/src/ofApp.cpp324
-rw-r--r--test-kinect/src/ofApp.h73
6 files changed, 419 insertions, 0 deletions
diff --git a/test-kinect/Makefile b/test-kinect/Makefile
new file mode 100644
index 0000000..7a7fe8b
--- /dev/null
+++ b/test-kinect/Makefile
@@ -0,0 +1,13 @@
+# Attempt to load a config.make file.
+# If none is found, project defaults in config.project.make will be used.
+ifneq ($(wildcard config.make),)
+ include config.make
+endif
+
+# make sure the the OF_ROOT location is defined
+ifndef OF_ROOT
+ OF_ROOT=../../..
+endif
+
+# call the project makefile!
+include $(OF_ROOT)/libs/openFrameworksCompiled/project/makefileCommon/compile.project.mk
diff --git a/test-kinect/addons.make b/test-kinect/addons.make
new file mode 100644
index 0000000..ab4486b
--- /dev/null
+++ b/test-kinect/addons.make
@@ -0,0 +1,2 @@
+ofxKinect
+ofxOpenCv
diff --git a/test-kinect/config.make b/test-kinect/config.make
new file mode 100644
index 0000000..98bb09c
--- /dev/null
+++ b/test-kinect/config.make
@@ -0,0 +1 @@
+OF_ROOT=../../openFrameworks
diff --git a/test-kinect/src/main.cpp b/test-kinect/src/main.cpp
new file mode 100644
index 0000000..1fa9125
--- /dev/null
+++ b/test-kinect/src/main.cpp
@@ -0,0 +1,6 @@
+#include "ofApp.h"
+
+int main() {
+ ofSetupOpenGL(1600, 900, OF_WINDOW);
+ ofRunApp(new ofApp());
+}
diff --git a/test-kinect/src/ofApp.cpp b/test-kinect/src/ofApp.cpp
new file mode 100644
index 0000000..02875f2
--- /dev/null
+++ b/test-kinect/src/ofApp.cpp
@@ -0,0 +1,324 @@
+#include "ofApp.h"
+
+
+//--------------------------------------------------------------
+void ofApp::setup() {
+ ofSetLogLevel(OF_LOG_WARNING);
+
+ // enable depth->video image calibration
+ kinect.setRegistration(true);
+
+ kinect.init();
+ //kinect.init(true); // shows infrared instead of RGB video image
+ //kinect.init(false, false); // disable video image (faster fps)
+
+ kinect.open(); // opens first available kinect
+ //kinect.open(1); // open a kinect by id, starting with 0 (sorted by serial # lexicographically))
+ //kinect.open("A00362A08602047A"); // open a kinect using it's unique serial #
+
+ // print the intrinsic IR sensor values
+ if(kinect.isConnected()) {
+ ofLogNotice() << "sensor-emitter dist: " << kinect.getSensorEmitterDistance() << "cm";
+ ofLogNotice() << "sensor-camera dist: " << kinect.getSensorCameraDistance() << "cm";
+ ofLogNotice() << "zero plane pixel size: " << kinect.getZeroPlanePixelSize() << "mm";
+ ofLogNotice() << "zero plane dist: " << kinect.getZeroPlaneDistance() << "mm";
+ }
+
+
+ colourImage.allocate(kinect.width, kinect.height);
+ depthImage.allocate(kinect.width, kinect.height);
+
+ farThreshold = 70;
+
+ ofSetFrameRate(60);
+
+ // zero the tilt on startup
+ angle = 0;
+ kinect.setCameraTiltAngle(angle);
+
+ //start with the largest size square
+ //get the depth, if below a certain size, subdivide and repeat
+
+ //OR start at the smallest size, if adjacent squares are the same, work up
+
+ //1 - create a pyramid of mip maps of depth
+ //2 - work upwards
+
+ //a find layout of largest tiles
+ //b extend captured frame to this ratio
+ //c mip map this down into prepared containers
+
+ int tiles_w=ceil((float)kinect.width/(MAX_TILE_SIZE*2))*2;
+ int tiles_h=ceil((float)kinect.height/(MAX_TILE_SIZE*2))*2;
+ extend_w=(tiles_w)*MAX_TILE_SIZE;
+ extend_h=(tiles_h)*MAX_TILE_SIZE;
+
+ //get number of levels
+ levels=0;
+ for (int i=MIN_TILE_SIZE;i<=MAX_TILE_SIZE;i*=2) {
+ levels++;
+ }
+
+ colourTiles.resize(levels);
+ depthTiles.resize(levels);
+
+ for (int i=MIN_TILE_SIZE,l=0;i<=MAX_TILE_SIZE;i*=2,l++) {
+ colourTiles[l].allocate(extend_w/i,extend_h/i);
+ depthTiles[l].allocate(extend_w/i,extend_h/i);
+ cerr<<"level "<<l<<" mipmap: "<<colourTiles[l].getWidth()<<"x"<<colourTiles[l].getHeight()<<endl;
+ }
+
+ mode=MODE_COLOURTILES;
+
+ fullscreen=false;
+}
+
+//--------------------------------------------------------------
+void ofApp::update() {
+
+ ofSetWindowTitle(ofToString(ofGetFrameRate()));
+
+ kinect.update();
+
+ // there is a new frame and we are connected
+ if(kinect.isFrameNew()) {
+
+ depthImage.setFromPixels(kinect.getDepthPixels(), kinect.width, kinect.height);
+ colourImage.setFromPixels(kinect.getPixels(), kinect.width, kinect.height);
+
+ depthImage.threshold(farThreshold);
+
+ //threshold needs to be multiplied by the original
+ depthImage2.setFromPixels(kinect.getDepthPixels(), kinect.width, kinect.height);
+ depthImage*=depthImage2;
+
+ depthImage.extend(extend_w,extend_h);
+ colourImage.extend(extend_w,extend_h);
+
+ // mark pixels and texture dirty
+ depthImage.flagImageChanged();
+ colourImage.flagImageChanged();
+
+ ofxCvColorImage *prevCol=&colourImage;
+ ofxCvGrayscaleImage *prevDepth=&depthImage;
+
+ for (int i=0;i<colourTiles.size();i++){
+ colourTiles[i].scaleIntoMe(*prevCol);
+ depthTiles[i].scaleIntoMe(*prevDepth);
+ prevCol=&colourTiles[i];
+ prevDepth=&depthTiles[i];
+
+ colourTiles[i].flagImageChanged();
+ depthTiles[i].flagImageChanged();
+ }
+ }
+}
+
+void ofApp::checktile(int level,int x,int y,int size){
+ int levels_factor=128/levels;
+ if (level>0&&max(0,depthTiles[level].getPixels()[y*((int)depthTiles[level].getWidth())+x]-128)>level*levels_factor){
+ for (int i=0;i<2;i++){
+ for (int j=0;j<2;j++){
+ checktile(level-1,x*2+i,y*2+j,size/2);
+ }
+ }
+ }
+ else {
+ ofSetColor(colourTiles[level].getPixels()[(y*((int)colourTiles[level].getWidth())+x)*3],
+ colourTiles[level].getPixels()[(y*((int)colourTiles[level].getWidth())+x)*3+1],
+ colourTiles[level].getPixels()[(y*((int)colourTiles[level].getWidth())+x)*3+2]);
+ ofRect(x*size,y*size,size,size);
+ }
+}
+
+//--------------------------------------------------------------
+void ofApp::draw() {
+
+ ofBackground(0,0,0);
+
+ ofSetColor(255, 255, 255);
+
+ int pixelsize=ofGetWidth()/colourTiles[levels-1].getWidth();
+
+
+ switch(mode){
+ case MODE_COLOURTILES:
+
+ //recursively draw pixels
+
+ //int yoffset=
+
+ for (int i=0;i<colourTiles[levels-1].getWidth();i++){
+ for (int j=0;j<colourTiles[levels-1].getHeight();j++){
+ checktile(levels-1,i,j,pixelsize);
+ }
+ }
+ break;
+
+ case MODE_DEPTH:
+
+ depthImage.draw(0,0,ofGetWidth(),ofGetHeight());
+ break;
+
+ case MODE_COMPONENTS:
+
+ depthImage.draw(0,0, 640,480);
+ colourImage.draw(640,0, 640,480);
+
+ int xoffs=10;
+ for (int i=0;i<depthTiles.size();i++){
+ depthTiles[i].draw(xoffs,500,depthTiles[i].getWidth(),depthTiles[i].getHeight());
+ colourTiles[i].draw(xoffs+640,500,colourTiles[i].getWidth(),colourTiles[i].getHeight());
+ xoffs+=(colourTiles[i].getWidth()+10);
+ }
+ break;
+
+ }
+
+
+ /*
+
+
+
+
+
+ */
+
+
+ /*
+
+ // draw from the live kinect
+ kinect.drawDepth(10, 10, 400, 300);
+ kinect.draw(420, 10, 400, 300);
+
+ depthImage.draw(10, 320, 400, 300);
+
+
+ // draw instructions
+ ofSetColor(255, 255, 255);
+ stringstream reportStream;
+
+ reportStream << "press p to switch between images and point cloud, rotate the point cloud with the mouse" << endl
+ << "set far threshold " << farThreshold
+ << ", fps: " << ofGetFrameRate() << endl
+ << "press c to close the connection and o to open it again, connection is: " << kinect.isConnected() << endl;
+
+ if(kinect.hasCamTiltControl()) {
+ reportStream << "press UP and DOWN to change the tilt angle: " << angle << " degrees" << endl
+ << "press 1-5 & 0 to change the led mode" << endl;
+ }
+
+ ofDrawBitmapString(reportStream.str(), 20, 652);
+
+ */
+
+}
+
+
+
+//--------------------------------------------------------------
+void ofApp::exit() {
+ kinect.setCameraTiltAngle(0); // zero the tilt on exit
+ kinect.close();
+
+}
+
+//--------------------------------------------------------------
+void ofApp::keyPressed (int key) {
+ switch (key) {
+
+ case '>':
+ case '.':
+ farThreshold ++;
+ if (farThreshold > 255) farThreshold = 255;
+ break;
+
+ case '<':
+ case ',':
+ farThreshold --;
+ if (farThreshold < 0) farThreshold = 0;
+ break;
+
+
+ case 'w':
+ kinect.enableDepthNearValueWhite(!kinect.isDepthNearValueWhite());
+ break;
+
+ case 'o':
+ kinect.setCameraTiltAngle(angle); // go back to prev tilt
+ kinect.open();
+ break;
+
+ case 'c':
+ kinect.setCameraTiltAngle(0); // zero the tilt
+ kinect.close();
+ break;
+
+ case '1':
+ kinect.setLed(ofxKinect::LED_GREEN);
+ break;
+
+ case '2':
+ kinect.setLed(ofxKinect::LED_YELLOW);
+ break;
+
+ case '3':
+ kinect.setLed(ofxKinect::LED_RED);
+ break;
+
+ case '4':
+ kinect.setLed(ofxKinect::LED_BLINK_GREEN);
+ break;
+
+ case '5':
+ kinect.setLed(ofxKinect::LED_BLINK_YELLOW_RED);
+ break;
+
+ case '0':
+ kinect.setLed(ofxKinect::LED_OFF);
+ break;
+
+ case OF_KEY_UP:
+ angle++;
+ if(angle>30) angle=30;
+ kinect.setCameraTiltAngle(angle);
+ break;
+
+ case OF_KEY_DOWN:
+ angle--;
+ if(angle<-30) angle=-30;
+ kinect.setCameraTiltAngle(angle);
+ break;
+
+ case OF_KEY_LEFT:
+ mode--;
+ if (mode<0) mode==NUM_MODES-1;
+ break;
+
+ case OF_KEY_RIGHT:
+ mode=(mode+1)%NUM_MODES;
+ break;
+
+ case ' ':
+ fullscreen=!fullscreen;
+ ofSetFullscreen(fullscreen);
+ break;
+
+ }
+}
+
+//--------------------------------------------------------------
+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)
+{}
diff --git a/test-kinect/src/ofApp.h b/test-kinect/src/ofApp.h
new file mode 100644
index 0000000..45cecdb
--- /dev/null
+++ b/test-kinect/src/ofApp.h
@@ -0,0 +1,73 @@
+#pragma once
+
+#include "ofMain.h"
+#include "ofxOpenCv.h"
+#include "ofxKinect.h"
+
+#define MIN_TILE_SIZE 4 //has to be a divisor of 320 and 240 as the tiles are centred
+#define MAX_TILE_SIZE 32 //has to be a power of 2 * MIN_TILE_SIZE
+
+#define MODE_COLOURTILES 0
+#define MODE_DEPTH 1
+#define MODE_COMPONENTS 2
+
+#define NUM_MODES 3
+
+#define STORE_SIZE 256
+
+class offsetCvColorImage : public ofxCvColorImage {
+ public:
+ void extend( int w, int h ) {
+ IplImage* temp = cvCreateImage( cvSize(w,h), IPL_DEPTH_8U, 3 );
+ cvCopyMakeBorder(cvImage,temp, cvPoint(0,0),IPL_BORDER_REPLICATE );
+ allocate( w, h );
+ cvCopy( temp, cvImage );
+ cvReleaseImage( &temp );
+ }
+};
+
+class offsetCvGrayscaleImage : public ofxCvGrayscaleImage {
+ public:
+ void extend( int w, int h ) {
+ IplImage* temp = cvCreateImage( cvSize(w,h), IPL_DEPTH_8U, 1 );
+ cvCopyMakeBorder(cvImage,temp, cvPoint(0,0),IPL_BORDER_REPLICATE );
+ allocate( w, h );
+ cvCopy( temp, cvImage );
+ cvReleaseImage( &temp );
+ }
+};
+
+class ofApp : public ofBaseApp {
+public:
+
+ void setup();
+ void update();
+ void draw();
+ void exit();
+
+ void keyPressed(int key);
+ 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 checktile(int level,int x,int y,int size);
+
+ ofxKinect kinect;
+
+ offsetCvColorImage colourImage;
+ offsetCvGrayscaleImage depthImage,depthImage2;
+
+ vector<offsetCvColorImage> colourTiles;
+ vector<offsetCvGrayscaleImage> depthTiles;
+ vector<pair<int,int> > numTiles;
+
+ int farThreshold;
+ int angle;
+ int levels;
+ int extend_w,extend_h;
+
+ int mode;
+
+ bool fullscreen;
+};