【人脸检测与识别】C++、OpenCV实现人脸检测

一、效果

【人脸检测与识别】C++、OpenCV实现人脸检测_第1张图片

二、原理

OpenCV自带的Haar特征,加级联分类器实现。

三、代码

#include  
#include 
#include 
#include 
#include 
#include 
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("C:\\Users\\cb\\source\\repos\\Project2\\x64\\Debug\\haarcascade_frontalface_alt2.xml") ) {
		cout << "人脸检测级联分类器没找到!!" << endl;
		return -1;
	}
	if (!eyes_Cascade.load("C:\\Users\\cb\\source\\repos\\Project2\\x64\\Debug\\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 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

四、参考

opencv 实现人脸检测(opencv3.3 + visual studio 2017)

你可能感兴趣的:(图像处理)