none
Kinect App crashes and I can't figure out why. RRS feed

  • Question

  • I am trying to write a wrapper so that I can use the background removal in a C# application. 

    Unfortunately, it crashes when I try to get to the frame data. GetNextFrame succeeds. I am sure I am doing something small wrong; I would appreciate it if someone could help me out, or suggest a strategy that I can use to find the problem.

    If I use the functions in Kinect10.dll throughout instead, no crash occurs, and everything works as expected. (I do need the wrapper so that I can use the BackgroundRemoval functionality). This makes me think the error is on the C side of things, rather than in my application code.

    The code below shows just the color polling. Depth polling has the same problem. Skeleton polling works fine.  

    Here is the C wrapper code:

    __declspec(dllexport) HRESULT BR_NuiImageStreamOpen( NUI_IMAGE_TYPE eImageType, NUI_IMAGE_RESOLUTION eResolution, DWORD dwImageFrameFlags_NotUsed, DWORD dwFrameLimit, HANDLE hNextFrameEvent, HANDLE * phStreamHandle) { int hr = sensor->NuiImageStreamOpen( eImageType, eResolution, dwImageFrameFlags_NotUsed, dwFrameLimit, hNextFrameEvent, phStreamHandle); return hr; }

    __declspec(dllexport) HRESULT BR_NuiImageStreamGetNextFrame(HANDLE phStreamHandle, DWORD dwMillisecondsToWait, NUI_IMAGE_FRAME * ppcImageFrame) { HRESULT hr = sensor->NuiImageStreamGetNextFrame(phStreamHandle, dwMillisecondsToWait, ppcImageFrame); return hr; }

    And here is the code on the C# side:

    [DllImportAttribute(@"BackgroundRemoval3.dll", EntryPoint = "BR_NuiImageStreamOpen")] public static extern int BR_NuiImageStreamOpen(NuiImageType eImageType, NuiImageResolution eResolution, uint dwImageFrameFlags_NotUsed, uint dwFrameLimit, IntPtr hNextFrameEvent, ref IntPtr phStreamHandle);

    [DllImportAttribute(@"BackgroundRemoval3.dll", EntryPoint = "BR_NuiImageStreamGetNextFrame")] public static extern int BR_NuiImageStreamGetNextFrame(IntPtr phStreamHandle, uint dwMillisecondsToWait, ref IntPtr ppcImageFrame);

    public static bool PollColor2(IntPtr colorStreamHandle, refbyte[] videoBuffer, ref Color32[] colorImage) { IntPtr imageFramePtr = IntPtr.Zero; bool newColor = false; int hr = BR_NuiImageStreamGetNextFrame(colorStreamHandle, 0, ref imageFramePtr); if (hr ==0) { newColor =true; NuiImageFrame imageFrame = (NuiImageFrame)Marshal.PtrToStructure(imageFramePtr, typeof(NuiImageFrame));

    ///// The line below is the line that crashes (or rather, if I return ///// with false before this line, it does not crash) INuiFrameTexture frameTexture = (INuiFrameTexture)Marshal.GetObjectForIUnknown(imageFrame.pFrameTexture);

    NuiLockedRect lockedRectPtr =new NuiLockedRect(); IntPtr r = IntPtr.Zero; frameTexture.LockRect(0, ref lockedRectPtr, r, 0); ColorBuffer cb = (ColorBuffer)Marshal.PtrToStructure(lockedRectPtr.pBits, typeof(ColorBuffer)); int totalPixels = Constants.ImageWidth * Constants.ImageHeight; for (int pix = 0; pix < totalPixels; pix++) { int ind = totalPixels - pix -1; colorImage[ind].r = cb.pixels[pix].r; colorImage[ind].g = cb.pixels[pix].g; colorImage[ind].b = cb.pixels[pix].b; colorImage[ind].a =255; } frameTexture.UnlockRect(0); hr = BR_NuiImageStreamReleaseFrame(colorStreamHandle, imageFramePtr); } return newColor; }






    Tuesday, March 18, 2014 9:49 PM

Answers

  • Turns out I missed the subtle distinction between 

    NuiImageStreamGetNextFrame
    INuiSensor::NuiImageStreamGetNextFrame 

    where the last argument is of type const NUI_IMAGE_FRAME ** for the first and NUI_IMAGE_FRAME * for the second.



    Friday, March 28, 2014 10:26 AM