Sign in to follow this  
Eliad Moshe

A Kinect Wrapper

Recommended Posts

Hi!

I made a Kinect Wrapper on top of the CL NUI api.
It supports a built in interface to OpenCV as well.
(I used the folowing snippet as a reference := [url="http://codelaboratories.com/forums/viewthread/518/"]Link[/url] ).


[code]
// Kinect.h
#ifndef KINECT
#define KINECT

#pragma comment (lib, "CLNUIDevice.lib")
#pragma comment (lib, "cv210.lib")
#pragma comment (lib, "cxcore210.lib")
#pragma comment (lib, "highgui210.lib")

#include <cv.h>
#include <cxcore.h>
#include <highgui.h>

#include <CLNUIDevice.h>
#include <Windows.h>





class Kinect{

protected:

signed DeviceCount ;
char * DeviceSerial ;
CLNUIMotor Motor ;
CLNUICamera Camera ;
bool IsCamStarted ;
PDWORD rgb32_data;
IplImage * rgb32 ;
PDWORD depth32_data ;
IplImage *depth32 ;


public:
Kinect ();
~Kinect ();
bool SetLed(unsigned State);
bool SetPosition(signed Position);
bool GetPosition(short &x, short &y, short &z);
IplImage * GetImage();
DWORD * GetPixelBuffer();

IplImage * GetDepthImage();
DWORD * GetDepthBuffer();



};


#endif
[/code]


[code]
// Kinect.cpp
#include "Kinect.h"


Kinect :: Kinect ()
: DeviceCount(GetNUIDeviceCount())
, DeviceSerial(GetNUIDeviceSerial(0))
, Motor(CreateNUIMotor(DeviceSerial))
, Camera(CreateNUICamera(DeviceSerial))
, IsCamStarted(StartNUICamera(Camera))
, rgb32_data (new DWORD [640*480])
, rgb32 (cvCreateImageHeader(cvSize(640,480),8,4))
, depth32_data (new DWORD [640*480])
, depth32 (cvCreateImageHeader(cvSize(640,480),8,4))
{
}

bool Kinect :: SetLed(unsigned State){return SetNUIMotorLED( Motor, State );}
bool Kinect :: SetPosition(signed Position){return SetNUIMotorPosition(Motor,Position);}


bool Kinect :: GetPosition(short &x, short &y, short &z){return GetNUIMotorAccelerometer(this->Motor, x,y,z);


}

Kinect :: ~Kinect (){
DestroyNUIMotor(Motor);
StopNUICamera(this->Camera);
DestroyNUICamera(this->Camera);

delete [] rgb32_data ;
delete [] depth32_data ;

cvReleaseImageHeader(&this->rgb32);
cvReleaseImageHeader(&this->depth32);
}


IplImage * Kinect :: GetImage(){
cvReleaseImageHeader(&this->rgb32);
GetNUICameraColorFrameRGB32(this->Camera, this->rgb32_data ) ;
// for(int i = 0 ; i < 480 ; i++ )for(int j = 0 ; j < 640 ; j++ )this->rgb32_data[640*i+j]= 0xFF00FF ;
this->rgb32 = cvCreateImageHeader(cvSize(640,480), 8, 4);
cvSetData(this->rgb32, this->rgb32_data, rgb32->widthStep);
return &*rgb32 ;
}



DWORD * Kinect :: GetPixelBuffer(){
GetNUICameraColorFrameRGB32(this->Camera, this->rgb32_data ) ;
return &*rgb32_data ;
}




IplImage * Kinect :: GetDepthImage(){
cvReleaseImageHeader(&this->depth32);
GetNUICameraDepthFrameRGB32(this->Camera, this->depth32_data ) ;
// for(int i = 0 ; i < 480 ; i++ )for(int j = 0 ; j < 640 ; j++ )this->rgb32_data[640*i+j]= 0xFF00FF ;
this->depth32 = cvCreateImageHeader(cvSize(640,480), 8, 4);
cvSetData(this->depth32, this->depth32_data, rgb32->widthStep);
return &*depth32 ;
}



DWORD * Kinect :: GetDepthBuffer(){
GetNUICameraDepthFrameRGB32(this->Camera, this->depth32_data ) ;
return &*depth32_data ;
}


[/code]


[code]

#include "Kinect.h"

void main(){

cvNamedWindow( "A", CV_WINDOW_AUTOSIZE );

Kinect * k = new Kinect();

// k->SetLed(7);
//k->SetPosition(0);
do{
IplImage * frame = k->GetDepthImage();
cvShowImage( "A", frame );

cvWaitKey(1);

} while (!GetAsyncKeyState(0x50));

delete k ;



}


}
[/code]


Hope it will help someone, Have Fun! :)






Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this