C++ SVM Opencv3.4实现人脸检测
生活随笔
收集整理的這篇文章主要介紹了
C++ SVM Opencv3.4实现人脸检测
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
很通俗的來說,haar算法計算特征就是用一塊區域內黑色的值減去白色的值。但是一張圖片像素點是非常多的,如果用普通的方法去計算一塊區域的值,效率相當低下。
這里有一種加速計算的方法--積分圖:
定義如下:
并且只需要遍歷一遍圖像就可得到積分圖。
而對于任意一點(x,y) 積分圖可這樣計算 I(x,y)= i(x,y)+I(x-1,y)+I(x,y-1)-I(x-1,y-1)
利用積分圖就可以很高效的計算出圖像中的特征區域。
我這里用的是已經訓練好的haar級聯分類器。 眼睛檢測 haarcascade_eye_tree_eyeglasses.xml 人臉檢測 haarcascade_frontalface_alt2.xml 檢測思路:
先把圖片轉為灰度,接著將圖片直方均勻化,在上面處理后的圖片矩陣中檢測臉的區域,然后把臉這一塊圈出來去檢測眼睛。
檢測函數代碼如下: 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 man!", 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); } 最后檢測效果: 全部代碼如下: #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("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<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); }
這里有一種加速計算的方法--積分圖:
定義如下:
(維基百科貼過來的“—”)
積分圖每一點(x,y)都是這個點對應左上角區域所有值的和并且只需要遍歷一遍圖像就可得到積分圖。
而對于任意一點(x,y) 積分圖可這樣計算 I(x,y)= i(x,y)+I(x-1,y)+I(x,y-1)-I(x-1,y-1)
利用積分圖就可以很高效的計算出圖像中的特征區域。
我這里用的是已經訓練好的haar級聯分類器。 眼睛檢測 haarcascade_eye_tree_eyeglasses.xml 人臉檢測 haarcascade_frontalface_alt2.xml 檢測思路:
先把圖片轉為灰度,接著將圖片直方均勻化,在上面處理后的圖片矩陣中檢測臉的區域,然后把臉這一塊圈出來去檢測眼睛。
檢測函數代碼如下: 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 man!", 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); } 最后檢測效果: 全部代碼如下: #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("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<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); }
總結
以上是生活随笔為你收集整理的C++ SVM Opencv3.4实现人脸检测的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ubuntu18 Win10搭建Caff
- 下一篇: Python设置网卡自己封装的Inter