2013-01-21 56 views
2

我目前在嘗試從Kinect Depth Stream中找到特定像素的顏色時有點困難。下面的代碼是我用來計算(100,100)像素的顏色。從Kinect Depth Stream獲取像素顏色

我有我的邏輯是什麼地方有缺陷的感覺(也許是計算索引,我想colorPixels時)

colorPixels和depthPixels聲明如下:

colorFrame.CopyPixelDataTo(colorPixels); //colorPixels is a byte[] 
depthFrame.CopyDepthImagePixelDataTo(depthPixels); //depthPixels is a DepthImagePixel[] 

我計算的RGB值在100,100中的深度流的像素的如下:

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"; 
    } 

如果您需要更多信息/代碼,請告訴我。

非常感謝,

戴夫MCB

+0

嘿Dave,你首先用什麼來讀取深度數據流?我會假設專有的微軟工具,但我想確定。 – Jacksonkr

回答

2

了那裏到底,正確的方法,以指數的彩色像素的像素似乎是:

colorPixels[(ballColorPoint.X * colorFrame.BytesPerPixel) + (ballColorPoint.Y * stride)]; 

其中:

int stride = colorFrame.BytesPerPixel * colorFrame.Width; 
+0

這是一個Kinect編程書中的例子嗎?我想我以前看過它。 –