我目前在尝试从 Kinect 深度流中找到特定像素的颜色时遇到了一些困难。下面的代码是我用来计算 (100, 100) 处像素颜色的代码。
我感觉我的逻辑在某处有缺陷(也许在将索引计算为我想要的 colorPixels 时)
colorPixels 和 depthPixels 声明如下:
colorFrame.CopyPixelDataTo(colorPixels); //colorPixels is a byte[]
depthFrame.CopyDepthImagePixelDataTo(depthPixels); //depthPixels is a DepthImagePixel[]
我计算深度流中100,100处像素的rgb值如下:
DepthImagePoint ballDepthPoint = new DepthImagePoint();
int ballPosX = 100;
int ballPosY = 100;
int blueTotal = 0, greenTotal = 0, redTotal = 0;
ColorImagePoint ballColorPoint;
//build a depth point to translate to a color point
ballDepthPoint.X = ballPosX;
ballDepthPoint.Y = ballPosY;
ballDepthPoint.Depth = this.depthPixels[ballDepthPoint.X * ballDepthPoint.Y].Depth;
//work out the point on the color image from this depth point
ballColorPoint = this.sensor.CoordinateMapper.MapDepthPointToColorPoint(this.sensor.DepthStream.Format, ballDepthPoint, this.sensor.ColorStream.Format);
//extract the rgb values form the color pixels array
blueTotal += (int)colorPixels[(ballColorPoint.X * ballColorPoint.Y * colorFrame.BytesPerPixel)];
greenTotal += (int)colorPixels[(ballColorPoint.X * ballColorPoint.Y * colorFrame.BytesPerPixel) + 1];
redTotal += (int)colorPixels[(ballColorPoint.X * ballColorPoint.Y * colorFrame.BytesPerPixel) + 2];
System.Console.WriteLine("The ball found is " + redTotal + "," + blueTotal + "," + greenTotal + " which is " + Helper.ColorChooser(redTotal, greenTotal, blueTotal));
ColorChooser 方法如下:
public static String ColorChooser(int r, int g, int b)
{
if (r >= g && r >= b)
{
return "RED";
}
else if (b >= g && b >= r)
{
return "BLUE";
}
else
return "GREEN";
}
如果您需要更多信息/代码,请告诉我。
非常感谢,
戴夫·麦克布