2013-10-05 168 views
1

我一直在研究一個有趣的編碼項目,它使用emguCV以及C#和.NET。我遇到的問題是試圖在我的代碼中初始化Capture()類。每次我嘗試初始化捕獲它拋出一個異常:emgu CV C#捕獲類

The Type initializer for 'Emgu.CV.CvInvoke' threw an Exception 
    Exception type: System.InitializationException from Emgu.CV.dll 

這裏是C#代碼,我有:

class Vision 
{ 
    private Capture cap; 
    private HaarCascade haar; 
    private Form1 form; 

    public Vision() 
    { 
      form = new Form1(); 
      cap = new Capture(); 
      haar = new HaarCascade("C:\\haarcascade_frontalface_alt2.xml"); 
    } 
    public void faceDetect() 
    { 
     using(Image<Bgr, Byte> nextFrame = cap.QueryFrame()) 
     { 
      if(nextFrame != null) 
      { 
       Image<Gray, Byte> grayframe = nextFrame.Convert<Gray, Byte>(); 
       var faces = grayframe.DetectHaarCascade(haar, 1.4, 4, HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(nextFrame.Width/8, nextFrame.Height/8))[0]; 
       foreach(var face in faces) 
       { 
        nextFrame.Draw(face.rect, new Bgr(0, double.MaxValue, 0), 1); 
       } 
       form.setImage(nextFrame.ToBitmap()); 
      } 
     } 
    } 
} 

的代碼的參考文獻:

using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Text; 
    using System.Threading.Tasks; 
    using Emgu.CV; 
    using Emgu.Util; 
    using Emgu.CV.Structure; 
    using Emgu.CV.CvEnum; 
    using System.Drawing; 

每次的發生異常,它顯示在 cap = new Capture();

我也嘗試將Capture類的攝像機索引設置爲0,1,2 ...並且沒有任何運氣。我也想過也許是因爲我有一個在Mac上運行的Windows,它沒有檢測到網絡攝像頭,但後來我下載了最新的Windows驅動程序以訪問攝像頭。我感謝所有提前幫助的人! :-)

+0

EmguCV應該對OpenCV有一些非託管的依賴關係,你確定一切都安裝了嗎? – tbridge

+0

是的,我下載了正確的軟件,並根據隨附的教程添加了依賴關係。 –

+0

我知道,我necroposter,但在Emgu CV 310 Capture被VideoCapture取代。我失去了2天找到它。 –

回答

0

它可能發生在未加載的非託管DLL依賴項時。請嘗試以下的事情:

  1. 確保您使用EmguCV的正確版本,也就是說,如果你正在建設中的x86模式下的解決方案,你應該有EmguCV x86版本,同樣爲x64版本。

  2. 確保OpenCV的DLL是在你的PATH(或直接在bin目錄中,如果這是一個控制檯/的WinForms/WPF應用程序。)

你可以找到如何做到這一點here

您也可以參考官方文檔here

0

這是EmguCV的第一個用戶一個非常「經典」的錯誤,所以不要擔心,很容易解決!

您需要爲您的Visual Studio版本安裝MSVCRT。另外,可能是因爲沒有將DLL文件放在項目中,並且沒有「Copy if newer」。

查看the official emguCV site