简介

自英特尔公司发行OpenCV,由于它采用了BSD许可授权,因此无论是在商业和个人或者学术研究等领域都得到了广泛的应用,OpenCV全称是开源计算机视觉库(Open Source Computer Vision Library),但在.NET平台上经过重新封装发布并主要用于C# 、VB等语言的开源库,又称为Emgu CV,其在本质来讲都是同一个东西,因此下文出现的OpenCV 与Emgu CV 均指同一个类库。

Emgu CV的图像处理包含:

  1. 图像变换
  2. 图像增强和复原
  3. 图像编码压缩
  4. 图像分割
  5. 图像分析和理解

file

下载与安装

使用emgucv 进行开发是很简单事情,首先我们需要安装Emgucv ,可以从sourceforge找到最新版的下载地址: https://sourceforge.net/projects/emgucv/

我用过几个版本,其中3.0改版有些旧的方法已经不再提供,所以不方便换成2.4.10,但是发现存在一个bug,不能使用haar训练,于是再降级到2.4.0,暂时没发现问题

为了更方便后续使用,建议添加到系统路径

如下:右键我的电脑,属性

file

把EmguCV加入到PATH file

demo time

配置完成后写个简单的demo检查是否有问题


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.Util;

namespace CameraCapture
{
   public partial class CameraCapture : Form
   {
      private Capture _capture = null;
      private bool _captureInProgress;

      public CameraCapture()
      {
         InitializeComponent();
         try
         {
            _capture = new Capture(0); //第0个摄像头,可以是外接USB摄像头
            _capture.ImageGrabbed += ProcessFrame; //获取到一帧图像后触发
         }
         catch (NullReferenceException excpt)
         {
            MessageBox.Show(excpt.Message);
         }
      }

      private void ProcessFrame(object sender, EventArgs arg)
      {
         Image<Bgr, Byte> frame = _capture.RetrieveBgrFrame();

         Image<Gray, Byte> grayFrame = frame.Convert<Gray, Byte>();//灰度图
         Image<Gray, Byte> smallGrayFrame = grayFrame.PyrDown();
         Image<Gray, Byte> smoothedGrayFrame = smallGrayFrame.PyrUp();//平滑滤波
         Image<Gray, Byte> cannyFrame = smoothedGrayFrame.Canny(new Gray(100), new Gray(60));//边缘检测

         captureImageBox.Image = frame;
         grayscaleImageBox.Image = grayFrame;
         smoothedGrayscaleImageBox.Image = smoothedGrayFrame;
         cannyImageBox.Image = cannyFrame;
      }

      private void captureButtonClick(object sender, EventArgs e)
      {
         if (_capture != null)
         {
            if (_captureInProgress)
            {  //stop the capture
               captureButton.Text = "Start Capture";
               _capture.Pause();
            }
            else
            {
               //start the capture
               captureButton.Text = "Stop";
               _capture.Start();
            }

            _captureInProgress = !_captureInProgress;
         }
      }

      private void ReleaseData()
      {
         if (_capture != null)
            _capture.Dispose();
      }

      private void FlipHorizontalButtonClick(object sender, EventArgs e)
      {
         if (_capture != null) _capture.FlipHorizontal = !_capture.FlipHorizontal;
      }

      private void FlipVerticalButtonClick(object sender, EventArgs e)
      {
         if (_capture != null) _capture.FlipVertical = !_capture.FlipVertical;
      }
   }
}

能正常运行说明配置没问题

file it works!

注:如果无法运行可能需要将安装目录下bin下的dll复制到本工程的debug目录