2015年8月30日 星期日

Emgu CV (3) Example1

不同版本的Emgu CV支援不同版本的Visual Studio,因為我這裡使用Visual Studio 2008(VC9),
因此我重新選擇下載Emgu CV 2.1.0版並重新安裝。

1. 新增專案,選擇"Windows Form應用程式"選項,專案"名稱"輸入"(這裡我使用預設值)",之後按"確定"。

2. 加入函式庫,點選"參考"按"滑鼠右鍵",選擇"加入參考(R)..."。

3. 至Emgu CV安裝路徑"\bin"下,選擇"瀏覽",將"Emgu.CV.dll、Emgu.CV.ML.dll、Emgu.CV.UI.dll、Emgu.Util.dll、ZedGraph.dll"檔加入,後按"確認"。

4. 撰寫範例程式,增加"Button"與"PictureBox"控制項,之後使用"滑鼠快點兩下"Button。

5.  輸入範例程式,最後按"開始偵錯"按鈕進行編譯。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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;
using Emgu.CV.Structure;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog Openfile = new OpenFileDialog();
            if (Openfile.ShowDialog() == DialogResult.OK)
            {
                Image<Bgr, byte> My_Image = new Image<Bgr, byte>(Openfile.FileName);
                pictureBox1.Image = My_Image.ToBitmap();
            }
        }
    }
}

6. 執行結果。



#增加Emgu CV控制項。

1. 增加工具箱設定,選擇"工具(T)" > "選擇工具箱項目(X)..."。

2.  選擇"瀏覽(B)..."。

3. 至Emgu CV安裝路徑"\bin"下,選擇"Emgu.CV.UI.dll",按"開起舊檔(O)"。

4. 按"確定"結束工具箱設定。

5. 工具箱增加了新的控制項。

Open CV (3) Example1

1. 新增專案,選擇"Win32主控台應用程式"選項,專案"名稱"輸入" Example",之後按"確定"。

2. 按"下一步 >"。

3. 取消"先行編譯標頭檔(P)",之後按"完成"。

4. 選擇"工具(T)" > "選項(O)..."。

5. 選擇"專案和方案" > "VC++目錄",在"顯示目錄(S):" 下拉式選單中,選擇"Include檔案",增加路徑"C:\OpenCV2.1\include\opencv"。

6. 選擇"專案和方案" > "VC++目錄",在"顯示目錄(S):" 下拉式選單中,選擇"程式庫檔",增加路徑"C:\OpenCV2.1\lib",最後按"確定"。

7. 點選"方案總管"下的專案名稱,按"滑鼠右鍵" > 屬性(R)。

8. 選擇"連結器" > "輸入","組態(C):"為"Debug",在"其他相依性"欄位中輸入
"cxcore210d.lib cv210d.lib highgui210d.lib"。

1
cxcore210d.lib cv210d.lib highgui210d.lib

9. 選擇"連結器" > "輸入","組態(C):"為"Release",在"其他相依性"欄位中輸入
"cxcore210d.lib cv210d.lib highgui210d.lib",之後按"確定"。

1
cxcore210d.lib cv210d.lib highgui210d.lib

10. 輸入範例程式,最後按"開始偵錯"按鈕進行編譯。

1
2
3
4
5
6
7
8
9
10
11
12
13
#include "stdafx.h"
#include <highgui.h>
int _tmain(int argc, _TCHAR* argv[])
{
    IplImage* img = cvLoadImage("rgb.bmp");
    cvNamedWindow("Example1", CV_WINDOW_AUTOSIZE);
    cvShowImage("Example1", img);
    cvWaitKey(0);
    cvReleaseImage(&img);
    cvDestroyWindow("Example1");
    return 0;
}

11. 顯示結果。

PS: 再編譯程式前,請在專案的原始程式路徑中,自行加入"rgb.bmp"圖檔。