使用 Emgu CV 进行人脸识别

Face Recognition

1- Create a Windows Form Application
2- Add a PictureBox and a Timer (and Enable it)
3- Run it on a x86 system
4- Be sure you have the OpenCV relevant dlls (included with the Emgu CV download) in the folder where you code executes.
5- Adjust the path to find the Haarcascade xml (last line of the code)

 

using System;
using System.Windows.Forms;
using System.Drawing;
using Emgu.CV;
using Emgu.Util;
using Emgu.CV.Structure;
using Emgu.CV.CvEnum;

namespace opencvtut
{
public partial class Form1 : Form
{
private Capture cap;
private HaarCascade haar;

public Form1()
{
InitializeComponent();
}

private void timer1_Tick(object sender, EventArgs e)
{
using (Image<Bgr, byte> nextFrame = cap.QueryFrame())
{
if (nextFrame != null)
{
// there’s only one channel (greyscale), hence the zero index
//var faces = nextFrame.DetectHaarCascade(haar)[0];
Image<Gray, byte> grayframe = nextFrame.Convert<Gray, byte>();
var faces =
grayframe.DetectHaarCascade(
haar, 1.4, 4,
HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
new Size(nextFrame.Width/8, nextFrame.Height/8)
)[0];

foreach (var face in faces)
{
nextFrame.Draw(face.rect, new Bgr(0,double.MaxValue,0), 3);
}
pictureBox1.Image = nextFrame.ToBitmap();
}
}
}

private void Form1_Load(object sender, EventArgs e)
{
// passing 0 gets zeroth webcam
cap = new Capture(0);
// adjust path to find your xml
haar = new HaarCascade(
“..\\..\\..\\..\\lib\\haarcascade_frontalface_alt2.xml”);
}
}
}

命名空间简介:

EmguCV 是一个在 .NET 平台下对 OpenCV 图像处理库进行封装的包,该包通过 C#编写。通过使用 EmguCV,可以实现 .NET 兼容语言(如 C#、VC++、VB、IronPytho等)对 OpenCV 函数的调用。以下是在 C# 中通过 EmguCV 使用 OpenCV 的环境配置方法:

  1. 下载 OpenCV 和 EmguCV,然后进行安装。注意, OpenCV 和 EmguCV 的版本必须一致。可以通过以下方法查看需安装的 OpenCV 版本:先安装 EmguCV,然后查看其安装目录的 bin 目录下的 opencv 动态链接库文件的版本,如 opencv_highgui220.dll 表示是 OpenCV 的版本必须是 2.2。
  2. 在安装 OpenCV 时,如果提示是否须将其 bin 目录加入到系统路径(path)中,选择是,如果没有这个提示,则可通过下面的方法将其 bin 目录加入到系统路径中。
    • 右击桌面上“我的计算机”,选择“属性”,然后在弹出的“属性”对话框中选择“高级”,再点击“环境变量”,在“系统变量”中双击“Path”;
    • 在变量值后加入 “C:\OpenCV2.2\bin”(注意,不同版本的 OpenCV 其 bin 目录也不相同)。
  3. 打开 Visual Studio,新建一个 C# 应用程序。
  4. 通 过菜单 "Project -->Add reference",或在 "Solution Explorer" 中右击工程,选择 “Add reference”,然后在弹出的 “Add reference” 对话框中点击 “Browse”,将 EmguCV 安装目录的 bin 目录下的Emgu.CV.dll、Emgu.Util.dll 和 ZedGraph.dll 都添加到引用里面。
  5. 在程序的开头加上
    using Emgu.CV;
    using Emgu.Util;
    之后就可以使用 EmguCV 中所有的库函数了。

OpenCV简介 

如 何在一副图片中检测到人脸,这涉及到计算机图形学中一些非常复杂的计算,如果这些计算都靠程序员自己来编程,那么工作量就相当大。OpenCV全称是 Open Computer Vision,是指开放的计算机视觉资源代码,它具有:统一的结构和功能定义、强大的图像和矩阵运算能力、方便灵活的接口等特点,是计算机视觉、图像处理 和模式识别等方面进行二次开发的理想工具。它可以在各种版本的Windows下运行,也可以在Linux下运行。OpenCV的源代码是用C和C++所编 写且完全开放的,因此具有很好的可移植性,在Microsoft Visual C++ 6.0、Microsoft Visual Studio 2003及Borland C++ BuilderX等环境下均可方便地使用OpenCV所提供的库来进行实际开发。本程序以Visual C++ 2005作为开发环境来介绍。

一、OpenCV在Visual C++ 2005下的安装与配置

1.OpenCV安装

首先到OpenCV的官方网站(http://sourceforge.net/projects/opencvlibrary)下载OpenCV并进行安装,安装过程很简单,只要按照安装向导一步一步进行即可。

2.对Visual C++ 2005进行全局设置

1)打开Visual C++ 2005,选择“Tools(工具) | Options(选项)”菜单项,弹出如图1所示的对话框。

2)在左侧列表框中选择“Projects and Solutions(项目和解决问题方案) | VC++ Directories(VC++目录)”。

3)在“Show directories for(显示以下内容的目录)”下拉列表框中选择“Library Files(库文件)”。

4)在右侧库文件列表框中定位并添加“< OpenCV 安装目录>OpenCVlib” 。


图1

5)在“Show directories for(显示以下内容的目录)”下拉列表框中选择“Include Files(包含文件)”,并在右侧列表框中定位并添加以下路径:

< OpenCV 安装目录>OpenCVcxcoreinclude

< OpenCV 安装目录>OpenCVcvinclude

< OpenCV 安装目录>OpenCVcvauxinclude

< OpenCV 安装目录>OpenCVmlinclude

< OpenCV 安装目录>OpenCVotherlibshighgui

< OpenCV 安装目录>OpenCVotherlibscvcaminclude

< OpenCV 安装目录>OpenCVMicrosoft Platform SDKInclude

⑥单击OK(确定)按钮保存配置。

原文地址:https://www.cnblogs.com/hxwzwiy/p/2419117.html