目录
效果
代码
下载
效果
代码
using OpenCvSharp;
namespace OPenCVDemo
{
class Program
{
static void Main(string[] args)
{
// Load the cascades
var haarCascade = new CascadeClassifier("haarcascade_frontalface_default.xml");
var lbpCascade = new CascadeClassifier("lbpcascade_frontalface.xml");
// Detect faces
Mat 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 faces
Rect[] faces = cascade.DetectMultiScale(
gray, 1.08, 2, HaarDetectionType.ScaleImage, new Size(30, 30));
// Render all detected faces
foreach (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;
}
}
}
下载
附件下载