none
Displaying Distance data getting from depth data of Kinect in C++. RRS feed

  • Question

  • Hello Everyone,

    I need some help about Displaying Distance data getting from depth data of Kinect in C++.

    I want to display/show the distance of tracked person from kinect sensor in C++.

    I went to some examples of Working depthdata in C#. But I need to write program in C++.

    if someone has some source codes of working on distance/depth data in C++, please give to me or please suggest me how to work on depth data on C++.

    Please help me.

    Thanks in advance.

    Tuesday, July 19, 2011 8:49 AM

Answers

  • m_pDepthStreamHandle is an instance variable, and it's a handle to the depth stream. You can see it being passed to NuiImageStreamOpen() inside the NuiInit() function in the sample. NuiImageStreamOpen is also passed m_hNextDepthFrameEvent, an event handle to be signaled when a new frame of data arrives.

    Then, in Nui_ProcessThread(), you can see where the event is signaled when there is a new frame of depth data. The Nui_GotDepthAlert() function is called to handle that event. It uses m_pDepthStreamHandle to retrieve the new frame of depth data and process it by drawing to the screen.

     

    I haven't built this code or run it, but what you need is something like this:

     

    void CSkeletalViewerApp::Nui_GotDepthAlert( )
     {
      const NUI_IMAGE_FRAME * pImageFrame = NULL;
     
     HRESULT hr = NuiImageStreamGetNextFrame(
       m_pDepthStreamHandle,
       0,
       &pImageFrame );
     
     if( FAILED( hr ) )
      {
       return;
      }
     
     NuiImageBuffer * pTexture = pImageFrame->pFrameTexture;
      KINECT_LOCKED_RECT LockedRect;
      pTexture->LockRect( 0, &LockedRect, NULL, 0 );
      if( LockedRect.Pitch != 0 )
      {
       BYTE * pBuffer = (BYTE*) LockedRect.pBits;
     
      // draw the bits to the bitmap
       RGBQUAD * rgbrun = m_rgbWk;
       USHORT * pBufferRun = (USHORT*) pBuffer;
       for( int y = 0 ; y < 240 ; y++ )
       {
        for( int x = 0 ; x < 320 ; x++ )
        {
         USHORT depth = ((*pBufferRun) & 0xfff8)>>3; // This is the depth value you care about
    RGBQUAD quad = Nui_ShortToQuad_Depth( *pBufferRun ); pBufferRun++; rgbrun++; } } m_DrawDepth.DrawFrame( (BYTE*) m_rgbWk ); } else { OutputDebugString( L"Buffer length of received texture is bogus\r\n" ); } NuiImageStreamReleaseFrame( m_pDepthStreamHandle, pImageFrame ); }

    I inserted a comment at the place where you know the depth value of the pixel at (x,y).

     


    Wednesday, July 20, 2011 8:00 PM

All replies

  • There's a C++ version of the SkeletalViewer sample installed as part of the SDK. Look at the Nui_GotDepthAlert function in NuiImpl.cpp for an example of using depth data. That function converts the depth value to a color and paints that color to the screen, but you could do other processing instead. The depth value is the distance to that point in the frame, in millimeters.
    Tuesday, July 19, 2011 9:19 PM
  • void CSkeletalViewerApp::Nui_GotDepthAlert( )
    {
        const NUI_IMAGE_FRAME * pImageFrame = NULL;

        HRESULT hr = NuiImageStreamGetNextFrame(
            m_pDepthStreamHandle,
            0,
            &pImageFrame );

        if( FAILED( hr ) )
        {
            return;
        }

        NuiImageBuffer * pTexture = pImageFrame->pFrameTexture;
        KINECT_LOCKED_RECT LockedRect;
        pTexture->LockRect( 0, &LockedRect, NULL, 0 );
        if( LockedRect.Pitch != 0 )
        {
            BYTE * pBuffer = (BYTE*) LockedRect.pBits;

            // draw the bits to the bitmap
            RGBQUAD * rgbrun = m_rgbWk;
            USHORT * pBufferRun = (USHORT*) pBuffer;
            for( int y = 0 ; y < 240 ; y++ )
            {
                for( int x = 0 ; x < 320 ; x++ )
                {
                    RGBQUAD quad = Nui_ShortToQuad_Depth( *pBufferRun );
                    pBufferRun++;
                    *rgbrun = quad;
                    rgbrun++;
                }
            }

            m_DrawDepth.DrawFrame( (BYTE*) m_rgbWk );
        }
        else
        {
            OutputDebugString( L"Buffer length of received texture is bogus\r\n" );
        }

        NuiImageStreamReleaseFrame( m_pDepthStreamHandle, pImageFrame );
    }


    Is m_pDepthStreamHandle depth data or distance?
    Kinect's program in C# is easier than in C++, isn't it right?

    I could not understand kinect's program in C++?

    Anyone has very simple program in c++ of extracting distance of tracked object from  depth data?

    Please help me.

    Thanks in advance.

     

    Wednesday, July 20, 2011 2:30 AM
  • m_pDepthStreamHandle is an instance variable, and it's a handle to the depth stream. You can see it being passed to NuiImageStreamOpen() inside the NuiInit() function in the sample. NuiImageStreamOpen is also passed m_hNextDepthFrameEvent, an event handle to be signaled when a new frame of data arrives.

    Then, in Nui_ProcessThread(), you can see where the event is signaled when there is a new frame of depth data. The Nui_GotDepthAlert() function is called to handle that event. It uses m_pDepthStreamHandle to retrieve the new frame of depth data and process it by drawing to the screen.

     

    I haven't built this code or run it, but what you need is something like this:

     

    void CSkeletalViewerApp::Nui_GotDepthAlert( )
     {
      const NUI_IMAGE_FRAME * pImageFrame = NULL;
     
     HRESULT hr = NuiImageStreamGetNextFrame(
       m_pDepthStreamHandle,
       0,
       &pImageFrame );
     
     if( FAILED( hr ) )
      {
       return;
      }
     
     NuiImageBuffer * pTexture = pImageFrame->pFrameTexture;
      KINECT_LOCKED_RECT LockedRect;
      pTexture->LockRect( 0, &LockedRect, NULL, 0 );
      if( LockedRect.Pitch != 0 )
      {
       BYTE * pBuffer = (BYTE*) LockedRect.pBits;
     
      // draw the bits to the bitmap
       RGBQUAD * rgbrun = m_rgbWk;
       USHORT * pBufferRun = (USHORT*) pBuffer;
       for( int y = 0 ; y < 240 ; y++ )
       {
        for( int x = 0 ; x < 320 ; x++ )
        {
         USHORT depth = ((*pBufferRun) & 0xfff8)>>3; // This is the depth value you care about
    RGBQUAD quad = Nui_ShortToQuad_Depth( *pBufferRun ); pBufferRun++; rgbrun++; } } m_DrawDepth.DrawFrame( (BYTE*) m_rgbWk ); } else { OutputDebugString( L"Buffer length of received texture is bogus\r\n" ); } NuiImageStreamReleaseFrame( m_pDepthStreamHandle, pImageFrame ); }

    I inserted a comment at the place where you know the depth value of the pixel at (x,y).

     


    Wednesday, July 20, 2011 8:00 PM
  • General advice for learning how a new piece of code works:

    • Run the code under the debugger and single-step through the code, watching how variables change and what the flow of execution looks like.
    • Set breakpoints at places of interest and watch how the values of variables change.
    • Use the code browsing tools in Visual Studio. Highlight the name of a variable, right-click, and then choose "Go to Definition" or "Find All References" to see where in the code the variable is defined and used.

    Wednesday, July 20, 2011 8:04 PM