尝试从C#中的Kinect v2获取颜色值时出现IndexOutOfRange异常 [英] IndexOutOfRange Exception while trying to get color values from Kinect v2 in C#

查看:114
本文介绍了尝试从C#中的Kinect v2获取颜色值时出现IndexOutOfRange异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获取Kinect v2中每个CameraSpacePoint的RGB值.尝试访问转换的帧数据时,代码抛出IndexOutOfRangeException.我发现以下几行引发此错误:

I am trying to get the RGB values of each CameraSpacePoint in Kinect v2. While trying to access converted frame data, the code is throwing IndexOutOfRangeException. I found out that following lines are throwing this error:

byte red   = pixels[4 * pixelsBaseIndex + 0];
byte green = pixels[4 * pixelsBaseIndex + 1];
byte blue  = pixels[4 * pixelsBaseIndex + 2];
byte alpha = pixels[4 * pixelsBaseIndex + 3];

请参见下面的完整代码:

Please see the complete code below:

int depthWidth = kinectSensor.DepthFrameSource.FrameDescription.Width;
int depthHeight = kinectSensor.DepthFrameSource.FrameDescription.Height;
int colorWidth = kinectSensor.ColorFrameSource.FrameDescription.Width;
int colorHeight = kinectSensor.ColorFrameSource.FrameDescription.Height;

ushort[] depthData = new ushort[depthWidth * depthHeight];
CameraSpacePoint[] cameraPoints = new CameraSpacePoint[depthData.Length];
ColorSpacePoint[] colorPoints = new ColorSpacePoint[depthData.Length];

// Assuming RGBA format here
byte[] pixels = new byte[colorWidth * colorHeight * 4];

depthFrame = multiSourceFrame.DepthFrameReference.AcquireFrame();
colorFrame = multiSourceFrame.ColorFrameReference.AcquireFrame();

if ((depthFrame == null) || (colorFrame == null))
{
    return;
}

depthFrame.CopyFrameDataToArray(depthData);
coordinateMapper.MapDepthFrameToCameraSpace(depthData, cameraPoints);
coordinateMapper.MapDepthFrameToColorSpace(depthData, colorPoints);

// We are using RGBA format
colorFrame.CopyConvertedFrameDataToArray(pixels, ColorImageFormat.Rgba);

for (var index = 0; index < depthData.Length; index++)
{
    var u = colorPoints[index].X;
    var v = colorPoints[index].Y;

    if (u < 0 || u >= colorWidth || v < 0 || v >= colorHeight) continue;

    int pixelsBaseIndex = (int)(v * colorWidth + u);
    try
    {
        byte red = pixels[4 * pixelsBaseIndex + 0];
        byte green = pixels[4 * pixelsBaseIndex + 1];
        byte blue = pixels[4 * pixelsBaseIndex + 2];
        byte alpha = pixels[4 * pixelsBaseIndex + 3];
    }
    catch (IndexOutOfRangeException ex)
    {
        int minValue = 4 * pixelsBaseIndex;
        int maxValue = 4 * pixelsBaseIndex + 3;
        Console.WriteLine((minValue > 0) + ", " + (maxValue < pixels.Length));
    }
}

代码看起来不错,但是我不确定我在这里缺少什么! 如何避免IndexOutOfRangeException异常?,有什么建议吗?

The code looks fine however I am not sure what am I missing here! How to avoid IndexOutOfRangeException exception? Any suggestions, please?

推荐答案

第一件事pixelsBaseIndex =< 2070601 (2070601 = 1919 * 1079)

2074817pixelsBaseIndex的无效值.

此错误可能在此处.

if (u < 0 || u >= colorWidth || v < 0 || v >= colorHeight) continue;

int pixelsBaseIndex = (int)(v * colorWidth + u);

最好的方法是控制台日志u, v, colorWidth, colorHeight并对其进行验证.

Best thing is to console log u, v, colorWidth, colorHeight and validate them.

编辑

我怀疑这是由浮点运算引起的. 将一帧映射到另一帧需要复杂的矩阵计算,因此u,v获得浮点值.请记住, u,v是色彩空间的像素坐标.它们必须是非负整数.

As i suspected, it is caused by the Floating-Point Arithmetic. Mapping one frame to another frame needs complex matrix calculations, thus u,v get floating point values. Keep in mind that u,v are the pixel coordinates of the color space. They must be non-negative integers.

我推荐的解决方案是

...
    int u = (int) Math.Floor(colorPoints[index].X);
    int v = (int) Math.Floor(colorPoints[index].Y);

    if (u < 0 || u >= colorWidth || v < 0 || v >= colorHeight) continue;
...

仅供参考

请查看以下代码行,以了解为什么u,v像素坐标获取浮点值.决定发言还是放弃无效值取决于开发人员,因为在这两种情况下,您都会丢失某种信息.例如,占地表示您正在获取最近像素的颜色.

Check out following lines of code to understand why u, v pixel coordinates getting floating point values. The decision, taking the floor or discarding the invalid values, is depending on the developer since in either cases, you are loosing some sort of information. For example, taking the floor means you are taking the color of the nearest pixel.

在我个人的选择中,输出更精确的值(因此是浮点值)对于对输出值进行更多计算的开发人员很重要,例如点云注册.

In my personal option, out putting more precise value (thus floating point value) is important for developers who do more calculations on the outputted values.For example, point cloud registration.

depth_to_color

根据深度值计算rgb图像的x偏移量

这篇关于尝试从C#中的Kinect v2获取颜色值时出现IndexOutOfRange异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆