我玩了一下Kinect v2和C#,并尝试获得一个512x424像素大小的图像数组,其中包含深度数据以及相关颜色信息(RGBA)。
因此,我使用MultiSourceFrameReader类来接收MultiSourceFrame,从中获取ColorFrame和DepthFrame。使用ColorFrame.CopyConvertedFrameDataToArray()和DepthFrame.CopyFrameDataToArray()方法,我收到了保存颜色和深度信息的数组:
// Contains 4*1920*1080 entries of color-info: BGRA|BGRA|BGRA..
byte[] cFrameData = new byte[4 * cWidth * cHeight];
cFrame.CopyConvertedFrameDataToArray(cFrameData, ColorImageFormat.Bgra);
// Has 512*424 entries with depth information
ushort[] dFrameData = new ushort[dWidth* dHeight];
dFrame.CopyFrameDataToArray(dFrameData);现在,我必须将居住在ColorFrame数据数组cFrameData中的颜色四重图映射到DepthFrame-数据数组dFrameData的每个条目,但这就是我被困的地方。输出应该是dFrameData数组大小的4倍(RGBA/BGRA),并包含到深度帧的每个像素的颜色信息:
// Create the array that contains the color information for every depth-pixel
byte[] dColors = new byte[4 * dFrameData.Length];
for (int i = 0, j = 0; i < cFrameData.Length; ++i)
{
// The mapped color index. ---> I'm stuck here:
int colIx = ?;
dColors[j] = cFrameData[colIx]; // B
dColors[j + 1] = cFrameData[colIx + 1]; // G
dColors[j + 2] = cFrameData[colIx + 2]; // R
dColors[j + 3] = cFrameData[colIx + 3]; // A
j += 4;
}有人有什么建议吗?
我还看了一下Kinect-SDK的CoordinateMappingBasics示例,但是对于我已经开始工作的1920x1080像素大小的图像,他们也是这样做的。
编辑
我认识到,我应该能够通过ColorSpacePoint-struct获得映射的颜色信息,它包含特定颜色像素的X和Y坐标。因此,我设置了这样的点。
// Lookup table for color-point information
ColorSpacePoint[] cSpacePoints = new ColorSpacePoint[dWidth * dHeight];
this.kinectSensor.CoordinateMapper.MapDepthFrameToColorSpace(dFrameData, cSpacePoints);。。并试图访问颜色信息,比如..。
int x = (int)(cSpacePoints[i].X + 0.5f);
int y = (int)(cSpacePoints[i].Y + 0.5f);
int ix = x * cWidth + y;
byte r = cFrameData[ix + 2];
byte g = cFrameData[ix + 1];
byte b = cFrameData[ix];
byte a = cFrameData[ix + 3];。。但我还是搞错颜色了。大多是白色的。
发布于 2018-03-26 18:49:01
我自己想出来的。这个错误是微不足道的。由于数组不是一个像素数组,其中一个条目包含RGBA信息,而是一个字节数组,其中每个条目代表R、G、B或A,所以我必须将索引乘以每个像素值的字节数(在本例中为4 )。所以解决方案如下:
int ix = (x * cWidth + y) * 4;
byte r = cFrameData[ix + 2];
byte g = cFrameData[ix + 1];
byte b = cFrameData[ix];
byte a = cFrameData[ix + 3];https://stackoverflow.com/questions/49352126
复制相似问题