索引超出了 Kinect v2 中数组的范围
Index was outside the bounds of the array in Kinect v2
我正在尝试获取 Kinect v2 中每个 CameraSpacePoint 的 RGB 值。我能够获取 CameraSpacePoints 和 Color 像素。但似乎在我的代码中从 CameraSpacePoints 到 Color 像素的映射被破坏,这导致我出现 IndexOutOfRangeException。
请查看下面显示数据收集部分的代码片段:
var depthWidth = depthFrame.FrameDescription.Width;
var depthHeight = depthFrame.FrameDescription.Height;
ushort[] depthData = new ushort[depthWidth * depthHeight];
var colorWidth = colorFrame.FrameDescription.Width;
var colorHeight = colorFrame.FrameDescription.Height;
byte[] pixels = new byte[colorWidth * colorHeight * 4];
CameraSpacePoint[] cameraSpacePoints = new CameraSpacePoint[depthData.Length];
ColorSpacePoint[] colorSpacePoints = new ColorSpacePoint[depthData.Length];
depthFrame.CopyFrameDataToArray(depthData);
coordinateMapper.MapDepthFrameToCameraSpace(depthData, cameraSpacePoints);
coordinateMapper.MapDepthFrameToColorSpace(depthData, colorSpacePoints);
// Assuming RGBA format here
colorFrame.CopyConvertedFrameDataToArray(pixels, ColorImageFormat.Rgba);
请查看下面显示 ColorSpacePoints 的 RGB 值的代码:
for (var index = 0; index < depthData.Length; index++)
{
var u = colorSpacePoints[index].X;
var v = colorSpacePoints[index].Y;
int pixelsBaseIndex = (int)(v * depthWidth + u);
byte red = pixels[4 * pixelsBaseIndex + 0];
byte green = pixels[4 * pixelsBaseIndex + 1];
byte blue = pixels[4 * pixelsBaseIndex + 2];
byte alpha = pixels[4 * pixelsBaseIndex + 3];
}
以上代码片段抛出以下错误:
IndexOutOfRangeException: Index was outside the bounds of the array.
为了调试这个问题,我监控了每个索引然后计算了最小和最大索引值如下:
List<int> allIndex = new List<int>();
for (var index = 0; index < depthData.Length; index++)
{
var u = colorpoints[index].X;
var v = colorpoints[index].Y;
int pixelsBaseIndex = (int)(v * depthWidth + u);
allIndex.Add(pixelsBaseIndex);
}
var maxIndex = allIndex.Max();
var minIndex = allIndex.Min();
Console.WriteLine(minIndex);//Prints -2147483648
Console.WriteLine((maxIndex < pixels.Length) && (minIndex >= 0));//Prints False
令人惊讶的是,最小索引是-2147483648。任何猜测?我在这里遗漏了一些明显的东西吗?
试试这个
bool flag = true;
for (int i = 1; (i <= ((depthData.Length)- 1)) && flag; i++)
{
flag = false;
for (int j = 0; j < ((depthData.Length)- 1); j++)
{
//You Statements Goes here
flag = true;
}
for (var index = 0; index < depthData.Length; index++) {
int u = (int) Math.Floor(colorPoints[index].X);
int v = (int) Math.Floor(colorPoints[index].Y);
/*
1. not every depth pixel has a corresponding color pixel.
So always check whether u,v are valid or not
*/
if ((0 <= u) && (u < colorWidth) && (0 <= v) && (v < colorHeight)) {
/*
2. now u,v are pixel coordinates in colorspace,
so to get the index of the corresponding pixel,
you need to multiply v by colorWidth instead of depthwidth
*/
int pixelsBaseIndex = v * colorWidth + u;
byte red = pixels[4 * pixelsBaseIndex + 0];
byte green = pixels[4 * pixelsBaseIndex + 1];
byte blue = pixels[4 * pixelsBaseIndex + 2];
byte alpha = pixels[4 * pixelsBaseIndex + 3];
}
}
如果您需要更多参考,请查看以下内容,这是C++中注册的另一种实现。但是你可以理解这个概念。
Surprisingly the minimum index is -2147483648. Any guess?
如果您检查原始深度帧的值,您会发现一些无效的深度值。 Kinect v2 的深度范围是 0.5m 到 4.5m。此范围之外的所有对象都会导致无效的深度值。这些值导致 -2147483648。您可以预处理这些无效值或忽略它们。
我正在尝试获取 Kinect v2 中每个 CameraSpacePoint 的 RGB 值。我能够获取 CameraSpacePoints 和 Color 像素。但似乎在我的代码中从 CameraSpacePoints 到 Color 像素的映射被破坏,这导致我出现 IndexOutOfRangeException。
请查看下面显示数据收集部分的代码片段:
var depthWidth = depthFrame.FrameDescription.Width;
var depthHeight = depthFrame.FrameDescription.Height;
ushort[] depthData = new ushort[depthWidth * depthHeight];
var colorWidth = colorFrame.FrameDescription.Width;
var colorHeight = colorFrame.FrameDescription.Height;
byte[] pixels = new byte[colorWidth * colorHeight * 4];
CameraSpacePoint[] cameraSpacePoints = new CameraSpacePoint[depthData.Length];
ColorSpacePoint[] colorSpacePoints = new ColorSpacePoint[depthData.Length];
depthFrame.CopyFrameDataToArray(depthData);
coordinateMapper.MapDepthFrameToCameraSpace(depthData, cameraSpacePoints);
coordinateMapper.MapDepthFrameToColorSpace(depthData, colorSpacePoints);
// Assuming RGBA format here
colorFrame.CopyConvertedFrameDataToArray(pixels, ColorImageFormat.Rgba);
请查看下面显示 ColorSpacePoints 的 RGB 值的代码:
for (var index = 0; index < depthData.Length; index++)
{
var u = colorSpacePoints[index].X;
var v = colorSpacePoints[index].Y;
int pixelsBaseIndex = (int)(v * depthWidth + u);
byte red = pixels[4 * pixelsBaseIndex + 0];
byte green = pixels[4 * pixelsBaseIndex + 1];
byte blue = pixels[4 * pixelsBaseIndex + 2];
byte alpha = pixels[4 * pixelsBaseIndex + 3];
}
以上代码片段抛出以下错误:
IndexOutOfRangeException: Index was outside the bounds of the array.
为了调试这个问题,我监控了每个索引然后计算了最小和最大索引值如下:
List<int> allIndex = new List<int>();
for (var index = 0; index < depthData.Length; index++)
{
var u = colorpoints[index].X;
var v = colorpoints[index].Y;
int pixelsBaseIndex = (int)(v * depthWidth + u);
allIndex.Add(pixelsBaseIndex);
}
var maxIndex = allIndex.Max();
var minIndex = allIndex.Min();
Console.WriteLine(minIndex);//Prints -2147483648
Console.WriteLine((maxIndex < pixels.Length) && (minIndex >= 0));//Prints False
令人惊讶的是,最小索引是-2147483648。任何猜测?我在这里遗漏了一些明显的东西吗?
试试这个
bool flag = true;
for (int i = 1; (i <= ((depthData.Length)- 1)) && flag; i++)
{
flag = false;
for (int j = 0; j < ((depthData.Length)- 1); j++)
{
//You Statements Goes here
flag = true;
}
for (var index = 0; index < depthData.Length; index++) {
int u = (int) Math.Floor(colorPoints[index].X);
int v = (int) Math.Floor(colorPoints[index].Y);
/*
1. not every depth pixel has a corresponding color pixel.
So always check whether u,v are valid or not
*/
if ((0 <= u) && (u < colorWidth) && (0 <= v) && (v < colorHeight)) {
/*
2. now u,v are pixel coordinates in colorspace,
so to get the index of the corresponding pixel,
you need to multiply v by colorWidth instead of depthwidth
*/
int pixelsBaseIndex = v * colorWidth + u;
byte red = pixels[4 * pixelsBaseIndex + 0];
byte green = pixels[4 * pixelsBaseIndex + 1];
byte blue = pixels[4 * pixelsBaseIndex + 2];
byte alpha = pixels[4 * pixelsBaseIndex + 3];
}
}
如果您需要更多参考,请查看以下内容,这是C++中注册的另一种实现。但是你可以理解这个概念。
Surprisingly the minimum index is -2147483648. Any guess?
如果您检查原始深度帧的值,您会发现一些无效的深度值。 Kinect v2 的深度范围是 0.5m 到 4.5m。此范围之外的所有对象都会导致无效的深度值。这些值导致 -2147483648。您可以预处理这些无效值或忽略它们。