Emgu CV 是 OpenCV 跨平台的 C# 封裝包,主要是為了方便在 C# 裡使用 OpenCV 的庫函數,下載和安裝都很簡單,建立一個 C# 控制視窗程序後,Hello World 例子代碼如下:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Drawing;using Emgu.CV;using Emgu.Util;using Emgu.CV.Structure;namespace OpenCVTester{ class Program { static void Main(string[] args) { //The name of the window String win1 = "Test Window"; //Create the window using the specific name CvInvoke.cvNamedWindow(win1); //Create an image of 400x200 of Blue color using (Image<Bgr, Byte> img = new Image<Bgr, byte>(400, 200, new Bgr(255, 0, 0))) { //Create the font MCvFont f = new MCvFont(Emgu.CV.CvEnum.FONT.CV_FONT_HERSHEY_COMPLEX, 1.0, 1.0); //Draw "Hello, world." on the image using the specific font img.Draw("Hello, world", ref f, new Point(10, 80), new Bgr(0, 255, 0)); //Show the image CvInvoke.cvShowImage(win1, img.Ptr); //Wait for the key pressing event CvInvoke.cvWaitKey(0); //Destory the window CvInvoke.cvDestroyWindow(win1); } } }}
這裡需要注意的是,如果用 Visual Studio 2008 來調試上面的代碼,則記得要把 OpenCV 相關的動態連結程式庫放到你測試工程的這個目錄裡:
\bin\Debug
否則在跑到下面這句代碼
CvInvoke.cvNamedWindow(win1);
的時候,就會彈出一個莫名其妙的出錯框,提示如下錯誤:
未處理的"System.TypeInitializationException"類型的異常出現在 OpenCVTester.exe 中。其他資訊: "Emgu.CV.CvInvoke"的類型初始值設定項引發異常
看來,C#調試時的程式動態連結程式庫讀取位置,還比較特殊呀~~