[轉載+原創]Emgu CV on C# (一) —— Emgu CV on Visual C# 2010

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   java   使用   os   

2014-08-16

最近要進行Image Recognition,準備利用幾天的時間研究一下Emgu CV,花了一晚上功夫進行調試環境安裝,期間遇到了不少問題,現梳理一下安裝過程和調試過程中出現的問題。

中間有轉載別人的部分,有簡體和繁體,還摻雜英文,就不整理了,基本都能看懂。

一、環境建立準備

本篇著在於使用Visual C# 2010 Express + EmguCV環境的建置。

環境:


Visual C# 2010 Express 下載點:
http://www.microsoft.com/visualstudio/en-us/products/2010-editions/visual-csharp-express 

EmguCV 下載點:

檔案名稱是2.9版本,但個人感覺是2.4.9版本。

libemgucv-windows-universal-cuda-2.9.0.1922-beta.exe (214.0 MB)

2.4.0版本

thunder://QUFodHRwOi8vaXdlYi5kbC5zb3VyY2Vmb3JnZS5uZXQvcHJvamVjdC9lbWd1Y3YvZW1ndWN2LzIuNC4wL2xpYmVtZ3Vjdi13aW5kb3dzLXg4Ni0yLjQuMC4xNzE3LmV4ZVpa/

截至2014年8月16日,emgucv的版本為2.9.0.1922-beta。2.4.2及之前版本,若安裝檔案檔案名稱中帶gpu字樣的,則僅限支援CUDA的顯卡使用;不帶gpu字樣的,則通用。而通用版目前的最高版本為2.4.0。

OpenCV下載點:

截至2014年8月16日,OpenCV的穩定版本為opencv-2.4.9.exe (365.7 MB)

鑒於目前調試功能不是很複雜,根據部分網友反饋和調試經驗,即使不安裝opencv,emgucv也能正常運行。但據反饋,如果兩個版本不匹配,會出現錯誤,如果要裝opencv,最好保證兩個軟體版本一致。

二、安裝調試環境

opencv安裝

為了便於管理,我在C盤根目錄下建立了檔案夾,然後解壓至下面。

EmguCV:

過程類似。我安裝在C盤下面,以便統一管理。

三、參數配置(轉載)

轉載部分是以2.3.0版為基礎,但和2.4.0配置方式一樣,供參考。

Emgucv完整路徑是C:\Emgu\emgucv-windows-x86 2.3.0.1416\bin
OpenCV完整路徑是C:\opencv\opencv\build\x86\vc10\bin

因此要將這兩個路徑加進Windows環境變數裡。實際只添加了opencv環境變數。

環境變數的設定方法與設定Java的方法一樣,將
C:\Emgu\emgucv-windows-x86 2.3.0.1416\bin;C:\opencv\opencv\build\x86\vc10\bin;
填入Variable value欄位內的最前面。要注意到路徑中間及最後面需要加上“;”,如果漏加了,評論就是一長串的問題,然後我也不知道你的問題在哪裡。

到目前已經算是把軟體都安裝好,也設定完成了。

四、調試環境配置

將Emgu.CV.UI.dll加入工具箱內 

工具>選擇工具箱項目>.NetFrameworkm元件頁籤點內選瀏覽>

Emgu.CV.UI.dll的路徑在C:\Emgu\emgucv-windows-x86 2.3.0.1416\bin

加入工具箱以後,我們可以確認一下有沒有成功加入成功。

若成功加入工具箱則會在工具箱內自動新增四個元件

 

最後在加入開發程式時所需的dll函式庫,就可以開始編程。

 

包括Emgu.CV.dll、Emgu.CV.ML.dll、Emgu.CV.UI.dll、Emgu.Util.dll、ZedGraph.dll等檔案。

相關的dll檔一樣在C:\Emgu\emgucv-windows-x86 2.3.0.1416\bin底下

至於需要加那些dll,則看需求。

到目前為止,從環境的建置一直到建立專案完整的步驟都如上面所述。

五、調試測試

是否能正常運行,用下面的例子可以測試一下,如果正常,可以快顯視窗顯示一張圖片。

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using Emgu.CV;using Emgu.Util;namespace EmguDemo{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void button1_Click(object sender, EventArgs e)        {            CvInvoke.cvNamedWindow("Hello");            IntPtr img = CvInvoke.cvLoadImage("C:\\1.jpg", Emgu.CV.CvEnum.LOAD_IMAGE_TYPE.CV_LOAD_IMAGE_ANYCOLOR);            CvInvoke.cvShowImage("Hello", img);            CvInvoke.cvWaitKey(0);            CvInvoke.cvReleaseImage(ref img);            CvInvoke.cvDestroyWindow("Hello");        }    }}

注意更改圖片的絕對路徑。這種錯誤不要再犯。

六、錯誤處理

大部分遇到的問題都是一樣,“cvinvoke”異常。

個人感覺,還是DLL連結庫沒有完整拷貝到程式檔案夾導致的。這裡給出幾個解決方案供大家參考。 (一)“EmguCV+VS2010 C#,cvinvoke異常的解決辦法”http://blog.sina.com.cn/s/blog_6233611f01016mpj.html嘗試了一下,沒通過,但是後來重新看了一下,是我沒有嚴格按照步驟處理,但後來的解決方案,和他的差不多。學習不認真( ⊙ o ⊙ )啊!(二)EMGU官網給出的權威解決辦法,全英文,比較簡單,翻譯一下關鍵區段。

Have you installed MSVCRT?

  • For Version 2.4+, the bundled OpenCV binary is build with Visual Studio 2010, you will needs to installed MSVCRT 10.0 SP1 x86 or MSVCRT 10.0 SP1 x64 to resolve the dependency issue.

是否安裝了Microsoft Visual C++ 2010 SP1可轉散發元件包x86,現在基本都帶了,這個不是問題。

Have you copied the OpenCV dlls to the execution directory?

Make sure the unmanaged DLLs are in the execution directory.

  • For EMGU CV version 2.4 cudart64_42_9.dll, cvextern.dll, npp64_42_9.dll, opencv_calib3dXXX.dll, opencv_contribXXX.dll, opencv_coreXXX.dll, opencv_features2dXXX.dll, opencv_flannXXX.dll, opencv_highguiXXX.dll, opencv_imgprocXXX.dll, opencv_legacyXXX.dll, opencv_mlXXX.dll, opencv_nonfreXXX.dll, opencv_objectdetectXXX.dll, opencv_videoXXX.dll, where XXX is the OpenCV version number.
  • For Emgu CV version >=2.4.2, this means the "x86" and "x64" folder and all the dlls within the folders. The folder names and file structures should not be altered when deploying with the application.

確認unmanaged DLLs 是否已經拷貝到程式檔案夾。o(╯□╰)o  天生的缺陷。

這步很重要,把相關dll拷貝過去就OK了。

 

The best way to set up your project is:

  • Copy the unmanaged DLLs to your project folder
  • Right click on the project, click Add->Existing Item and select all unmanaged DLLs. Add them to the project.
  • For each of the included Dlls, left click on it, find the "Copy to Output Directory" option and select "Copy if newer"
最好的方法,就是把用到的unmanaged DLLs添加到工程中。 5點半起來送完客人,花了一個半小時把昨天的經驗總結一下,頭腦清爽,早上幹活效率真是高效,寫完可以去睡覺了。 
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.