1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
| //openni library for acquering data from kinect
#include <XnOpenNI.h>
#include <XnCppWrapper.h>
#include <XnVNite.h>
//opencv libraries
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include <iostream>
#define SAMPLE_XML_PATH "c:\\openni.xml"
#define X_SIZE 640 //this is how wide my window should be
#define Y_SIZE 480 //this is how long my window should be
#define DEPTH_THRESH_FAR 5000 //all depth info beyond this point (in mm) will be removed
#define DEPTH_THRESH_NEAR 0 //all depth info before this point will be removed
#define MAX_COLOR 255 //max color that can be used
#define COLOR_ZONES 20
//--------------------------------------- Globals --------------------------------
XnStatus rc; //to check status of various open-ni reld operations
xn::Context cxt; //context - represents the sensor (source of image / depth info)
xn::DepthGenerator depthGen; //will hold the depth generator node
xn::DepthMetaData depthMapMetaData; //will hold the meta data for the depth generator node
xn::ImageGenerator imageGen; //will hold the image generator node
xn::ImageMetaData imageMapMetaData; //will hold the meta data for the image generator node
/*************************************** NITE OBJECTS *********************************/
XnVSessionManager *sessionMgr; //this is a pointer to a session manager object
XnVPointControl *pointCtrl; //this is a pointer to a point control object
bool inSession = false; //this variable indicates if we're in session on not
XnPoint3D handPointCoords; //this variable will store the coordinates of the primary hand point
/***********************************************************************************
Callback routines for various NITE objects. These are invoked by the respective NITE
objects when various events occur
************************************************************************************/
void XN_CALLBACK_TYPE sessionStart(const XnPoint3D& ptPosition, void* UserCxt); //session started event callback
void XN_CALLBACK_TYPE sessionEnd(void* UserCxt); //session ended event callback
void XN_CALLBACK_TYPE pointCreate(const XnVHandPointContext *pContext, const XnPoint3D &ptFocus, void *cxt); //point created callback
void XN_CALLBACK_TYPE pointUpdate(const XnVHandPointContext *pContext, void *cxt); //point updated callback
void XN_CALLBACK_TYPE pointDestroy(XnUInt32 nID, void *cxt); //point destroyed callback
//----------------------------------------------------------------------------
/**********************************************************************************
Macro for nullifying the global variable to track hand point. I'm using negative values
as indications that the point is no longer valid.
**********************************************************************************/
#define nullifyHandPoint(){ \
handPointCoords.X = -1; \
handPointCoords.Y = -1; \
handPointCoords.Z = -1; \
}
/*********************************************************************************
This is simple function that allows us to check if the global hand point is valid
or not
**********************************************************************************/
inline bool isHandPointNull(){
if(handPointCoords.X == -1) return true;
else return false;
}
/*
* Main routine
*/
int main(int argc,char *argv[])
{
//------------------ Context initializations go in here ------------------
xn::EnumerationErrors errors;
rc = cxt.InitFromXmlFile(SAMPLE_XML_PATH,&errors); //initialize the context from the xml file
if(rc != XN_STATUS_OK)
{
cout << "Failed to open:" << xnGetStatusString(rc) << endl; //handle error in reading from XML
return rc;
}
rc = cxt.FindExistingNode(XN_NODE_TYPE_DEPTH,depthGen); //try to find the depth node from the context
if(rc != XN_STATUS_OK)
{ //handle error if node is not found
cout << "Failed to open Depth node!" << endl;
return rc;
}
rc=cxt.FindExistingNode(XN_NODE_TYPE_IMAGE,imageGen); //try to find the raw image from the context
if(rc != XN_STATUS_OK)
{ //handle error if node is not found
cout << "Failed to open Image node!" << endl;
return rc;
}
//-------------------- Init Nite objects -----------------------
sessionMgr = new XnVSessionManager(); //session manager is created
rc = sessionMgr->Initialize(&cxt,"Click,Wave","RaiseHand"); //session manager is initialized
if(rc!= XN_STATUS_OK){ //check if this init operation was good
printf("Session manager couldn't be initialized");
return rc;
}
sessionMgr->RegisterSession(&cxt,sessionStart,sessionEnd); //register the callbacks for the session manager
pointCtrl = new XnVPointControl("Point Tracker"); //create the point control object
pointCtrl->RegisterPrimaryPointCreate(&cxt,pointCreate); //register the primary point created handler
pointCtrl->RegisterPrimaryPointUpdate(&cxt,pointUpdate); //register the primary point updated handler
pointCtrl->RegisterPrimaryPointDestroy(&cxt,pointDestroy); //register the primary point destroyed handler
sessionMgr->AddListener(pointCtrl); //make the session manager listen to the point control object
nullifyHandPoint(); //initialize the global variable to track hand points
//------------------- opencv init ------------------------
cvNamedWindow("Depth", CV_WINDOW_AUTOSIZE);
cvNamedWindow("Raw Image", CV_WINDOW_AUTOSIZE);
IplImage *depth_image = cvCreateImage(cvSize(X_SIZE,Y_SIZE),8,1);
IplImage *raw_image = cvCreateImage(cvSize(X_SIZE,Y_SIZE),8,3);
//running routine
while(1)
{
rc = cxt.WaitAnyUpdateAll(); //first update the context - refresh the depth/image data coming from the sensor
sessionMgr->Update(&cxt);
if(rc != XN_STATUS_OK) //if the update failed, i.e. couldn't be read
{
cout << "ERROR:Read failed... Quitting!" << endl; //print error message
exit(0); //exit the program
}
else
{
//update the session manager
depthGen.GetMetaData(depthMapMetaData); //grab the depth map meta data
imageGen.GetMetaData(imageMapMetaData); //grab the image map meta data
const XnRGB24Pixel* imageMetaData; //this array will contain the color of each pixel in depth map
imageMetaData = imageMapMetaData.RGB24Data(); //this will grab the color values and store it into the array
int i,j,colorToSet,depth,temp;
for(i = 0;i<X_SIZE;i++) //this loop will run through width of the depth map
{
for(j=0;j<Y_SIZE;j++) //this will run through the height
{
temp = j*X_SIZE + i;
depth = depthMapMetaData(i,j); //get the depth info from the depthMapData. lvalue is (i*Xmax + j) in order to translate matrix coords to linear ones
colorToSet = MAX_COLOR - depth/COLOR_ZONES;
depth_image->imageData[temp] = colorToSet;
raw_image->imageData[3*temp]= imageMetaData[temp].nBlue;
raw_image->imageData[3*temp+1]= imageMetaData[temp].nGreen;
raw_image->imageData[3*temp+2]= imageMetaData[temp].nRed;
}
}
if(inSession)
{
if(isHandPointNull() == false)
{
cvCircle(raw_image, cvPoint((int)handPointCoords.X,(int)handPointCoords.Y), 10, CV_RGB(0,255,0), -1);
}
}
cvShowImage("Depth", depth_image);
cvShowImage("Raw Image",raw_image);
}
int key = (char)cvWaitKey(10);
if ( key == 27|| key == 'q'|| key == 'Q') break;
}
cvReleaseImage(&depth_image);
cvReleaseImage(&raw_image);
return 0;
}
/************************************ CALLBACK DEFINITIONS ********************************/
/**********************************************************************************
Session started event handler. Session manager calls this when the session begins
**********************************************************************************/
void XN_CALLBACK_TYPE sessionStart(const XnPoint3D& ptPosition, void* UserCxt){
inSession = true;
printf("\nin session");
}
/**********************************************************************************
session end event handler. Session manager calls this when session ends
**********************************************************************************/
void XN_CALLBACK_TYPE sessionEnd(void* UserCxt){
inSession = false;
printf("\nnot in session");
}
/**********************************************************************************
point created event handler. this is called when the pointControl detects the creation
of the hand point. This is called only once when the hand point is detected
**********************************************************************************/
void XN_CALLBACK_TYPE pointCreate(const XnVHandPointContext *pContext, const XnPoint3D &ptFocus, void *cxt){
XnPoint3D coords(pContext->ptPosition);
depthGen.ConvertRealWorldToProjective(1,&coords,&handPointCoords);
}
/**********************************************************************************
Following the point created method, any update in the hand point coordinates are
reflected through this event handler
**********************************************************************************/
void XN_CALLBACK_TYPE pointUpdate(const XnVHandPointContext *pContext, void *cxt){
XnPoint3D coords(pContext->ptPosition);
depthGen.ConvertRealWorldToProjective(1,&coords,&handPointCoords);
}
/**********************************************************************************
when the point can no longer be tracked, this event handler is invoked. Here we
nullify the hand point variable
**********************************************************************************/
void XN_CALLBACK_TYPE pointDestroy(XnUInt32 nID, void *cxt){
nullifyHandPoint();
printf("\nDead");
} |
Partager