[英]C# - Detect face and crop image
我正在 C# 中編寫一個HttpHandler
,它提供調整大小的圖像和 blah blah blah ... 沒問題,我們有數百萬個處理程序可供參考。
問題是我的用戶使用“傳統”尺寸拍攝的照片,如 4:3 和 16:9。 但是這個處理程序需要提供照片 ID 大小(4 厘米 x 3 厘米)的圖片,並且顯然需要在用戶面部周圍進行裁剪。 面部位置變化很大(並不總是在圖片中心)。
那么,我可以使用什么樣的算法來檢測面部中心,然后圍繞該點裁剪圖像?
您可以在 EmguCV(OpenCV 的 DotNet 端口)中使用 HaarCascade class http://www.emgu.com/wiki/index.php/Face_detection
運行此示例的注意事項:
- 創建 Windows 表格申請
- 添加一個 PictureBox 和一個 Timer(並啟用它)- 在 x86 系統上運行它
- 確保在執行代碼的文件夾中有 OpenCV 相關的 dll(包含在 Emgu CV 下載中)。
- 調整路徑找到Haarcascade xml(最后一行代碼)
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");
}
}
}
如果您正在尋找裁剪圖像,您可以使用名為 Face API 的 Microsoft Cognitive Service 來分隔照片上所有人的臉,它會返回一個 JSON,它具有返回給您 Rectangle 結構的元素,然后您可以根據需要裁剪和調整圖像大小。
在這里你可以看到更多關於它的信息: FaceAPI
我正在用 C# 編寫一個HttpHandler
,它提供調整大小的圖像和等等等等......沒問題,我們有數百萬個處理程序用作參考。
問題是我的用戶的照片是用“傳統”尺寸拍攝的,如 4:3 和 16:9。 但是此處理程序需要以照片 ID 大小(4 厘米 x 3 厘米)提供圖片,並且顯然需要在用戶面部周圍進行裁剪。 面部位置變化很大(並不總是在圖片中心)。
那么,我可以使用什么樣的算法來檢測人臉中心,然后圍繞這一點裁剪圖像?
您可以在http://deteksiwajah.blogspot.com/查看面部檢測和裁剪軟件示例。 它是開源的,使用 OpenCV 庫。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.