本文主要是介绍C# OpenCv Haar、LBP 人脸检测,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录
效果
代码
下载
效果
代码
using OpenCvSharp;namespace OPenCVDemo
{class Program{static void Main(string[] args){// Load the cascadesvar haarCascade = new CascadeClassifier("haarcascade_frontalface_default.xml");var lbpCascade = new CascadeClassifier("lbpcascade_frontalface.xml");// Detect facesMat haarResult = DetectFace(haarCascade);Mat lbpResult = DetectFace(lbpCascade);Cv2.ImShow("Faces by Haar", haarResult);Cv2.ImShow("Faces by LBP", lbpResult);Cv2.WaitKey(0);Cv2.DestroyAllWindows();}static private Mat DetectFace(CascadeClassifier cascade){Mat result;using (var src = new Mat("3.jpg", ImreadModes.Color))using (var gray = new Mat()){result = src.Clone();Cv2.CvtColor(src, gray, ColorConversionCodes.BGR2GRAY);// Detect facesRect[] faces = cascade.DetectMultiScale(gray, 1.08, 2, HaarDetectionType.ScaleImage, new Size(30, 30));// Render all detected facesforeach (Rect face in faces){var center = new Point{X = (int)(face.X + face.Width * 0.5),Y = (int)(face.Y + face.Height * 0.5)};var axes = new Size{Width = (int)(face.Width * 0.5),Height = (int)(face.Height * 0.5)};Cv2.Ellipse(result, center, axes, 0, 0, 360, new Scalar(255, 0, 255), 4);}}return result;}}
}
下载
附件下载
这篇关于C# OpenCv Haar、LBP 人脸检测的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!