获得其他对象的深度Kinect

时间:2013-02-25 15:52:48

标签: object opencv kinect tracking depth

我是kinect编程的新手,我正在使用kinect和opencv进行球跟踪。我们都知道kinect提供深度数据,并使用以下代码:

DepthImagePoint righthandDepthPoint = 
    sensor.CoordinateMapper.MapSkeletonPointToDepthPoint
    (
        me.Joints[JointType.HandRight].Position, 
        DepthImageFormat.Resolution640x480Fps30
    );

double rightdepthmeters = (righthandDepthPoint.Depth);

使用这个,我可以通过指定关节类型使用函数MapSkeletonPointToDepthPoint()来获得右手的深度。

是否可以通过在图像中指定来获取其他对象的深度? 给定坐标..我想获得该坐标中对象的深度?

1 个答案:

答案 0 :(得分:0)

可以从DepthImagePixel结构中提取Kinect SDK中的深度数据。

下面的示例代码循环遍历整个DepthImageFrame以检查每个像素。如果您有想要查看的特定坐标,请移除for循环并将xy设置为特定值。

// global variables

private const DepthImageFormat DepthFormat = DepthImageFormat.Resolution320x240Fps30;
private const ColorImageFormat ColorFormat = ColorImageFormat.RgbResolution640x480Fps30;

private DepthImagePixel[] depthPixels;

// defined in an initialization function

this.depthWidth = this.sensor.DepthStream.FrameWidth;
this.depthHeight = this.sensor.DepthStream.FrameHeight;

this.depthPixels = new DepthImagePixel[this.sensor.DepthStream.FramePixelDataLength];

private void SensorAllFramesReady(object sender, AllFramesReadyEventArgs e)
{
    if (null == this.sensor)
        return;

    bool depthReceived = false;

    using (DepthImageFrame depthFrame = e.OpenDepthImageFrame())
    {
        if (null != depthFrame)
        {
            // Copy the pixel data from the image to a temporary array
            depthFrame.CopyDepthImagePixelDataTo(this.depthPixels);

            depthReceived = true;
        }
    }

    if (true == depthReceived)
    {
        // loop over each row and column of the depth
        for (int y = 0; y < this.depthHeight; ++y)
        {
            for (int x = 0; x < this.depthWidth; ++x)
            {
                // calculate index into depth array
                int depthIndex = x + (y * this.depthWidth);

                // extract the given index
                DepthImagePixel depthPixel = this.depthPixels[depthIndex];

                Debug.WriteLine("Depth at [" + x + ", " + y + "] is: " + depthPixel.Depth);
            }
        }
    }
}