【问题标题】:Eyes' positions in an image of a human face?眼睛在人脸图像中的位置?
【发布时间】:2019-02-26 12:41:22
【问题描述】:

如何获取人脸图像中眼睛的位置? 例如,我的程序搜索眼睛,然后它们的位置可以存储在二维向量中,例如:

Vector2 leftEye = new Vector2(56, 50);

我听说过 Emgu,但我真的不明白它是如何与 XML 一起工作的......

【问题讨论】:

  • 这个问题的定义太宽泛了,真的不适合stackoverflow格式。请考虑阅读有关提出好问题以及可以提出哪些问题的帮助

标签: c# image-recognition emgucv


【解决方案1】:

这是一个使用 Emgu 3.4.1 的示例。训练数据 xml 在 GitHub 上可用,您可以将其加载到 CascadeClassifier 类中,然后该类可以执行检测。

using Emgu.CV;
using Emgu.CV.Structure;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Net;

public class Program
{
    private const string EYE_DETECTION_XML = "https://raw.githubusercontent.com/opencv/opencv/master/data/haarcascades/haarcascade_eye.xml";
    private const string SAMPLE_IMAGE = "https://upload.wikimedia.org/wikipedia/commons/thumb/1/18/Lewis_Hamilton_2016_Malaysia_2.jpg/330px-Lewis_Hamilton_2016_Malaysia_2.jpg";

    static void Main()
    {
        // download sample photo
        WebClient client = new WebClient();
        Bitmap image = null;
        using (MemoryStream ms = new MemoryStream(client.DownloadData(SAMPLE_IMAGE)))
            image = new Bitmap(Image.FromStream(ms));

        // convert to Emgu image, convert to grayscale and increase brightness/contrast
        Emgu.CV.Image<Bgr, byte> emguImage = new Emgu.CV.Image<Bgr, byte>(image);
        var grayScaleImage = emguImage.Convert<Gray, byte>();
        grayScaleImage._EqualizeHist();

        // load eye classifier data
        string eye_classifier_local_xml = @"c:\temp\haarcascade_eye.xml";
        client.DownloadFile(@EYE_DETECTION_XML, eye_classifier_local_xml);        
        CascadeClassifier eyeClassifier = new CascadeClassifier(eye_classifier_local_xml);

        // perform detection which will return rectangles of eye positions
        var eyes = eyeClassifier.DetectMultiScale(grayScaleImage, 1.1, 4);

        // draw those rectangles on original image
        foreach (Rectangle eye in eyes)
            emguImage.Draw(eye, new Bgr(255, 0, 0), 3);

        // save image and show it
        string output_image_location = @"c:\temp\output.png";        
        emguImage.ToBitmap().Save(output_image_location, ImageFormat.Png);
        Process.Start(output_image_location);    
    }
}

【讨论】:

  • 非常感谢你是个好人 :)
猜你喜欢
  • 2016-10-19
  • 1970-01-01
  • 2012-10-19
  • 2013-12-26
  • 2014-06-27
  • 2011-10-29
  • 2015-04-23
  • 2015-09-06
  • 2012-09-21
相关资源
最近更新 更多