lbpcascade_frontalface.xml与 haarcascade_eye_tree_eyeglasses.xml从摄像头中配合进行人眼检测的c++程序
时间: 2023-02-12 22:51:37 浏览: 142
这是一个示例C++程序,使用OpenCV库来配合使用lbpcascade_frontalface.xml和haarcascade_eye_tree_eyeglasses.xml进行人眼检测:
```c++
#include <opencv2/objdetect.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
using namespace cv;
using namespace std;
int main()
{
VideoCapture cap(0); // 打开摄像头
if (!cap.isOpened())
{
cout << "摄像头打开失败" << endl;
return -1;
}
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;
// 加载lbpcascade_frontalface.xml和haarcascade_eye_tree_eyeglasses.xml
face_cascade.load("lbpcascade_frontalface.xml");
eyes_cascade.load("haarcascade_eye_tree_eyeglasses.xml");
Mat frame;
while (true)
{
cap >> frame; // 获取摄像头帧
if (frame.empty())
break;
Mat gray;
cvtColor(frame, gray, COLOR_BGR2GRAY); // 转为灰度图像
equalizeHist(gray, gray); // 直方图均衡
// 人脸检测
vector<Rect> faces;
face_cascade.detectMultiScale(gray, faces);
for (size_t i = 0; i < faces.size(); i++)
{
Point center(faces[i].x + faces[i].width / 2, faces[i].y + faces[i].height / 2);
ellipse(frame, center, Size(faces[i].width / 2, faces[i].height / 2), 0, 0, 360, Scalar(255, 0, 0), 2);
Mat faceROI = gray(faces[i]);
vector<Rect> eyes;
// 人眼检测
eyes_cascade.detectMultiScale(faceROI, eyes);
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(frame, eye_center, radius, Scalar(0, 0, 255), 2);
}
}
imshow("Frame", frame);
if (waitKey(1) == 27) // 按ESC键退
阅读全文