opencv學習筆記五十二:基於Haar或LBP級聯分類器的實時人臉人眼檢測
阿新 • • 發佈:2018-12-10
#include<opencv2\opencv.hpp> using namespace cv; using namespace std; int main(int arc, char** argv) { namedWindow("output", CV_WINDOW_AUTOSIZE); VideoCapture capture(0); //例項化分類器 CascadeClassifier face_classifier; CascadeClassifier eye_classifier; //載入opencv訓練好的資料 face_classifier.load("E:/anzhuang/opencv3.4.1/opencv/build/etc/haarcascades/haarcascade_frontalface_alt.xml"); eye_classifier.load("E:/anzhuang/opencv3.4.1/opencv/build/etc/haarcascades/haarcascade_eye.xml"); Mat frame, gray; vector<Rect>faces; vector<Rect>eyes; Rect rect; Rect eyesRoi; Mat eyesImg; while (capture.read(frame)) { cvtColor(frame, gray, CV_BGR2GRAY); //直方圖均衡化 equalizeHist(gray, gray); //人臉檢測 face_classifier.detectMultiScale(gray, faces, 1.2, 3, 0, Size(30, 30)); for (int i = 0; i < faces.size(); i++) { //畫出人臉 rectangle(frame, faces[i], Scalar(0, 0, 255), 2); //獲得人眼ROI eyesRoi.x = faces[i].x; eyesRoi.y = faces[i].y; eyesRoi.width = faces[i].width; eyesRoi.height = faces[i].height / 2; eyesImg = gray(eyesRoi); //在ROI影象中進行人眼檢測 eye_classifier.detectMultiScale(eyesImg, eyes, 1.2, 3, 0, Size(20, 20)); for (int j = 0; j < eyes.size(); j++) { rect.x = faces[i].x + eyes[j].x; rect.y = faces[i].y + eyes[j].y; rect.width = eyes[j].width; rect.height = eyes[j].height; rectangle(frame, rect, Scalar(0, 255, 0), 2); } } imshow("output", frame); char c = waitKey(20); if (c == 27) { break; } } waitKey(0); return 0; } //基於模板匹配的人眼跟蹤 /* int main(int arc, char** argv) { namedWindow("input", CV_WINDOW_AUTOSIZE); namedWindow("output", CV_WINDOW_AUTOSIZE); VideoCapture capture(0); //例項化分類器 CascadeClassifier face_classifier; CascadeClassifier eye_classifier; //載入opencv訓練好的資料 face_classifier.load("E:/anzhuang/opencv3.4.1/opencv/build/etc/haarcascades/haarcascade_frontalface_alt.xml"); eye_classifier.load("E:/anzhuang/opencv3.4.1/opencv/build/etc/haarcascades/haarcascade_eye.xml"); Mat frame, gray; vector<Rect>faces; vector<Rect>eyes; Rect rect1, rect2; Rect lefteyes, righteyes; Mat lefteyesImg, righteyesImg; Mat temp1, temp2; Mat result1, result2; double min, max; Point minLoc, maxLoc; bool firstFrame = true; int count = 0; while (capture.read(frame)) { cvtColor(frame, gray, CV_BGR2GRAY); //直方圖均衡化 equalizeHist(gray, gray); face_classifier.detectMultiScale(gray, faces, 1.2, 3, 0, Size(30, 30)); for (int i = 0; i < faces.size(); i++) { //畫出人臉 rectangle(frame, faces[i], Scalar(0, 0, 255), 2); //獲得左眼ROI lefteyes.x = faces[i].x; lefteyes.y = faces[i].y; lefteyes.width = faces[i].width/2; lefteyes.height = faces[i].height / 2; lefteyesImg = gray(lefteyes); //獲得右眼ROI righteyes.x = faces[i].x + faces[i].width / 2; righteyes.y = faces[i].y; righteyes.width = faces[i].width / 2; righteyes.height = faces[i].height / 2; righteyesImg = gray(righteyes); if (firstFrame) { eye_classifier.detectMultiScale(lefteyesImg, eyes, 1.2, 3, 0, Size(20, 20)); if (eyes.size()) { rect1.x = lefteyes.x + eyes[0].x; rect1.y = lefteyes.y + eyes[0].y; rect1.width = eyes[0].width; rect1.height = eyes[0].height; rectangle(frame, rect1, Scalar(0, 255, 0), 2); temp1 = gray(rect1); } eye_classifier.detectMultiScale(righteyesImg, eyes, 1.2, 3, 0, Size(20, 20)); if (eyes.size()) { rect2.x = righteyes.x + eyes[0].x; rect2.y = righteyes.y + eyes[0].y; rect2.width = eyes[0].width; rect2.height = eyes[0].height; rectangle(frame, rect2, Scalar(0, 255, 0), 2); temp2 = gray(rect2); } count++; } imshow("input", frame); if (count >= 5) { firstFrame = false; result1 = Mat::zeros(Size(lefteyes.width - temp1.cols + 1, lefteyes.height - temp1.rows + 1), CV_32FC1); result2 = Mat::zeros(Size(righteyes.width - temp2.cols + 1, righteyes.height - temp2.rows + 1), CV_32FC1); matchTemplate(lefteyesImg, temp1, result1, 3); normalize(result1, result1, 0, 1, NORM_MINMAX); minMaxLoc(result1, &min, &max, &minLoc, &maxLoc, Mat()); rectangle(frame, Rect(maxLoc.x + lefteyes.x, maxLoc.y + lefteyes.y, temp1.cols, temp1.rows), Scalar(0, 255, 0), 2); matchTemplate(righteyesImg, temp2, result2, 3); normalize(result2, result2, 0, 1, NORM_MINMAX); minMaxLoc(result1, &min, &max, &minLoc, &maxLoc); rectangle(frame, Rect(maxLoc.x + righteyes.x, maxLoc.y+ righteyes.y, temp1.cols, temp1.rows), Scalar(0, 255, 0), 2); } imshow("output", frame); } char c = waitKey(20); if (c == 27) { break; } } waitKey(0); return 0; } */