这个提供的代码例子是Emgu CV提供的源码里面自带的例子,很好用,基本不需要改,代码做的是人脸检测不是人脸识别,这个要分清楚。再就是新版本的Emgu CV可能会遇到系统32位和64位处理方式有区别的问题,解决的办法不止一种,我这里的建议在条件允许的情况下尽量使用Emgu CV的早期版本,因为越新的版本的兼容性越差,早期的版本是不分32位和64位的,而且新版本的Emgu CV可能不再支持一些老的硬件,这也是选择老版本的原因,总之,是具体情况而定吧。这里只是给大家看看代码,要想运行起来,完整的解决方案,请大家去Emgu CV的官网下载相应的源码。

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.CV.UI; namespace FaceDetection
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
if (!IsPlaformCompatable()) return;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Run();
} static void Run()
{
Image<Bgr, Byte> image = new Image<Bgr, byte>("lena.jpg"); //Read the files as an 8-bit Bgr image
Image<Gray, Byte> gray = image.Convert<Gray, Byte>(); //Convert it to Grayscale Stopwatch watch = Stopwatch.StartNew();
//normalizes brightness and increases contrast of the image
gray._EqualizeHist(); //Read the HaarCascade objects
HaarCascade face = new HaarCascade("haarcascade_frontalface_alt_tree.xml");
HaarCascade eye = new HaarCascade("haarcascade_eye.xml"); //Detect the faces from the gray scale image and store the locations as rectangle
//The first dimensional is the channel
//The second dimension is the index of the rectangle in the specific channel
MCvAvgComp[][] facesDetected = gray.DetectHaarCascade(
face,
1.1,
10,
Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
new Size(20, 20)); foreach (MCvAvgComp f in facesDetected[0])
{
//draw the face detected in the 0th (gray) channel with blue color
image.Draw(f.rect, new Bgr(Color.Blue), 2); //Set the region of interest on the faces
gray.ROI = f.rect;
MCvAvgComp[][] eyesDetected = gray.DetectHaarCascade(
eye,
1.1,
10,
Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
new Size(20, 20));
gray.ROI = Rectangle.Empty; foreach (MCvAvgComp e in eyesDetected[0])
{
Rectangle eyeRect = e.rect;
eyeRect.Offset(f.rect.X, f.rect.Y);
image.Draw(eyeRect, new Bgr(Color.Red), 2);
}
} watch.Stop();
//display the image
ImageViewer.Show(image, String.Format("Perform face and eye detection in {0} milliseconds", watch.ElapsedMilliseconds));
} /// <summary>
/// Check if both the managed and unmanaged code are compiled for the same architecture
/// </summary>
/// <returns>Returns true if both the managed and unmanaged code are compiled for the same architecture</returns>
static bool IsPlaformCompatable()
{
int clrBitness = Marshal.SizeOf(typeof(IntPtr)) * 8;
if (clrBitness != CvInvoke.UnmanagedCodeBitness)
{
MessageBox.Show(String.Format("Platform mismatched: CLR is {0} bit, C++ code is {1} bit."
+ " Please consider recompiling the executable with the same platform target as C++ code.",
clrBitness, CvInvoke.UnmanagedCodeBitness));
return false;
}
return true;
}
}
}

基于Emgu CV的人脸检测代码的更多相关文章

  1. 基于Emgu CV+百度人脸识别,实现视频动态 人脸抓取与识别

    背景 目前AI 处于风口浪尖,作为 公司的CTO,也作为自己的技术专研,开始了AI之旅,在朋友圈中也咨询 一些大牛对于AI 机器学习框架的看法,目前自己的研究方向主要开源的 AI 库,如:Emgu C ...

  2. 基于Haar特征Adaboost人脸检测级联分类

    基于Haar特征Adaboost人脸检测级联分类 基于Haar特征Adaboost人脸检测级联分类,称haar分类器. 通过这个算法的名字,我们能够看到这个算法事实上包括了几个关键点:Haar特征.A ...

  3. C# net Emgu.CV.World 人脸识别 根据照片将人脸抠图出来。

    Emgu.CV.World 人脸识别 根据照片将人脸抠图出来.效果如下: 应用范围:配合摄像头,抓取的图像,抠出人脸照片,这样人脸照片的大小会很小,传输速度快.这样识别速度也就快. 目前我正在做百度人 ...

  4. 可学习的多人人脸识别程序(基于Emgu CV)

    源代码下载(需要安装Emgu CV,安装方法请百度) 很多朋友使用Emgu CV遇到CvInvoke()的报错,我找到一种解决方法. 把EmguCV目录下bin里面的所有dll复制到C:\WINDOW ...

  5. [Winform]基于Emgu.CV人脸识别

    摘要 “OpenCV是一个开源的计算机视觉库.OpenCV采用C/C++语言编写,可以运行在Linux/Windows/Mac等操作系统上.OpenCV还提供了Python.Ruby.MATLAB以及 ...

  6. 基于Emgu CV 的手势识别实现PPT的控制放映

    Emgu CV 简介         众所周知,Emgu CV是.NET平台下对OpenCV图像处理库的封装,也就是.NET版的OpenCV.开发者可以很方便的通过C#,VB等语言调用OpenCV函数 ...

  7. [转载+原创]Emgu CV on C# (七) —— Emgu CV on 轮廓检测

    轮廓检测 对于查找轮廓我们一般要对图像Canny检测.但是对于很特殊的场合其实我们还可以直接对二值化的图像进行轮廓的提取. 关键函数 1. cvFindContours Retrieves conto ...

  8. Android—基于OpenCV+Android实现人脸检测

    导读 OpenCV 是一个开源的跨平台计算机视觉库, 采C++语言编写,实现了图像处理和计算机视觉方面的很多通用算法,同时也提供对Python,Java,Android等的支持,这里利用Android ...

  9. 基于Emgu cv的图像拼接(转)

    分类: 编程 C# Emgu cv Stitching 2012-10-27 11:04 753人阅读 评论(1) 收藏 举报 在新版本的Emgu cv中添加了Emgu.CV.Stitching,这极 ...

随机推荐

  1. Linux0.11内核--引导程序分析

    1.简介 本文主要介绍三个文件bootsect.s.setup.s.head.s,主要是做了些从软盘加载内核和设置32位保护模式的操作. 2.程序分析 当PC电源打开后,BIOS自检后将bootsec ...

  2. css解决方案

    1,Flexbox(更优雅的布局) ①居中:{display:flex; justify-content:center; align-items:center;}②设定flex-grow属性的话,会自 ...

  3. vagrant vbox上配置好开发环境缓存问题

    vagrant配置完成 设置好共享目录 搭建好nginx环境 访问 127.0.0.1:8080 一切正常  然后进入本的的开发目录修改测试文件保存后刷新页面 问题来了..........没变化  然 ...

  4. Oracle解锁,解决“ora00054:资源正忙”错误

    Oracle解锁,解决“ora00054:资源正忙”错误 一.处理步骤:--1.获取被锁对象的session_idSELECT session_id FROM v$locked_object; --2 ...

  5. 将String转化成Stream,将Stream转换成String

    using System;using System.IO;using System.Text;namespace CSharpConvertString2Stream{     class Progr ...

  6. Expdp 导数错误 ORA-00832

    问题实验环境 操作系统:Red Hat Enterprise Linux Server release 5.7 (Tikanga) 数据库  :Oracle Database 10g Release ...

  7. composer "Illegal offset type in isset or empty"报错解决方案

    最近更新了composer版本,即执行以下任一命令 composer selfupdate | composer self-update 再次执行 composer update -vvv 会出现“I ...

  8. memcache+magent的高可用

    memcache+magent的高可用 一.安装步骤: 1.编译安装libevent: wget http://monkey.org/~provos/libevent-1.4.9-stable.tar ...

  9. Java 对象初始化

      对象A的创建过程: 1. 构造器实际上是静态方法.当首次创建对象A 或者 A类的静态方法/静态域首次被访问时,Java解释器查找类路径,以定位     A.class文件.(当程序创建第一个对类的 ...

  10. linux(64位的系统)下nasm进行汇编链接时出现的问题

    出现问题: $nasm -f elf hello.asm -o hello.o $ld -s hello.o -o hello ld: i386 architecture of input file ...