2013-02-15 126 views
1

我正在爲涉及C#,Kinect和Emgu CV的學校開發一個項目。我對C#和Emgu CV都很新,所以我可能會錯過簡單的東西。我想要做的是使用Kinect中的圖像作爲Emgu CV圖像進行處理,但是我一直收到錯誤。出現的錯誤是「TypeInitializationException未處理」和「Emgu.Cv.CVInvoke的類型初始值設定項引發異常。」與Emgu CV一起使用Kinect ColorImageFrame CV

我使用從Kinect的獲取圖像的代碼是:

using (ColorImageFrame colorFrame = e.OpenColorImageFrame()) 
{ 
    if (colorFrame != null) 
    {   
     byte[] pixels = new byte[colorFrame.PixelDataLength]; 
     colorFrame.CopyPixelDataTo(pixels); 

     int stride = colorFrame.Width * 4; 
     BitmapSource color = BitmapImage.Create(colorFrame.Width, colorFrame.Height,96, 96, PixelFormats.Bgr32, null, pixels, stride); 
     liveFeed.Source = color; 

     EmguCVProcessing(color); 
    } 
    else 
    { 
     return; 
    } 
} 

我還發現,我使用到的BitmapSource從轉換爲位圖一些代碼:Is there a good way to convert between BitmapSource and Bitmap?

的不工作的代碼如下:

void EmguCVProcessing(BitmapSource bitmap) 
{ 
    Bitmap bmp = GetBitmap(bitmap); 

    Image<Bgr, Byte> imgLiveFeed = new Image<Bgr, Byte>(bmp); 
} 

從我能找到,這應該位圖轉換成Emgu CV形象,但由於某種原因,事實並非如此。

只是一些詳細信息:

的InnerException:確保文件的圖像是一個有效的管理組件。

InnerException:確保您爲程序集提供了正確的文件路徑。

我最好的猜測是該程序使用不同版本的.NET Framework,但我不確定如何解決這個問題。

回答

0

我得到了同樣的問題和半合適的解決方案。所以我也希望有人有一個好主意。

我目前做的是通過像素轉換圖像像素:

private KinectSensor sensor; 
private byte[] colorPixels; 
private Image<Gray, UInt16> grayImage; 
// during init: 
this.colorPixels = new byte[this.sensor.ColorStream.FramePixelDataLength]; 
grayImage = new Emgu.CV.Image<Gray, UInt16>(this.sensor.ColorStream.FrameWidth, this.sensor.ColorStream.FrameHeight, new Gray(0)); 

// the callback for the Kinect SDK 
private void SensorColorFrameReady(object sender, ColorImageFrameReadyEventArgs e) 
{ 
    using (ColorImageFrame colorFrame = e.OpenColorImageFrame()) 
    { 
     if (colorFrame != null) 
     { 
      // Copy the pixel data from the image to a temporary array 
      colorFrame.CopyPixelDataTo(this.colorPixels); 

      int width = 640; 
      int height = 480; 
      int bytesPerPx = 2; 
      if (nthShot == 0) 
      { 
       for (int y = 0; y < height; y++) 
       { 
        for (int x = 0; x < width; x++) 
        { 
         grayImage[height - y -1, x] = new Gray(((this.colorPixels[(y * width + x) * bytesPerPx + 1]))); 
        } 
       } 
       // *** processing of the image comes here *** 
      } 
      nthShot++; 
      if (nthShot == 4) 
       nthShot = 0; 
     } 
    } 
} 

然而,這種做法是相當慢的,所以我只處理每一個第n個樣本。

這將是真棒,如果有人有更好/更快的解決方案:)

感謝,

弗洛

0

TypeInitializationException可能與未能找到所需的DLL二進制Emgu CV。

這個問題解決您的問題: EmguCV TypeInitializationException

此外,如果你想要一個ColorFrame從Kinect的V2轉換成Emgu CV處理的圖像(Image<Bgra,byte>),您可以使用此功能:

/** 
* Converts the ColorFrame of the Kinect v2 to an image applicable for Emgu CV 
*/ 
    public static Image<Bgra, byte> ToImage(this ColorFrame frame) 
    { 
     int width = frame.FrameDescription.Width; 
     int height = frame.FrameDescription.Height; 
     PixelFormat format = PixelFormats.Bgr32; 

     byte[] pixels = new byte[width * height * ((format.BitsPerPixel + 7)/8)]; 

     if (frame.RawColorImageFormat == ColorImageFormat.Bgra) 
     { 
      frame.CopyRawFrameDataToArray(pixels); 
     } 
     else 
     { 
      frame.CopyConvertedFrameDataToArray(pixels, ColorImageFormat.Bgra); 
     } 

     Image<Bgra, byte> img = new Image<Bgra, byte>(width, height); 
     img.Bytes = pixels; 

     return img; 
    }