Giới thiệu
Đếm số khuôn mặt rất đơn giản: sau detectMultiScale, vector faces chứa các Rect khuôn mặt. faces.size() chính là số lượng khuôn mặt.
Chương trình đếm khuôn mặt
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <string>
using namespace std;
using namespace cv;
int main(int argc, char** argv) {
Mat image_with_humanface = imread("friends.jpg");
string trained_classifier_location = "haarcascade_frontalface_alt.xml";
CascadeClassifier faceDetector;
faceDetector.load(trained_classifier_location);
vector<Rect> faces;
faceDetector.detectMultiScale(image_with_humanface, faces, 1.1, 4, CASCADE_SCALE_IMAGE, Size(20, 20));
int x = faces.size();
cout << "Số khuôn mặt trong ảnh = " << x << endl;
system("pause");
return 0;
}
Kết quả
Console in ra: Số khuôn mặt trong ảnh = 3 (tùy ảnh đầu vào).