c# OpenCv 2-使用haarcascade模型進行人臉識別
阿新 • • 發佈:2018-12-02
Test:
1. 讀取cascade模型檔案
2. 對輸入的圖片進行灰度處理
3. 使用cascade以及模型檔案(本例使用的是人臉正面)對圖片進行預測
TestCrop:
步驟如Test,截取出人臉照片
haarcascades的模型可以從opencv獲得:
https://github.com/opencv/opencv/tree/master/data
... public class FRTest { private CascadeClassifier _cascadeClassifier; private Capture _capture; public void Test(Bitmap bmp) { //_capture = new Capture(); var imageFrame = new Image<Bgr, byte>(bmp); //_capture.QueryFrame(); _cascadeClassifier = new CascadeClassifier(Application.StartupPath + "/haarcascades/haarcascade_frontalface_default.xml"); //using (var imageFrame = _capture.QueryFrame().ToImage<Bgr, Byte>()) //{ if (imageFrame != null) { var grayframe = imageFrame.Convert<Gray, byte>(); var faces = _cascadeClassifier.DetectMultiScale(grayframe, 1.1, 10, Size.Empty); //the actual face detection happens here foreach (var face in faces) { imageFrame.Draw(face, new Bgr(Color.BurlyWood), 3); //the detected face(s) is highlighted here using a box that is drawn around it/them } } ImageViewer.Show(imageFrame); // imgCamUser.Image = imageFrame; } public void TestCrop(Bitmap bmp) { //_capture = new Capture(); var imageFrame = new Image<Bgr, byte>(bmp); //_capture.QueryFrame(); _cascadeClassifier = new CascadeClassifier(Application.StartupPath + "/haarcascades/haarcascade_frontalface_default.xml"); //using (var imageFrame = _capture.QueryFrame().ToImage<Bgr, Byte>()) //{ var result = new List<Image<Bgr, byte>>(); var grayframe = imageFrame.Convert<Gray, byte>(); var faces = _cascadeClassifier.DetectMultiScale(grayframe, 1.1, 10, Size.Empty); //the actual face detection happens here foreach (var face in faces) { var copyImg = imageFrame.Copy(); copyImg.ROI = face; result.Add(copyImg.Copy()); //imageFrame.Draw(face, new Bgr(Color.BurlyWood), 3); //the detected face(s) is highlighted here using a box that is drawn around it/them } ImageViewer.Show(imageFrame); foreach (var image in result) { ImageViewer.Show(image); } // imgCamUser.Image = imageFrame; } } ...