2014-04-01 73 views
1

我知道如何去相反的方向。我正在尋找的是,如果在像素空間(1920×1080圖像)中給出(x,y)座標,我如何獲得深度的相應(如果可用)(x,y,z)(以米爲單位)圖片。我意識到有更多的像素比體素,它可能是不可能找到任何,但微軟的SDK有一個CoordinateMapper類。這暴露了MapColorFrameToCameraSpace函數的。如果我使用它,我可以在相機空間(x,y,z)中獲取點陣列,但我無法弄清楚如何提取特定像素的映射。在Kinect SDK v2.0中,如何將彩色圖像中的像素映射到深度圖像中的體素?

回答

1

您可能需要使用

CoordinateMapper.MapDepthFrameToColorSpace

找到所有深點的顏色空間座標。然後,將您的像素座標(x,y)與這些顏色空間座標進行比較。我的解決方案是找到最近的點(可能有更好的方法),因爲映射的座標是浮點數。

如果您使用C#,這裏是代碼。希望能幫助到你!

private ushort GetDepthValueFromPixelPoint(KinectSensor kinectSensor, ushort[] depthData, float PixelX, float PixelY) 
    { 
     ushort depthValue = 0; 
     if (null != depthData) 
     { 
      ColorSpacePoint[] depP = new ColorSpacePoint[512 * 424]; 

      kinectSensor.CoordinateMapper.MapDepthFrameToColorSpace(_depthData, depP); 

      int depthIndex = FindClosestIndex(depP, PixelX, PixelY); 

      if (depthIndex < 0) 
       Console.WriteLine("-1"); 
      else 
      { 
       depthValue = _depthData[depthIndex]; 
       Console.WriteLine(depthValue); 
      } 
     } 
     return depthValue; 
    } 

    private int FindClosestIndex(ColorSpacePoint[] depP, float PixelX, float PixelY) 
    { 
     int depthIndex = -1; 
     float closestPoint = float.MaxValue; 
     for (int j = 0; j < depP.Length; ++j) 
     { 
      float dis = DistanceOfTwoPoints(depP[j], PixelX, PixelY); 
      if (dis < closestPoint) 
      { 
       closestPoint = dis; 
       depthIndex = j; 
      } 
     } 
     return depthIndex; 
    } 

    private float DistanceOfTwoPoints(ColorSpacePoint colorSpacePoint, float PixelX, float PixelY) 
    { 
     float x = colorSpacePoint.X - PixelX; 
     float y = colorSpacePoint.Y - PixelY; 
     return (float)Math.Sqrt(x * x + y * y); 
    } 
相關問題