您的位置:首页 > 编程语言 > C#

Emgu CV2.3里 C#人脸检测例程 显示在Imagebox

2011-12-30 14:40 330 查看




创建一个窗体程序,在窗体里加入Imagebox控件和两个button控件。

注意:此程序用到两个文件haarcascade_frontalface_default.xml和haarcascade_eye.xml。这两个文件在Egmu CV2.3里有,查找一下可以找到。

在窗体里输入以下程序(此程序在Emgu CV2.3里有):

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using Emgu.CV;

using Emgu.Util;

using Emgu.CV.Structure;

using System.Diagnostics;

using System.Runtime.InteropServices;

using Emgu.CV.UI;

using Emgu.CV.GPU;

namespace Chuanlin2012_egmu2.FaceDetection

{

public partial class MainFrm : Form

{

public MainFrm()

{

InitializeComponent();

}

private Image<Bgr, byte> image;

private void button1_Click(object sender, EventArgs e) //打开图像

{

OpenFileDialog opdlg = new OpenFileDialog();

opdlg.Filter = "所有文件|*.*";

if (opdlg.ShowDialog() == DialogResult.OK)

{

Image<Bgr, byte> imgrgb = new Image<Bgr, byte>(opdlg.FileName);

imageBox1.Image = imgrgb;

image = imgrgb;

}

}

private void button2_Click(object sender, EventArgs e) //开始识别

{

if (!IsPlaformCompatable()) return;

Run();

}

void Run()

{

//Image<Bgr, Byte> image = new Image<Bgr, byte>("lena.jpg"); //Read the files as an 8-bit Bgr image

Stopwatch watch;

String faceFileName = "D:\\My Documents\\Visual Studio 2008\\Projects\\Project1\\Chuanlin2012-egmu2.3\\Chuanlin2012-egmu2.3\\bin\\Debug\\haarcascade_frontalface_default.xml"; //引用自己的文件地址

String eyeFileName = "D:\\My Documents\\Visual Studio 2008\\Projects\\Project1\\Chuanlin2012-egmu2.3\\Chuanlin2012-egmu2.3\\bin\\Debug\\haarcascade_eye.xml"; //引用自己的文件地址

if (GpuInvoke.HasCuda)

{

using (GpuCascadeClassifier face = new GpuCascadeClassifier(faceFileName))

using (GpuCascadeClassifier eye = new GpuCascadeClassifier(eyeFileName))

{

watch = Stopwatch.StartNew();

using (GpuImage<Bgr, Byte> gpuImage = new GpuImage<Bgr, byte>(image))

using (GpuImage<Gray, Byte> gpuGray = gpuImage.Convert<Gray, Byte>())

{

Rectangle[] faceRegion = face.DetectMultiScale(gpuGray, 1.1, 10, Size.Empty);

foreach (Rectangle f in faceRegion)

{

//draw the face detected in the 0th (gray) channel with blue color

image.Draw(f, new Bgr(Color.Blue), 2);

using (GpuImage<Gray, Byte> faceImg = gpuGray.GetSubRect(f))

{

//For some reason a clone is required.

//Might be a bug of GpuCascadeClassifier in opencv

using (GpuImage<Gray, Byte> clone = faceImg.Clone())

{

Rectangle[] eyeRegion = eye.DetectMultiScale(clone, 1.1, 10, Size.Empty);

foreach (Rectangle e in eyeRegion)

{

Rectangle eyeRect = e;

eyeRect.Offset(f.X, f.Y);

image.Draw(eyeRect, new Bgr(Color.Red), 2);

}

}

}

}

}

watch.Stop();

}

}

else

{

//Read the HaarCascade objects

using (HaarCascade face = new HaarCascade(faceFileName))

using (HaarCascade eye = new HaarCascade(eyeFileName))

{

watch = Stopwatch.StartNew();

using (Image<Gray, Byte> gray = image.Convert<Gray, Byte>()) //Convert it to Grayscale

{

//normalizes brightness and increases contrast of the image

gray._EqualizeHist();

//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 = face.Detect(

gray,

1.1,

10,

Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,

new Size(20, 20));

foreach (MCvAvgComp f in facesDetected)

{

//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 = eye.Detect(

gray,

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)

{

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(

"Completed face and eye detection using {0} in {1} milliseconds",

GpuInvoke.HasCuda ? "GPU" : "CPU",

watch.ElapsedMilliseconds));

*/

imageBox1.Image = image;

}

/// <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;

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: