Skip to content

Instantly share code, notes, and snippets.

@hagino3000
Created March 6, 2011 09:54
Show Gist options
  • Save hagino3000/857176 to your computer and use it in GitHub Desktop.
Save hagino3000/857176 to your computer and use it in GitHub Desktop.
Transfer depth data from ofxOpenNI to ofOpenCV
#ifndef _NEOCOS
#define _NEOCOS
#include "ofMain.h"
#include "ofxOpenNI.h"
#include "myDepthGenerator.h"
class neocos : 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);
ofxOpenNIContext recordContext;
ofxDepthGenerator recordDepth;
ofxImageGenerator recordImage;
ofxUserGenerator recordUser, playUser;
ofxOpenNIRecorder oniRecorder;
unsigned char * depth_pixels;
ofxCvGrayscaleImage grayImage;
};
#endif
#include "neocos.h"
//--------------------------neocos------------------------------------
void neocos::setup(){
ofBackground(0, 0, 0);
ofSetFrameRate(30);
recordContext.setup();
recordContext.setupUsingXMLFile();
recordDepth.setup(&recordContext);
recordImage.setup(&recordContext);
recordUser.setup(&recordContext, &recordDepth, &recordImage);
recordDepth.toggleRegisterViewport(&recordImage);
recordContext.toggleMirror();
oniRecorder.setup(&recordContext, &recordDepth, &recordImage);
XnMapOutputMode map_mode;
map_mode.nXRes = XN_VGA_X_RES;
map_mode.nYRes = XN_VGA_Y_RES;
map_mode.nFPS = 30;
depth_pixels = new unsigned char[map_mode.nXRes * map_mode.nYRes * 4];
memset(depth_pixels, 0, map_mode.nXRes * map_mode.nYRes * 4 * sizeof(unsigned char));
grayImage.allocate(640, 480);
}
//--------------------------------------------------------------
void neocos::update(){
recordContext.update();
recordUser.update();
xn::DepthGenerator& dgen = recordDepth.getXnDepthGenerator();
xn::DepthMetaData dmd;
dgen.GetMetaData(dmd);
const XnDepthPixel* depth = dmd.Data();
float max_depth = dgen.GetDeviceMaxDepth();
int pos = 0;
for (XnUInt16 y = dmd.YOffset(); y < dmd.YRes() + dmd.YOffset(); y++) {
for (XnUInt16 x = 0; x < dmd.XRes(); x++, depth++){
pos++;
depth_pixels[pos] = (XnUInt16)(((*depth) / (max_depth / 256)));
}
}
grayImage.setFromPixels(depth_pixels, 640, 480);
}
//--------------------------------------------------------------
void neocos::draw(){
ofSetColor(255, 255, 255);
//recordDepth.draw(0,0,640,480);
//grayImage.draw(0, 0);
grayImage.draw(0, 0);
recordUser.draw();
recordImage.draw(640, 0, 640, 480);
glEnable(GL_BLEND);
glBlendFunc(GL_DST_COLOR, GL_ZERO);
recordUser.drawUserMasks(640, 0);
glDisable(GL_BLEND);
}
//--------------------------------------------------------------
void neocos::keyPressed(int key){
}
//--------------------------------------------------------------
void neocos::keyReleased(int key){
}
//--------------------------------------------------------------
void neocos::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void neocos::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void neocos::mousePressed(int x, int y, int button){
}
//--------------------------------------------------------------
void neocos::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void neocos::windowResized(int w, int h){
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment