Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
OpenCV 人脸识别
#include <opencv2/core/core.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
string face_cascade_name = "haarcascade_frontalface_alt.xml";
CascadeClassifier face_cascade;
void detectAndDisplay(Mat frame);
int main(int argc, char** argv)
{
Mat image = imread("face.png");
if (!image.data)
{
printf("[ERR] 加载图片失败");
}
if (!face_cascade.load(face_cascade_name))
{
printf("[ERR] 无法加载级联分类器文件!\n");
}
detectAndDisplay(image);
waitKey(0);
}
void detectAndDisplay(Mat frame)
{
vector<Rect> faces;
Mat frame_gray;
cvtColor(frame, frame_gray, CV_BGR2GRAY);
equalizeHist(frame_gray, frame_gray);
face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));
for (int i = 0; i < faces.size(); i++)
{
Point center(faces[i].x + faces[i].width * .5, faces[i].y + faces[i].height * .5);
ellipse(frame, center, Size(faces[i].width * .5, faces[i].height * .5), 0, 0, 360, Scalar(255, 0, 255), 4, 8, 0);
}
imshow("人脸识别", frame);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment