基于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;
}
}
}
上一篇:Python爬虫从入门到放弃(十三)之 Scrapy框架的命令行详解


下一篇:post multipart data boundary问题 使用curl 向jersey post文件