none
How do I access x, y, z, coordinates from the Kinect depth camera? (in C++) RRS feed

  • Question

  • Hi,

    I'm trying to access x, y, z, depth data captured from the Kinect camera using C++. What I'm ultimately trying to do is to display a point cloud on OpenGL with the depth data from the Kinect. I have read through the sample skeleton viewer code from the Kinect Windows SDK and also tried to follow along with the walkthrough for the skeleton viewer. I've also tried to implement some example code I saw floating around in these forums.

    I want to access the x, y, z coordinates so that I can put it into an array and then call glDrawArrays to show the point cloud on OpenGL.

    Here is what I have so far...

    void camera (void) {
    	HRESULT    hr;
    	m_hNextDepthFrameEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
    	hr = NuiInitialize( NUI_INITIALIZE_FLAG_USES_DEPTH);
    	hr = NuiImageStreamOpen(NUI_IMAGE_TYPE_DEPTH, NUI_IMAGE_RESOLUTION_320x240,
    							0, 2, NULL, &m_pDepthStreamHandle ); 
    	if( FAILED( hr ) ) printf("DepthStreamHanle verkackt \n");
     
    	HANDLE	hEvents[4];
    	int	nEventIdx;
    	
    	// Configure events to be listened on
    	hEvents[0] = m_hEvNuiProcessStop;
    	hEvents[1] = m_hNextDepthFrameEvent;
    	hEvents[2] = m_hNextVideoFrameEvent;
    	hEvents[3] = m_hNextSkeletonEvent;
    	
    	
    	while (1) {
    		const NUI_IMAGE_FRAME * pImageFrame = NULL;
    		HRESULT hr = NuiImageStreamGetNextFrame(
            m_pDepthStreamHandle,
            200,
            &pImageFrame );
    		else if (FAILED(hr)) {
    		break;
    		}
    
    		NuiImageBuffer * pTexture = pImageFrame->pFrameTexture;
    		KINECT_LOCKED_RECT LockedRect;
    		pTexture->LockRect(0, &LockedRect, NULL, 0);
    		BYTE * pBuffer = (BYTE*) LockedRect.pBits;
    		USHORT * pBufferRun = (USHORT*) pBuffer;
    		int i = 0;
    		for( int y = 0 ; y < 240 ; y++ ) {
    			for( int x = 0 ; x < 320 ; x++ ) {
    				Vector4 v = NuiTransformDepthImageToSkeletonF(x/320.0f,y/240.0f, *pBufferRun >> 3);
    				points[i] = v.x;
    				points[i+1] = v.y;
    				points[i+2] = v.z;
    				i += 3;
    				pBufferRun++;
    			}
    			
    		}
    	NuiImageStreamReleaseFrame( m_pDepthStreamHandle, pImageFrame );
    	glutPostRedisplay();
    	}
    }
    

    the "points" is an array of size 230400 that stores the x, y, z coordinates in order.

    Whenever I run this, the coordinates in the "points" array are not what I expect them to be and gives some weird numbers. Is this because of the call  "NuiTransformDepthImageToSkeletonF"? Is there a better way to do this?

    I would greatly appreciate any help on this.

     

    Thanks!

    Tuesday, August 2, 2011 9:31 AM

Answers

  • Youre just using the depth stream. In only depth images, the depth data comes in the 12 lsb's of the short, so you shouldnt shift the data to the right. Infact, you should shift it to the left (by 3), because NuiImageStreamReleaseFrame shifts it back.

    Vector4 v = NuiTransformDepthImageToSkeletonF(x/320.0f,y/240.0f, *pBufferRun << 3);

    Tuesday, August 2, 2011 10:11 AM

All replies

  • Youre just using the depth stream. In only depth images, the depth data comes in the 12 lsb's of the short, so you shouldnt shift the data to the right. Infact, you should shift it to the left (by 3), because NuiImageStreamReleaseFrame shifts it back.

    Vector4 v = NuiTransformDepthImageToSkeletonF(x/320.0f,y/240.0f, *pBufferRun << 3);

    Tuesday, August 2, 2011 10:11 AM
  • Hi,

    Am working on the same but am using the SDK v1.5 so will it work as well?? Where shall I add this piece of code?? 

    Your help is really appreciated.

    Monday, September 24, 2012 4:42 PM