基于Emgu CV的人脸检测代码
这个提供的代码例子是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的人脸检测代码的更多相关文章
- 基于Emgu CV+百度人脸识别,实现视频动态 人脸抓取与识别
背景 目前AI 处于风口浪尖,作为 公司的CTO,也作为自己的技术专研,开始了AI之旅,在朋友圈中也咨询 一些大牛对于AI 机器学习框架的看法,目前自己的研究方向主要开源的 AI 库,如:Emgu C ...
- 基于Haar特征Adaboost人脸检测级联分类
基于Haar特征Adaboost人脸检测级联分类 基于Haar特征Adaboost人脸检测级联分类,称haar分类器. 通过这个算法的名字,我们能够看到这个算法事实上包括了几个关键点:Haar特征.A ...
- C# net Emgu.CV.World 人脸识别 根据照片将人脸抠图出来。
Emgu.CV.World 人脸识别 根据照片将人脸抠图出来.效果如下: 应用范围:配合摄像头,抓取的图像,抠出人脸照片,这样人脸照片的大小会很小,传输速度快.这样识别速度也就快. 目前我正在做百度人 ...
- 可学习的多人人脸识别程序(基于Emgu CV)
源代码下载(需要安装Emgu CV,安装方法请百度) 很多朋友使用Emgu CV遇到CvInvoke()的报错,我找到一种解决方法. 把EmguCV目录下bin里面的所有dll复制到C:\WINDOW ...
- [Winform]基于Emgu.CV人脸识别
摘要 “OpenCV是一个开源的计算机视觉库.OpenCV采用C/C++语言编写,可以运行在Linux/Windows/Mac等操作系统上.OpenCV还提供了Python.Ruby.MATLAB以及 ...
- 基于Emgu CV 的手势识别实现PPT的控制放映
Emgu CV 简介 众所周知,Emgu CV是.NET平台下对OpenCV图像处理库的封装,也就是.NET版的OpenCV.开发者可以很方便的通过C#,VB等语言调用OpenCV函数 ...
- [转载+原创]Emgu CV on C# (七) —— Emgu CV on 轮廓检测
轮廓检测 对于查找轮廓我们一般要对图像Canny检测.但是对于很特殊的场合其实我们还可以直接对二值化的图像进行轮廓的提取. 关键函数 1. cvFindContours Retrieves conto ...
- Android—基于OpenCV+Android实现人脸检测
导读 OpenCV 是一个开源的跨平台计算机视觉库, 采C++语言编写,实现了图像处理和计算机视觉方面的很多通用算法,同时也提供对Python,Java,Android等的支持,这里利用Android ...
- 基于Emgu cv的图像拼接(转)
分类: 编程 C# Emgu cv Stitching 2012-10-27 11:04 753人阅读 评论(1) 收藏 举报 在新版本的Emgu cv中添加了Emgu.CV.Stitching,这极 ...
随机推荐
- Solr 4.0 部署实例教程
Solr 4.0 部署实例教程 Solr 4.0的入门基础教程,先说一点部署之后肯定会有人用solrj,solr 4.0好像添加了不少东西,其中CommonsHttpSolrServer这个类改名为H ...
- Java中 实现多线程成的三种方式(继承,实现,匿名内部类)
---------------------------------------------------------------------------------------------------- ...
- docker'部署
环境:ubuntu-14.04.4-server-amd64 1.更换阿里云源 备份源配置文件: $ sudo cp /etc/apt/sources.list /etc/apt/sources.li ...
- css sprite 调整大张图片中小图标的大小
直接说解决方法: 假设一张拼合好的大图大小是:900 x 1000 px (如上图) 现在想取图中左上角的河马图标,并缩小图标的大小. 正常取图: .sprite { background: url( ...
- Ignite用户配置管理介绍
Ignite8这款数据库性能监控.性能分析软件,不光是DBA监控.管理数据库的利器之一,开发人员也可以查看系统性能数据,下面介绍一下如何在Ignite里面配置用户权限,本文纯水文一篇,介绍简单基本的配 ...
- Android入门笔记(一)
第一部分,Android开发环境的搭建 1.去http://www.oracle.com/technetwork/java/javase/downloads/index.html下载最新版本jdk并安 ...
- kali 2.0 U盘安装错误Your installation cd-rom couldn't be mounted
1.kali 2.0前天(2015.08.11)发布了.果断下载下来换掉本机的1.0版本. 2.用U盘安装的过程中,出现cd-rom无法挂载.提示错误Your installation CD-ROM ...
- Entity Framework 6 with MySql
MySQL Connector/Net 6.8.x MySQL Server 5.1 or above Entity Framework 6 assemblies .NET Framework ...
- #essay 161218# 自己的markdown笔记(日记)方法
写在前面 本文可能极度无聊--自己markdown笔记方法 我的工具 1. computer 2. samsung mobile phone(自己的小S3) 3. markdownpad 2 4. p ...
- 《InsideUE4》-10-GamePlay架构(九)GameInstance
一人之下,万人之上 引言 上篇我们讲到了UE在World之上,继续抽象出了Player的概念,包含了本地的ULocalPlayer和网络的UNetConnection,并以此创建出了World中的Pl ...