通过OpenCV中的级联分类器
#include <opencv2/opencv.hpp> #include <opencv2/core/ocl.hpp> using namespace cv; using namespace std; void main() { VideoCapture cap(0); if (!cap.isOpened()) { cout << "错误" << endl; return; } Mat frame; CascadeClassifier facecascade, cascade_eye, cascade_mouth; facecascade.load("haarcascades/haarcascade_frontalface_alt.xml");while (cap.isOpened()) { cap >> frame; std::vector<Rect> rect; facecascade.detectMultiScale(frame, rect, 1.45, 2, 0); cv::waitKey(1); if (rect.size() > 0) { for (int i = 0; i < rect.size(); i++) { int x = rect[i].x; int y = rect[i].y; int w = rect[i].width; int h = rect[i].height; Point point1, point2; point1.x = x; point1.y = y; point2.x = x + w; point2.y = y + h; cv::rectangle(frame, point1, point2, (255, 0, 0), 2, 0); } } cv::imshow("result", frame); } }