Emgu CV的配置以及在VS 2012中進行圖像處理的步驟和實例


說明:

1、所使用的Emgu CV是目前的最新版本3.1.0,下載鏈接為:https://sourceforge.net/projects/emgucv/files/emgucv/3.1.0/(我選的下載量最多的那個版本,40.3MB)

2、經測試,在VS 2012中調用Emgu CV進行編程,不需要先安裝OpenCV。

3、可以不配置環境變量,也可以配置環境變量,具體后面有說明。

4、目前本人的計算機系統是Windows 7 64位,但是在項目的屬性中,只有將目標平台設置為x86才行。

 

安裝和配置步驟:

1、安裝下載的可執行程序,全部按照默認設置。這個版本的EmguCV默認安裝的話,它的路徑是:C:\Emgu\emgucv-windesktop 3.1.0.2282

 

2、配置環境變量,在系統變量中找到Path或者PATH(如果沒有則新建Path),然后在最前面添加(如果你安裝時選的不是默認安裝路徑,可能會有不同):

C:\Emgu\emgucv-windesktop 3.1.0.2282\bin\x64;C:\Emgu\emgucv-windesktop 3.1.0.2282\bin;C:\Emgu\emgucv-windesktop 3.1.0.2282\bin\x86;

 

如果是你的本賬戶使用,也可以在“用戶變量”中新建Path,然后將上面的三條路徑添加進去。如果想讓所有用這台計算機的人使用,則推薦在“系統變量”的Path中添加這些路徑。

如果不想配置環境變量,那么需要將原EmguCV的安裝目錄的bin文件下的x86文件夾整個拷到當前程序的Debug目錄下:

 

 

 3、打開VS 2012 ,新建一個Winform程序。在“工具箱”中右鍵“選擇項”,添加Emgu.CV.UI.dll(在EmguCV安裝的bin目錄中),添加以后,在“工具箱”的“所有Windows窗體”中會多出來4個控件:

4、在“解決方案資源管理器”中,添加“引用”,在本版本的EmguCV中,dll的數量得到了整合,一眼看去“估計”有用的dll一共只有4個:

本例中,我們需要導入:Emgu.CV.UI.dll、Emgu.CV.World.dll

 

5、然后在程序中導入命名空間:using Emgu.CV; 

 

6、然后拖3個ImageBox到主窗體,拖3個TextBox和4個Button到主窗體,如圖所示:

功能說明:點擊button1添加圖片並顯示到ImageBox1中;點擊button2將ImageBox1中的圖片去色,並將灰度圖顯示到ImageBox2中;點擊button3直接載入新的圖片並去色,然后顯示到ImageBox3中;點擊button4清除所有的圖片和文本框中的內容。

 完整程序如下所示:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Emgu.CV;  

namespace WindowsFormsApplication8
{
    public partial class Form1 : Form
    {
        Mat img1 = null;
        Mat img2 = null;
        Mat img3 = null;
        Stopwatch sw = new Stopwatch();
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog lvse = new OpenFileDialog();
            lvse.Title = "選擇圖片";
            lvse.InitialDirectory = "";
            lvse.Filter = "圖片文件|*.bmp;*.jpg;*.jpeg;*.gif;*.png";
            lvse.FilterIndex = 1;

            if (lvse.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = null;
                
                sw.Restart();

                img1 = CvInvoke.Imread(lvse.FileName, Emgu.CV.CvEnum.LoadImageType.AnyColor);

                imageBox1.Width = img1.Width / 2;
                imageBox1.Height = img1.Height / 2;
                imageBox1.Image = img1;
                
                sw.Stop();
                textBox1.Text = sw.ElapsedMilliseconds.ToString();
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (img1 != null)
            {
                img2 = new Mat(img1.Rows, img1.Cols, Emgu.CV.CvEnum.DepthType.Cv8U, 1);
                textBox2.Text = null;
                sw.Reset();
                sw.Start();

                CvInvoke.CvtColor(img1, img2, Emgu.CV.CvEnum.ColorConversion.Bgr2Gray);
                imageBox2.Width = img2.Width / 2;
                imageBox2.Height = img2.Height / 2;
                imageBox2.Image = img2;

                sw.Stop();
                textBox2.Text = sw.ElapsedMilliseconds.ToString();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox3.Text = null;
            OpenFileDialog lvse = new OpenFileDialog();
            lvse.Title = "選擇圖片";
            lvse.InitialDirectory = "";
            lvse.Filter = "圖片文件|*.bmp;*.jpg;*.jpeg;*.gif;*.png";
            lvse.FilterIndex = 1;
            if (lvse.ShowDialog() == DialogResult.OK)
            {
                sw.Reset();
                sw.Start();

                img3 = CvInvoke.Imread(lvse.FileName, Emgu.CV.CvEnum.LoadImageType.Grayscale);
                imageBox3.Width = img3.Width / 2;
                imageBox3.Height = img3.Height / 2;
                imageBox3.Image = img3;

                sw.Stop();
                textBox3.Text = sw.ElapsedMilliseconds.ToString();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            img1 = null;
            img2 = null;
            img3 = null;
            imageBox1.Image = null;
            imageBox2.Image = null;
            imageBox3.Image = null;
            textBox1.Text = null;
            textBox2.Text = null;
            textBox3.Text = null;
        }
    }
}

注意:項目的屬性和配置管理器設置如下:

配置錯誤了則會報以下錯誤:

 

后來我又用一台安裝有Windows 10 64位操作系統的計算機試了一下,配置為Any CPU或者x86、x64都能正常運行,並不報錯。

 

參考資料:

1、關於EMGU CV的那些事——1.環境搭建(win8 vs2012 emgucv3.0)  (這個例子中用的EmguCV版本不是最新版本,“引用”中添加的dll不一樣)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM