开发者

Memory Issues with cvShowImage and Kinect SDK: Skeletal Viewer

I'm using cvSetData to get the rgb frame into one I can use for openCV. I modified the SkeletalViewer slightly to produce the rgb stream.

void CSkeletalViewerApp::Nui_GotVideoAlert( )
{
    const NUI_IMAGE_FRAME * pImageFrame = NULL;

    IplImage* kinectColorImage = cvCreateImage(cvSize(640,480),IPL_DEPTH_8U, 4);

    HRESULT hr = NuiImageStreamGetNextFrame(
        m_pVideoStreamHandle,
        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;

        m_DrawVideo.DrawFrame( (BYTE*) pBuffer );

        cvSetData(kinectColorImage, (BYTE*) pBuffer,kinectColorImage->widthStep);

        cvShowImage("Color Image", kinectColorImage);
        //cvReleaseImage( &kinectColorImage );
        cvWaitKey(10);

    }
    else
    {
        OutputDebugString( L"Buffer length of received texture is bogus\r\n" );
    }

    cvReleaseImage(&kinectColorImage);
    NuiImageStreamReleaseFrame( m_pVideoStreamHandle, pImageFrame );

}

With the cvReleaseImage, I would get a cvException error. Not exactly sure which one as it didn't specify. Without cvReleaseImage, I would get the rgb video running in an openCV window but would eventually crash 开发者_开发问答because it ran out of memory.

How should I release the image properly?


Just solved this problem.

After a bunch of sleuthing using breakpoints and debugging, it appears as though the problem has to do with the pointers used in cvSetData. My best guess is that Nui_GotVideoAlert() updates the address pointed to by pBuffer before cvReleaseImage is called. In addition, cvSetData never appears to copy the bytes from this address.

What happens then is that cvReleaseImage is called on an address that no longer exists.

I fixed this by declaring kinectColorImage at the top of NuiImpl.cpp, calling cvSetData in ::Nui_GotVideoAlert(), and only calling cvReleaseImage in the Nui_Uninit() method. This way, kinectColorImage will just update instead of creating a new IplImage in each call of Nui_GotVideoAlert().


That's strange. As far as I know, cvReleaseImage released both the image header and the image data. I did the piece of code below and in this certain example, cvReleaseImage does not free the buffer that contains the data. There I didn't use cvSetData but I just updated the pointer to the image data. If you uncomment the commented lines and comment the ones just below each one, program still runs but you'll get some memory leaks. I used OpenCV 2.2 (this is the legacy interface).

#include <opencv/cv.h>
#include <stdlib.h>
#define NLOOPS 1000
int main(void){
  int i,j

  char *buff = (char *) malloc( sizeof(char) * 3 * 640 * 480 );
  for( i = 0; i < 640 * 480 * 3; i++ ) buff[i] = 128;
  j = 0;
  while( j++< NLOOPS ){

      IplImage *im = cvCreateImage(cvSize(640,480),IPL_DEPTH_8U, 3); 
      //cvSetData(im, buff, im->widthStep);  ---> If you use that version you'll get memory  leaks. Comment line below.
      im->imageData = buff;
      cvWaitKey(4);
      cvShowImage("kk", im);
      //cvReleaseImageHeader(&im);           ---> If you use that version you'll get memory leaks. Comment line below.
      cvReleaseImage(&im);

      free(im);
  }

  free(buff);
  return 0; 
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜