C# 尝试从C中的Kinect v2获取颜色值时发生IndexOutfrange异常#
我正在尝试获取Kinect v2中每个CameraSpacePoint的RGB值。尝试访问转换的帧数据时,代码抛出C# 尝试从C中的Kinect v2获取颜色值时发生IndexOutfrange异常#,c#,kinect,kinect-v2,C#,Kinect,Kinect V2,我正在尝试获取Kinect v2中每个CameraSpacePoint的RGB值。尝试访问转换的帧数据时,代码抛出索引自动失效异常。我发现以下行抛出了此错误: byte red = pixels[4 * pixelsBaseIndex + 0]; byte green = pixels[4 * pixelsBaseIndex + 1]; byte blue = pixels[4 * pixelsBaseIndex + 2]; byte alpha = pixels[4 * pixelsBa
索引自动失效异常
。我发现以下行抛出了此错误:
byte red = pixels[4 * pixelsBaseIndex + 0];
byte green = pixels[4 * pixelsBaseIndex + 1];
byte blue = pixels[4 * pixelsBaseIndex + 2];
byte alpha = pixels[4 * pixelsBaseIndex + 3];
请参阅下面的完整代码:
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));
}
}
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=新的ushort[depthWidth*depthHeight];
CameraSpacePoint[]cameraPoints=新的CameraSpacePoint[depthData.Length];
ColorSpacePoint[]colorPoints=新的ColorSpacePoint[depthData.Length];
//这里假设RGBA格式
字节[]像素=新字节[colorWidth*colorHeight*4];
depthFrame=multiSourceFrame.DepthFrameReference.AcquireFrame();
colorFrame=multiSourceFrame.ColorFrameReference.AcquireFrame();
if((depthFrame==null)| |(colorFrame==null))
{
返回;
}
depthFrame.CopyFrameDataToArray(depthData);
coordinateMapper.MapDepthFrameToCameraSpace(depthData、cameraPoints);
coordinateMapper.MapDepthFrameToColorSpace(depthData,colorPoints);
//我们使用的是RGBA格式
CopyConvertedFrameDataToArray(像素,ColorImageFormat.Rgba);
对于(var index=0;index=颜色宽度| | v<0 | | v>=颜色高度)继续;
int pixelsBaseIndex=(int)(v*colorWidth+u);
尝试
{
字节红色=像素[4*pixelsBaseIndex+0];
字节绿色=像素[4*pixelsBaseIndex+1];
字节蓝色=像素[4*pixelsBaseIndex+2];
字节alpha=像素[4*像素基本索引+3];
}
捕获(IndexOutOfRangeException ex)
{
int minValue=4*像素基索引;
int maxValue=4*pixelsBaseIndex+3;
Console.WriteLine((minValue>0)+“,”+(maxValue
代码看起来不错,但我不确定我在这里遗漏了什么如何避免索引自动失效异常异常?有什么建议吗?第一件事
pixelsBaseIndex=<2070601
(2070601=1919*1079)
2074817
对于pixelsBaseIndex
是无效的值
这个错误可能在这里的某个地方
if (u < 0 || u >= colorWidth || v < 0 || v >= colorHeight) continue;
int pixelsBaseIndex = (int)(v * colorWidth + u);
FYI
查看以下代码行,了解为什么u、v像素坐标获得浮点值。决定是发言还是放弃无效值取决于开发人员,因为在这两种情况下,您都会丢失某种信息。例如,选择地板意味着选择最近像素的颜色
在我个人的选择中,输出更精确的值(因此是浮点值)对于对输出值进行更多计算的开发人员来说很重要。例如,点云注册
您是否尝试控制台记录像素基索引和像素。长度,我猜颜色点可能会导致奇怪的索引,因为我了解CopyConvertedFrameDataToArray为4创建字节数组bytes@whoisthis:非常感谢。我做到了。请参阅此
Console.WriteLine(“pixelsBaseIndex:+pixelsBaseIndex+”,pixels.Length:+pixels.Length+”,minValue:+minValue+,maxValue:+maxValue”)代码>打印像素基索引:2074817,像素。长度:8294400,最小值:8299268,最大值:8299271
像素基索引正在更改,因此上述输出仅为一个snippet@whoisthis:有什么建议吗?我记录了所有u
和v
变量,并打印了最大/最小值。看到这个minu:169.4572,maxu:1750.199,minv:0.009765625,maxv:1079.988,colorWidth:1920,colorHeight:1080
如果我计算pixelsBaseIndex
,基于这些值,它会使索引自动失效<代码>1079.988*1920+1750.199=2075327.159
这是可行的。我意识到问题的出现是因为后来的casting
。但是,我无法理解的事实是,的像素坐标处于浮动状态!
...
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;
...