opencv 实现人脸检测(harr特征)


我这里用的是已经训练好的haar级联分类器。

眼睛检测 haarcascade_eye_tree_eyeglasses.xml
人脸检测 haarcascade_frontalface_alt2.xml
检测思路:
先把图片转为灰度,接着将图片直方均匀化,在上面处理后的图片矩阵中检测脸的区域,然后把脸这一块圈出来去检测眼睛。
检测函数代码如下:
#include <opencv/cv.h> 
#include <opencv/highgui.h>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
void DetectFace(Mat, Mat);
CascadeClassifier faceCascade;
CascadeClassifier eyes_Cascade;
int main(int argc, char** argv) {
	VideoCapture cap;
	if (!cap.open(0)) {
		cout << "摄像头打开失败!!" << endl;
		return -1;
	}
	if (!faceCascade.load("E:\\VS2015Opencv\\opencv3.2\\opencv\\build\\etc\\haarcascades\\haarcascade_frontalface_alt2.xml")) {
		cout << "人脸检测级联分类器没找到!!" << endl;
		return -1;
	}
	if (!eyes_Cascade.load("E:\\VS2015Opencv\\opencv3.2\\opencv\\build\\etc\\haarcascades\\haarcascade_eye_tree_eyeglasses.xml")) {
		cout << "眼睛检测级联分类器没找到!!" << endl;
		return -1;
	}
	Mat img, imgGray;
	int fps = 60;
	while (true) {
		cap >> img;
		cvtColor(img, imgGray, CV_BGR2GRAY);
		equalizeHist(imgGray, imgGray);//直方图均匀化
		DetectFace(img, imgGray);
		waitKey(1000 / fps);
	}
	return 0;
}

void DetectFace(Mat img, Mat imgGray) {
	namedWindow("src", WINDOW_AUTOSIZE);
	vector<Rect> faces, eyes;
	faceCascade.detectMultiScale(imgGray, faces, 1.2, 5, 0, Size(30, 30));
	for (auto b : faces) {
		cout << "输出一张人脸位置:(x,y):" << "(" << b.x << "," << b.y << ") , (width,height):(" << b.width << "," << b.height << ")" << endl;
	}
	if (faces.size()>0) {
		for (size_t i = 0; i<faces.size(); i++) {
			putText(img, "ugly girl!", cvPoint(faces[i].x, faces[i].y - 10), FONT_HERSHEY_PLAIN, 2.0, Scalar(0, 0, 255));

			rectangle(img, Point(faces[i].x, faces[i].y), Point(faces[i].x + faces[i].width, faces[i].y + faces[i].height), Scalar(0, 0, 255), 1, 8);
			cout << faces[i] << endl;
			//将人脸从灰度图中抠出来
			Mat face_ = imgGray(faces[i]);
			eyes_Cascade.detectMultiScale(face_, eyes, 1.2, 2, 0, Size(30, 30));
			for (size_t j = 0; j < eyes.size(); j++) {
				Point eye_center(faces[i].x + eyes[j].x + eyes[j].width / 2, faces[i].y + eyes[j].y + eyes[j].height / 2);
				int radius = cvRound((eyes[j].width + eyes[j].height)*0.25);
				circle(img, eye_center, radius, Scalar(65, 105, 255), 4, 8, 0);
			}
		}
	}
	imshow("src", img);
}

  

 
posted @ 2019-08-12 20:50  浮沉沉浮  阅读(796)  评论(6编辑  收藏  举报