gpt4 book ai didi

C# - 检测人脸和裁剪图像

转载 作者:太空狗 更新时间:2023-10-29 22:24:02 27 4
gpt4 key购买 nike

我正在用 C# 编写一个 HttpHandler,它提供调整大小的图像和 blah blah blah...没问题,我们有数百万个处理程序可供引用。

问题是我的用户使用“传统”尺寸拍摄的照片,例如 4:3 和 16:9。但是此处理程序需要提供照片 ID 大小(4 厘米 x 3 厘米)的图片,并且显然需要在用户面部周围裁剪。面部位置变化很大(并不总是在图片中心)。

那么,我可以使用什么样的算法来检测面部中心,然后围绕该点裁剪图像?

最佳答案

您可以在 EmguCV(OpenCV 的 DotNet 端口)中使用 HaarCascade 类 http://www.emgu.com/wiki/index.php/Face_detection

Notes in order to run this example:

  • Create a Windows Form Application
  • Add a PictureBox and a Timer (and Enable it) - Run it on a x86 system
  • Be sure you have the OpenCV relevant dlls (included with the Emgu CV download) in the folder where you code executes.
  • Adjust the path to find the Haarcascade xml (last line of the code)
using System;
using System.Windows.Forms;
using System.Drawing;
using Emgu.CV;
using Emgu.Util;
using Emgu.CV.Structure;
using Emgu.CV.CvEnum;

namespace opencvtut
{
public partial class Form1 : Form
{
private Capture cap;
private HaarCascade haar;

public Form1()
{
InitializeComponent();
}

private void timer1_Tick(object sender, EventArgs e)
{
using (Image<Bgr, byte> nextFrame = cap.QueryFrame())
{
if (nextFrame != null)
{
// there's only one channel (greyscale), hence the zero index
//var faces = nextFrame.DetectHaarCascade(haar)[0];
Image<Gray, byte> grayframe = nextFrame.Convert<Gray, byte>();
var faces =
grayframe.DetectHaarCascade(
haar, 1.4, 4,
HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
new Size(nextFrame.Width/8, nextFrame.Height/8)
)[0];

foreach (var face in faces)
{
nextFrame.Draw(face.rect, new Bgr(0,double.MaxValue,0), 3);
}
pictureBox1.Image = nextFrame.ToBitmap();
}
}
}

private void Form1_Load(object sender, EventArgs e)
{
// passing 0 gets zeroth webcam
cap = new Capture(0);
// adjust path to find your xml
haar = new HaarCascade(
"..\\..\\..\\..\\lib\\haarcascade_frontalface_alt2.xml");
}
}
}

关于C# - 检测人脸和裁剪图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7368709/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com