openCV mit Kinect

hyunjin JANG zeigt openCV mit Kinect. Code ist im Anhang.

Video zu Source 1

Video zu Source 2

Source 1

#pragma once

#include “ofConstants.h” #include “ofTexture.h” #include “ofGraphics.h” #include “ofTypes.h”

#include “ofxThread.h”

#include #include “libfreenect.h”

class ofxKinect : public ofBaseVideo, protected ofxThread{

public :

ofxKinect(); virtual ~ofxKinect();

/// is the current frame new? bool isFrameNew() {return true;}

/// open the connection and start grabbing images bool open();

/// close the connection and stop grabbing images void close();

/// initialize resources, must be called before open() bool init(bool bTexture=true);

/// clear resources void clear();

/// get the pixels of the most recent rgb frame unsigned char * getPixels();

/// get the pixels of the most recent depth frame unsigned char * getDepthPixels(); // grey scale values unsigned short * getRawDepthPixels(); // raw 11 bit values

/// get the distance in centimeters to a given point float* getDistancePixels();

/// get the rgb texture ofTexture & getTextureReference();

/// get the greyscale depth texture ofTexture & getDepthTextureReference();

void setVerbose(bool bTalkToMe);

void setUseTexture(bool bUse); void draw(float x, float y, float w, float h); void draw(float x, float y);

void drawDepth(float x, float y, float w, float h); void drawDepth(float x, float y);

/** \brief updates the pixel buffers and textures

make sure to call this to update to the latetst incoming frames */ void update();

float getHeight(); float getWidth();

const static int width = 640; const static int height = 480;

protected:

bool bUseTexture; ofTexture depthTex; // the depth texture ofTexture rgbTex; // the RGB texture bool bVerbose; bool bGrabberInited;

unsigned char * depthPixels; unsigned char * rgbPixels;

unsigned short * depthPixelsRaw; float * distancePixels;

private:

libusb_device_handle* kinectDev; // kinect device handle

unsigned short * depthPixelsBack; // depth back unsigned char * rgbPixelsBack; // rgb back

bool bNeedsUpdate; bool bUpdateTex;

// libfreenect callbacks static void grabDepthFrame(uint16_t *buf, int width, int height); static void grabRgbFrame(uint8_t *buf, int width, int height);

// thread function void threadedFunction(); };

Source 2

/* * ofxShader.h * PointCloud * * Created by hyunjin jang on 10. 12. 4.. * Copyright 2010 __MyCompanyName__. All rights reserved. * */

//#include “ofxShader.h”

ofxShader::ofxShader() : bLoaded(false) { }

ofxShader::~ofxShader() { unload(); }

void ofxShader::setup(string shaderName) { setup(shaderName, shaderName); }

void ofxShader::setup(string vertexName, string fragmentName) { vertexName += “.vert”; fragmentName += “.frag”; setupInline(loadTextFile(vertexName), loadTextFile(fragmentName)); }

void ofxShader::compileShader(GLuint shader, string source, string type) { const char* sptr = source.c_str(); int ssize = source.size(); glShaderSource(shader, 1, &sptr, &ssize); glCompileShader(shader); }

bool ofxShader::checkShaderCompileStatus(GLuint shader, string type) { GLint status = GL_FALSE; glGetShaderiv(shader, GL_COMPILE_STATUS, &status); if(status == GL_TRUE) ofLog(OF_LOG_VERBOSE, type + ” shader compiled.”); else if (status == GL_FALSE) { ofLog(OF_LOG_ERROR, type + ” shader failed to compile.”); checkShaderInfoLog(shader, type); return false; } return true; }

bool ofxShader::checkShaderLinkStatus(GLuint shader, string type) { GLint status; glGetProgramiv(shader, GL_LINK_STATUS, &status); if(status == GL_TRUE) ofLog(OF_LOG_VERBOSE, type + ” shader linked.”); else if (status == GL_FALSE) { ofLog(OF_LOG_ERROR, type + ” shader failed to link.”); checkShaderInfoLog(shader, type); return false; } return true; }

void ofxShader::checkShaderInfoLog(GLuint shader, string type) { GLsizei infoLength; glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLength); if (infoLength > 1) { GLchar* infoBuffer = new GLchar[infoLength]; glGetShaderInfoLog(shader, infoLength, &infoLength, infoBuffer); ofLog(OF_LOG_ERROR, type + ” shader reports:\n” + infoBuffer); delete [] infoBuffer; } }

void ofxShader::checkProgramInfoLog(GLuint program) { GLsizei infoLength; glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLength); if (infoLength > 1) { GLchar* infoBuffer = new GLchar[infoLength]; glGetProgramInfoLog(program, infoLength, &infoLength, infoBuffer); string msg = “Shader program reports:\n”; ofLog(OF_LOG_ERROR, msg + infoBuffer); delete [] infoBuffer; } }

void ofxShader::setupInline(string vertexShaderSource, string fragmentShaderSource) { unload(); if (GLEE_ARB_shader_objects) { vertexShader = glCreateShader(GL_VERTEX_SHADER); fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);

compileShader(vertexShader, vertexShaderSource, “Vertex”); bool vertexCompiled = checkShaderCompileStatus(vertexShader, “Vertex”);

compileShader(fragmentShader, fragmentShaderSource, “Fragment”); bool fragmentCompiled = checkShaderCompileStatus(fragmentShader, “Fragment”);

if(vertexCompiled && fragmentCompiled) { program = glCreateProgram(); glAttachShader(program, vertexShader); glAttachShader(program, fragmentShader); glLinkProgram(program);

checkShaderLinkStatus(vertexShader, “Vertex”); checkShaderLinkStatus(fragmentShader, “Fragment”); checkProgramInfoLog(program);

bLoaded = true; } } else { cout

Einen Kommentar schreiben