现在的位置: 首页 > 综合 > 正文

Emgu Cv配置–VS2010、C#、Emgu CV配置

2018年04月03日 ⁄ 综合 ⁄ 共 8623字 ⁄ 字号 评论关闭
以下是对Emgu Cv的配置及测试

一 配置Emgu CV

转自:http://hi.baidu.com/mvp_xuan/item/144c6ddfebb1413de3108fd1

在VS2010(VC10)中配置能使用C#开发语言调用Open CV视觉库的方法如下。众所周知,能供C#使用的OpenCV库叫:Emgu CV。注意用C#开发的话,是不需要单独安装OpenCV的,emgu cv内已经包含!!

————————————————————————————————

文中操作系统:Windows 7 x64 中文旗舰版;
文中编译环境:Visual Studio 2010 中文旗舰版;
Emgu CV(OpenCV for C#)版本:libemgucv-windows-x86-2.3.0.1416.exe (http://cdnetworks-kr-1.dl.sourceforge.net/project/emgucv/emgucv/2.3.0/libemgucv-windows-x86-2.3.0.1416.exe)。

最近有网友说,emguCV的官网已经打不开,估计看来是被国内防火墙限制了,如果需要安装包的,可以发送邮件到我的邮箱,我在线的话,会尽快发送软件给大家。

实在搞不定,加我百度hi:mvp_xuan,或发送邮件到:mvp_xuan@163.com。

OpenCV2.3在C/C++开发环境下的配置

http://hi.baidu.com/mvp_xuan/blog/item/eab5ecd2f6f1fbc5a8ec9a02.html

——————————————————————————————

1、下载Emgu CV的x86安装版

   http://sourceforge.net/projects/emgucv/files/

2、安装

    双击安装文件,一路下一步、我同意,默认配置安装到C盘即可;

3、设置环境变量(设置之后需重启计算机或注销):

PATH(添加如下一行;如无PATH,可自行新建;如修改了Emcu CV的默认安装路径,请自行修改成相应路径):

C:\Emgu\emgucv-windows-x86 2.3.0.1416\bin

有网友说,最新版本的Emgucv,设置环境变量Path时略有不同,dll在bin的子文件夹x86里面,例:

   <安装路径>\emgucv-windows-x86 2.4.0.1717\bin\x86

————————————————————————————————

注意:以下步骤,是每次新建项目之后都必须进行的操作。

————————————————————————————————

4、新建项目

    新建一个解决方案,并建立一个基于C#下的Windows窗体应用程序的项目:

5、导入UI插件

    点击“工具(T)” -> “选择工具箱项(X)…” ,在新打开的窗口中选择:“.NET Framework组件”,点击“浏览”,到目录:C:\Emgu\emgucv-windows-x86 2.3.0.1416\bin下,双击Emgu.CV.UI.dll即可。

    看到如图四项之后,点击确定即可。

6、添加引用

    右键单击“解决方案资源管理器”中刚才所建项目下的“引用”,选择“添加引用”;在新弹出的窗口中选择“浏览”,到目录:C:\Emgu\emgucv-windows-x86 2.3.0.1416\bin下,选择:Emgu.CV.dll、Emgu.CV.ML.dll、Emgu.CV.UI.dll、Emgu.Util.dll、ZedGraph.dll等共五个DLL文件,并点击确定加入。如下图所示即可:

————————————————————————————————

    如上,已经完成所有配置,如果有特殊用途,需要调用到其他的DLL中的方法,请自行在bin目录下,将相应DLL文件引用到项目中即可。

    在网上down一些简单demo的代码,copy到项目中,即可验证配置是否成功了。

————————————————————————————————

常见错误解决方案:

1、如遇错误提示:tbb_debug.dll找不到

    请下载安装Intel TBB库,需要将tbb库或dll文件所在路径,配置带系统环境变量path中。

2、如遇错误提示:“Emgu.CV.CvInvoke”的类型初始值设定项引发异常

    (1)、注销或重启计算机,使得系统环境变量中的path生效;

    (2)、通过path中设置的路径去寻找对应的dll文件,查看是否存在,是否被成功引用到VS中;

    (3)、copy dll文件到debug和release目录中。
    (4)、尝试将.Net FrameWork版本降低,如果是4.0的就降低到3.5,如果是3.5的就降低到2.0。

 

 

二  测试Emgn cv

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;//PS:调用的Emgu dll
using Emgu.CV.Structure;
using Emgu.Util;
using System.Threading;

namespace Emgu1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private Capture capture;
        private bool captureinprocess;//判断摄像头的状态

        private void button1_Click(object sender, EventArgs e)
        {
            if (capture != null)//摄像头不为空
            {
                if (captureinprocess)
                {
                    Application.Idle -= new EventHandler(processfram);
                    button1.Text = "Stop!";
                }
                else
                {
                    Application.Idle += new EventHandler(processfram);
                    button1.Text = "Start!";
                }
                captureinprocess = !captureinprocess;
            }
            else//摄像头为空则通过Capture()方法调用
            {
                try
                {
                    capture = new Capture();
                }
                catch (NullReferenceException excpt)
                {
                    MessageBox.Show(excpt.Message);
                }
            }
        }

        private void processfram(object sender, EventArgs arg)
        {
            Image<Bgr, Byte> frame = capture.QueryFrame();
            imageBox1.Image = frame;
        }
    }
}

 

三 其它较好的测试

转自:http://www.dotblogs.com.tw/chou/archive/2009/06/13/8812.aspx

2011/05/17 在 Visual Studio 2010 使用 OpenCV 對於 WebCam 有問題的話,請參考以下連結

http://social.msdn.microsoft.com/Forums/zh-TW/230/thread/d1647ae6-7f61-453e-818a-2fa4f52592bb

一、簡介

以前研究所的時候,有使用 VC.NET 配合 OpenCV 做影像處理,這東西相當讚,可以省去不少開發時間,今天嘗試看看如何在 Visual C# 2008 上使用 OpenCV。

以下引用
OpenCV 中文網站
的介紹

1. 什麼是 OpenCV
OpenCV是Intel®開源電腦視覺庫。它由一系列 C 函數和少量 C++ 類構成,實現了圖像處理和電腦視覺方面的很多通用演算法

2. OpenCV 重要特性
OpenCV 擁有包括 300 多個C函數的跨平臺的中、高層 API。它不依賴於其它的外部庫——儘管也可以使用某些外部庫。
OpenCV 對非商業應用和商業應用都是免費(FREE)的。(細節參考 license)。
OpenCV 為Intel® Integrated Performance Primitives (IPP) 提供了透明介面。 這意味著如果有為特定處理器優化的的 IPP 庫, OpenCV 將在運行時自動載入這些庫。 更多關於 IPP 的信息請參考:http://www.intel.com/software/products/ipp/index.htm

3. 體驗 OpenCV 的魅力

看了以上對OpenCV的介紹,還是不知道OpenCV在做什麼的話,可以先看這段影片,影片的成果是透過 OpenCV 所撰寫而成。

二、方法

1. 下載與安裝 OpenCV

要使用 OpenCV,首先必須要下載並且安裝,點這裡下載
OpenCV_1.1pre1a.exe
,下載後執行安裝,安裝過程幾乎都是點選下一步,在此需記住安裝目錄為何,預設為 C:\Program Files\OpenCV。

 

2. 下載與安裝 EmguCV

EmguCV 封裝了 OpenCV image processing library,而且可在.Net平台使用,因此在本文中,透過EmguCV,讓我們可以在 Visual C# 2008 中使用OpenCV。

點選這裡下載 Emgu.CV.Binary-1.5.0.1.zip,下載完成後,解壓縮檔案可看EmguCV的DLL檔;我將此目錄放到 C:\Program Files\OpenCV,解壓縮後的檔案只要自己記得就好,因為之後要將這些檔案加入參考。

(1) 下載網頁部份

(2) 下載後解壓縮檔案

3. 新增專案,並且加入相關的參考

先 [ 新增專案 ] -> [ Visual C# ] -> [ WIndows Form 應用程式 ],接著將上個步驟中的 dll 加入參考,要加入的參考請參考下圖。

4. 加入 EmguCV 的控制項,到工具箱中

主要是將 Emgu.CV.UI.dll 加入工具箱中,加入後會出現 ImageBox、HistogramCtrl

5. 到此,已經將要撰寫OpenCV的工作完成了。開始撰寫程式碼,在這裡示範兩個範例,第一個是開啟並顯示 WebCam 畫面。

5.1 先在Form上拉兩個控制項,分別是 Button ( Name : captureButton )與 ImageBox ( Name : captureImageBox )

5.2 撰寫以下程式碼

01 using...System;
02 using System.Collections.Generic;
03 using System.ComponentModel;
04 using System.Data;
05 using System.Drawing;
06 using System.Linq;
07 using System.Text;
08 using System.Windows.Forms;
09
10 // 要引用的類別
11 using Emgu.CV;
12 using Emgu.CV.Structure;
13 using Emgu.Util;
14 using System.Threading;

15
16 namespace MyOpenCV
17 ...{
18    public partialclass
Form2 : Form
19    ...{
20        public Form2()
21        ...{
22             InitializeComponent();
23         }

24
25        private Capture _capture;
26        privatebool
_captureInProgress;
27
28        privatevoid
ProcessFrame(object sender, EventArgs arg)
29        ...{
30             Image<Bgr, Byte> frame = _capture.QueryFrame(); 
31             captureImageBox.Image = frame;
32         }

33
34        privatevoid
captureButton_Click(object sender, EventArgs e)
35        ...{
36            if
capture is not created, create it now
#regionif captureis not created, create
it now
37            if (_capture ==null)
38            ...{
39                try
40                ...{
41                     _capture =new Capture();
42                 }

43                catch (NullReferenceException
excpt)
44                ...{
45                     MessageBox.Show(excpt.Message);
46                 }

47             }

48            #endregion

49
50            if (_capture !=null)
51            ...{
52                if (_captureInProgress)
53                ...//stop
the capture

54                     Application.Idle -=new
EventHandler(ProcessFrame);
55                     captureButton.Text ="Start Capture";
56                 }

57                else
58                ...{
59                    //start the capture
60                     captureButton.Text ="Stop";
61                     Application.Idle +=new
EventHandler(ProcessFrame);
62                 }

63
64                 _captureInProgress = !_captureInProgress;
65             }

66         }

67     }

68 }

5.3 執行結果,請注意喔,以下顯示的是 Webcam 即時視訊,不是影像喔

6. 第二個範例,我們將上一個範例,加上灰階、Canny處理

讓大家了解 OpenCV 的強大,而這動作,只需要多拉兩個ImageBox在加上幾行程式就可以了喔,真是太讚了,難怪很多學生想畢業,都用OpenCV

6.1 在上一個範例中,在多拉兩個 ImageBox,Name 分別為 grayscaleImageBox、cannyImageBox

6.2 將上一個範例,加上四行程式碼就可以達成將Webcam視訊灰階以及Canny

01 using...System;
02 using System.Collections.Generic;
03 using System.ComponentModel;
04 using System.Data;
05 using System.Drawing;
06 using System.Linq;
07 using System.Text;
08 using System.Windows.Forms;
09
10 // 要引用的類別
11 using Emgu.CV;
12 using Emgu.CV.Structure;
13 using Emgu.Util;
14 using System.Threading;

15
16 namespace MyOpenCV
17 ...{
18    public partialclass
Form2 : Form
19    ...{
20        public Form2()
21        ...{
22             InitializeComponent();
23         }

24
25        private Capture _capture;
26        privatebool
_captureInProgress;
27
28        privatevoid
ProcessFrame(object sender, EventArgs arg)
29        ...{
30             Image<Bgr, Byte> frame = _capture.QueryFrame(); 
31             captureImageBox.Image = frame;
32
33            // 請再加上以下四行程式碼
34             Image<Gray, Byte> grayFrame = frame.Convert<Gray, Byte>();
35             Image<Gray, Byte> cannyFrame = grayFrame.Canny(new
Gray(100), new Gray(60));
36             grayscaleImageBox.Image = grayFrame;
37             cannyImageBox.Image = cannyFrame;
38         }

39
40        privatevoid
captureButton_Click(object sender, EventArgs e)
41        ...{
42            if
capture is not created, create it now
#regionif captureis not created, create
it now
43            if (_capture ==null)
44            ...{
45                try
46                ...{
47                     _capture =new Capture();
48                 }

49                catch (NullReferenceException
excpt)
50                ...{
51                     MessageBox.Show(excpt.Message);
52                 }

53             }

54            #endregion

55
56            if (_capture !=null)
57            ...{
58                if (_captureInProgress)
59                ...//stop
the capture

60                     Application.Idle -=new
EventHandler(ProcessFrame);
61                     captureButton.Text ="Start Capture";
62                 }

63                else
64                ...{
65                    //start the capture
66                     captureButton.Text ="Stop";
67                     Application.Idle +=new
EventHandler(ProcessFrame);
68                 }

69
70                 _captureInProgress = !_captureInProgress;
71             }

72         }

73     }

74 }

6.3 執行結果

三、範例下載

http://cid-101d8ba47227b414.skydrive.live.com/self.aspx/.Public/MyOpenCV.rar

四、遇到錯誤 'Emgu.CV.CvInvoke' 的型別初始設定式發生例外狀況

1. 將您的 OpenCV 資料夾路徑與 Emgu 資料夾路徑加入 Windows 環境變數。

2. 使用 Visual Studio 開啟您的專案。

    (1) 在專案上按滑鼠右鍵,選擇 [屬性] / [參考路徑],加入你的 OpenCV 與 Emgu 的安裝路徑。

    (2) 在建置組態中,把平台改成 X86,參考
HOW TO:將專案設定成以平台為目標

 

 

抱歉!评论已关闭.