VS2019 OPENCV C++ 人脸和眼睛识别 01

环境:电脑镜头/VS 2019/opencv/C++

1. 识别人脸

  1. 实现:
    打开电脑的摄像头;
    对画面里的人脸识别,画图框。
1.1 问题

图框总是缺线,人头歪一定角度后不能识别,张开嘴巴图框会乱。

1.2 注意事项

haarcascade_frontalface_alt2和 “源文件” 应该放到一个路径下。

#include
#include
#include
#include
#include 
using namespace cv;
using namespace std;

//人脸检测的类
CascadeClassifier faceCascade;

int main()
{

	faceCascade.load("haarcascade_frontalface_alt2.xml");   //加载分类器,注意文件路径
	VideoCapture cap;
	cap.open(0);   //打开摄像头
				   //cap.open("../data/test.avi");   //打开视频

	Mat img, imgGray;
	vector<Rect> faces;//开始调试的时候这里报错,最终定义到了忘记using namespace std。
	int c = 0;

	if (!cap.isOpened())
	{
		return 1;
	}

	while (c != 27)//按Esc退出
	{
		cap >> img;
		if (img.channels() == 3)
		{
			cvtColor(img, imgGray, COLOR_RGB2GRAY);
		}
		else
		{
			imgGray = img;
		}

		faceCascade.detectMultiScale(imgGray, faces, 1.2, 2, 0, Size(0, 0));   //检测人脸

		if (faces.size() > 0)
		{
			for (int i = 0; i < faces.size(); i++)
			{
				rectangle(img, Point(faces[i].x, faces[i].y), Point(faces[i].x + faces[i].width, faces[i].y + faces[i].height),
					Scalar(0, 255, 0), 1, 8);    //框出人脸位置
			}
		}
		namedWindow("Camera", 0); 
		imshow("Camera", img);
		c = waitKey(4);//延迟4ms
		//std::cout << c << std::endl;
		//system("pause");
	}

	return 0;
}

2. 识别人脸眼睛

2.1 效果

人脸识别加框
眼睛加圆圈

2.2 问题

图框不稳定,识别率不高。

#include
#include 
#include
#include
#include
#include 
using namespace cv;
using namespace std;


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("haarcascade_frontalface_alt2.xml")) {
		cout << "人脸检测级联分类器没找到!!" << endl;
		return -1;
	}
	if (!eyes_Cascade.load("haarcascade_eye_tree_eyeglasses.xml")) {
		cout << "眼睛检测级联分类器没找到!!" << endl;
		return -1;
	}
	Mat img, imgGray;
	int fps = 60;
	while (true) {
		cap >> img;
		cvtColor(img, imgGray, COLOR_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, "people!", Point(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);
}

参考:
[1] 人眼识别
[2] Videocapture类使用介绍

你可能感兴趣的:(数字图像处理,Opencv)