在Kinect中查找颜色像素的深度值

时间:2015-06-17 06:28:12

标签: c# kinect kinect-sdk

我在1920 * 1080的彩色像素中有一定的X,Y点。我不知道如何在深度数据512×424中映射该特定点。我知道我需要使用坐标映射器,但无法弄清楚如何做到这一点。我是Kinect的初学者,我正在使用C#。有人请帮助我

2 个答案:

答案 0 :(得分:1)

以下是一个示例,我已将CameraSpacePoint转换为ColorSpacePoint,将CameraSpacePoint转换为DepthSpacePoint

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Kinect;

namespace Coordinate_Mapper
{
    class Program
    {
        public static KinectSensor ks;
        public static MultiSourceFrameReader msfr;
        public static Body[] bodies;
        public static CameraSpacePoint[] cameraSpacePoints;
        public static DepthSpacePoint[] depthSpacePoints;
        public static ColorSpacePoint[] colorSpacePoints;

        static void Main(string[] args)
        {
            ks = KinectSensor.GetDefault();
            ks.Open();
            bodies = new Body[ks.BodyFrameSource.BodyCount];
            cameraSpacePoints = new CameraSpacePoint[1];
            colorSpacePoints = new ColorSpacePoint[1];
            depthSpacePoints = new DepthSpacePoint[1];

            msfr = ks.OpenMultiSourceFrameReader(FrameSourceTypes.Depth | FrameSourceTypes.Color | FrameSourceTypes.Body);
            msfr.MultiSourceFrameArrived += msfr_MultiSourceFrameArrived;

            Console.ReadKey();
        }

        static void msfr_MultiSourceFrameArrived(object sender, MultiSourceFrameArrivedEventArgs e)
        {
            if (e.FrameReference == null) return;
            MultiSourceFrame multiframe = e.FrameReference.AcquireFrame();
            if (multiframe == null) return;

            if (multiframe.BodyFrameReference != null)
            {
                using (var bf = multiframe.BodyFrameReference.AcquireFrame())
                {
                    bf.GetAndRefreshBodyData(bodies);
                    foreach (var body in bodies)
                    {
                        if (!body.IsTracked) continue;
                        // CameraSpacePoint
                        cameraSpacePoints[0] = body.Joints[0].Position;
                        Console.WriteLine("{0} {1} {2}", cameraSpacePoints[0].X, cameraSpacePoints[0].Y, cameraSpacePoints[0].Z);

                        // CameraSpacePoints => ColorSpacePoints
                        ks.CoordinateMapper.MapCameraPointsToColorSpace(cameraSpacePoints, colorSpacePoints);
                        Console.WriteLine("ColorSpacePoint : {0} {1}", colorSpacePoints[0].X, colorSpacePoints[0].Y);

                        // CameraSpacePoints => DepthSpacePoints
                        ks.CoordinateMapper.MapCameraPointsToDepthSpace(cameraSpacePoints, depthSpacePoints);
                        Console.WriteLine("DepthSpacePoint : {0} {1}", depthSpacePoints[0].X, depthSpacePoints[0].Y);
                    }
                }
            }
        }
    }
}

N:B:

  1. 我要将CameraSpacePointDepthSpacePointColorSpacePoint存储到数组中,因为CoordinateMapper类中方法的参数是数组。有关详情,请查看CoordinateMapper Method Reference

  2. 此博客可能对您有所帮助。 Understanding Kinect Coordinate Mapping

答案 1 :(得分:1)

如果要将颜色框架映射到深度框架,则需要使用方法MapColorFrameToDepthSpace:

ushort[] depthData = ... // Data from the Depth Frame
DepthSpacePoint[] result = new DepthSpacePoint[512 * 424];

_sensor.CoordinateMapper.MapColorFrameToDepthSpace(depthData, result);

您需要为此方法提供2个参数:

  1. 完整的深度框架数据(如this)。
  2. DepthSpacePoints的空数组。
  3. 提供这些参数,空数组将填充正确的DepthSpacePoint值。

    否则,Rafaf的回答就是你所需要的。