利用opencv自带的训练好的模型进行人脸检测,没有什么技术含量主要可以了解一下各种操作
#include <iostream> #include <opencv2/opencv.hpp> using namespace std; using namespace cv; int main(int argc, char** argv) { //读取训练好的文件地址 string face_file = "C:/MyOpenCV/install/etc/haarcascades/haarcascade_frontalface_alt.xml"; Mat src = imread("E:/picture/teengirl.jpg"); if (src.data == NULL) { cout << "Can not find the image..."; return -1; } CascadeClassifier face_detect; if (!face_detect.load(face_file)) { cout << "Can not find the file..." << endl; return -2; } imshow("Input img", src); Mat gray,equa_gray; cvtColor(src, gray, COLOR_BGR2GRAY); equalizeHist(gray, equa_gray);//增强对比度 vector<Rect> faces; face_detect.detectMultiScale(equa_gray, faces, 1.1, 3, 0, Size(24, 24)); for (size_t t = 0; t < faces.size(); t++) { rectangle(src, faces[t], Scalar(0, 255, 0), 1, 8); } imshow("Out put", src); waitKey(0); return 0; }
效果: