简体   繁体   中英

Kinect v2 color depth mapping using C#

I am trying to map the depth map onto to the color space using the coordinateMapper function from the sdk. However, the output I received is not what I expected, it appears to have a smaller aspect ratio and the image seems to be mirrored.

Below is the code I used:

depthWidth = depthFrameDescription.Width;
depthHeight = depthFrameDescription.Height;

var colorDesc = colorFrame.FrameDescription;
int colorWidth = colorDesc.Width;
int colorHeight = colorDesc.Height;

int size = colorWidth * colorHeight;

DepthSpacePoint[] colSpacePoints = new DepthSpacePoint[colorWidth * colorHeight];

using (KinectBuffer depthFrameData = depthFrame.LockImageBuffer())
using (KinectBuffer colorBuffer = colorFrame.LockRawImageBuffer())
{
    this.bitmap.Lock();

    if ((colorWidth == this.bitmap.PixelWidth) && (colorHeight == this.bitmap.PixelHeight))
    {
        byte[] colorFrameData = new byte[size * bytesPerPixel];       

        // Map the values here
        ushort [] frameData = new ushort[depthWidth * depthHeight];
        depthFrame.CopyFrameDataToArray(frameData);
        coordinateMapper.MapColorFrameToDepthSpace(frameData, colSpacePoints);

        this.bitmap.WritePixels(new Int32Rect(0, 0, colorWidth, colorHeight), colSpacePoints, colorWidth * bytesPerPixel, 0);
        this.bitmap.AddDirtyRect(new Int32Rect(0, 0, this.bitmap.PixelWidth, this.bitmap.PixelHeight));
    }    
    this.bitmap.Unlock();
}

this.bitmapBackBufferSize = (uint)![enter image description here][1]((this.bitmap.BackBufferStride * (this.bitmap.PixelHeight - 1)) + (this.bitmap.PixelWidth * this.bytesPerPixel));
isBitmapLocked = true;

If you are trying to map the coordinates of the depth frame to the color frame, you are missing one step.

Here is a link that probably explains it better that I can: http://www.bryancook.net/2014/03/mapping-between-kinect-color-and-depth.html

But in your case when you use

coordinateMapper.MapColorFrameToDepthSpace(frameData, colSpacePoints);

The output is not to be used how you are using it. The output to this function contains all the points that exists both in the color space and in the depth space. So in order to display those, you want to iterate through your colSpacePoints and do something with each results, in the example I linked, it is called _colorSpacePoints so you can look at what he does with it.

I was stuck trying to do probably something similar to what you are doing, however after a few days I found how to properly use the mapping. If you want more help with your problem tell me what you want to display in your WriteableBitmap.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM