2014-12-22 180 views
0

我正在使用Microsoft Kinect SDK v2從Kinect V2傳感器讀取顏色框。我將幀數據複製到一個字節數組中,該數組稍後轉換爲EmguCV圖像。以下是代碼片段 -縮放後圖像損壞

// A pixel buffer to hold image data from the incoming color frame 
private byte[] pixels = null; 
private KinectSensor kinectSensor = null; 
private ColorFrameReader colorFrameReader = null; 
public KinectForm() 
{ 
    this.kinectSensor = KinectSensor.GetDefault(); 
    this.colorFrameReader = this.kinectSensor.ColorFrameSource.OpenReader(); 
    this.colorFrameReader.FrameArrived += this.Reader_ColorFrameArrived; 
    // create the colorFrameDescription from the ColorFrameSource using Bgra format 
    FrameDescription colorFrameDescription = this.kinectSensor.ColorFrameSource.CreateFrameDescription(ColorImageFormat.Bgra); 
    // Create a pixel buffer to hold the frame's image data as a byte array 
    this.pixels = new byte[colorFrameDescription.Width * colorFrameDescription.Height * colorFrameDescription.BytesPerPixel]; 
    // open the sensor 
    this.kinectSensor.Open(); 
    InitializeComponent(); 
} 

private void Reader_ColorFrameArrived(object sender, ColorFrameArrivedEventArgs e) 
{ 
    using (ColorFrame colorFrame = e.FrameReference.AcquireFrame()) 
    { 
     if (colorFrame != null) 
     { 
      FrameDescription colorFrameDescription = colorFrame.FrameDescription; 
      if (colorFrame.RawColorImageFormat == ColorImageFormat.Bgra) 
       colorFrame.CopyRawFrameDataToArray(pixels); 
      else 
       colorFrame.CopyConvertedFrameDataToArray(this.pixels, ColorImageFormat.Bgra); 

      //Initialize Emgu CV image then assign byte array of pixels to it 
      Image<Bgr, byte> img = new Image<Bgr, byte>(colorFrameDescription.Width, colorFrameDescription.Height); 
      img.Bytes = pixels; 

      imgBox.Image = img;//Show image in Emgu.CV.UI.ImageBox 
     } 
    } 
} 

轉換後的圖像在縮放超過25%後損壞。請參考下面screenshots-

50%縮放 - 50% Zoom

25%縮放 - 25% Zoom

12.5%縮放 - 12.5% Zoom

回答

1

的< TColor>應該是BGRA,可以確認來自「像素」字節數組。由於Kinect的顏色框的強度爲0,圖像將不可見,所以我添加了這個代碼來解決這個問題,我希望有一個更好的(更快)的方法來解決它。

private void FixIntensity(byte[] p) 
    { 
     int i = 0; 
     while (i < p.Length) 
     { 
      p[i+3] = 255; 
      i += 4; 
     } 
    }