1. 程式人生 > >利用opencv實現人臉檢測(C++版)

利用opencv實現人臉檢測(C++版)

小編所有的帖子都是基於unbuntu系統的,當然稍作修改同樣試用於windows的,經過小編的絞盡腦汁,把剛剛發的那篇python 實現人臉和眼睛的檢測的程式用C++ 實現了,當然,也參考了不少大神的部落格,下面我們就一起來看看:

Linux系統下安裝opencv我就再囉嗦一次,防止有些人沒有安裝沒調試出來噴小編的程式是個坑,
sudo apt-get install libcv-dev
sudo apt-get install libopencv-dev
看看你的usr/share/opencv/haarcascades目錄下有沒有出現幾個訓練集.XML檔案,接下來我拿人臉和眼睛檢測作為例項玩一下,程式如下:

好多人不會編譯opencv,我再多寫幾句解決一下好多菜鳥的困難吧
copy完程式碼之後,儲存為xiaorun.cpp哦,記得編譯試用
個g++ -o xiaorun ./xiaorun.cpp -lopencv_highgui -lopenc_imgproc -lopencv_core -lopencv_objdetect
即可實現

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <iostream>
using namespace cv;
using namespace std;

void detectAndDraw( Mat& img, CascadeClassifier& cascade,
                   CascadeClassifier& nestedCascade,
                   double
scale, bool tryflip ); int main() { CascadeClassifier cascade, nestedCascade; bool stop = false; cascade.load("/usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml"); nestedCascade.load("/usr/share/opencv/haarcascades/haarcascade_eye.xml"); // frame = imread("renlian.jpg"); VideoCapture cap(0
); //開啟預設攝像頭 if(!cap.isOpened()) { return -1; } Mat frame; Mat edges; while(!stop) { cap>>frame; detectAndDraw( frame, cascade, nestedCascade,2,0 ); if(waitKey(30) >=0) stop = true; imshow("cam",frame); } //CascadeClassifier cascade, nestedCascade; // bool stop = false; //訓練好的檔名稱,放置在可執行檔案同目錄下 // cascade.load("/usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml"); // nestedCascade.load("/usr/share/opencv/haarcascades/aarcascade_eye.xml"); // frame = imread("renlian.jpg"); // detectAndDraw( frame, cascade, nestedCascade,2,0 ); // waitKey(); //while(!stop) //{ // cap>>frame; // detectAndDraw( frame, cascade, nestedCascade,2,0 ); if(waitKey(30) >=0) stop = true; //} return 0; } void detectAndDraw( Mat& img, CascadeClassifier& cascade, CascadeClassifier& nestedCascade, double scale, bool tryflip ) { int i = 0; double t = 0; //建立用於存放人臉的向量容器 vector<Rect> faces, faces2; //定義一些顏色,用來標示不同的人臉 const static Scalar colors[] = { CV_RGB(0,0,255), CV_RGB(0,128,255), CV_RGB(0,255,255), CV_RGB(0,255,0), CV_RGB(255,128,0), CV_RGB(255,255,0), CV_RGB(255,0,0), CV_RGB(255,0,255)} ; //建立縮小的圖片,加快檢測速度 //nt cvRound (double value) 對一個double型的數進行四捨五入,並返回一個整型數! Mat gray, smallImg( cvRound (img.rows/scale), cvRound(img.cols/scale), CV_8UC1 ); //轉成灰度影象,Harr特徵基於灰度圖 cvtColor( img, gray, CV_BGR2GRAY ); // imshow("灰度",gray); //改變影象大小,使用雙線性差值 resize( gray, smallImg, smallImg.size(), 0, 0, INTER_LINEAR ); // imshow("縮小尺寸",smallImg); //變換後的影象進行直方圖均值化處理 equalizeHist( smallImg, smallImg ); //imshow("直方圖均值處理",smallImg); //程式開始和結束插入此函式獲取時間,經過計算求得演算法執行時間 t = (double)cvGetTickCount(); //檢測人臉 //detectMultiScale函式中smallImg表示的是要檢測的輸入影象為smallImg,faces表示檢測到的人臉目標序列,1.1表示 //每次影象尺寸減小的比例為1.1,2表示每一個目標至少要被檢測到3次才算是真的目標(因為周圍的畫素和不同的視窗大 //小都可以檢測到人臉),CV_HAAR_SCALE_IMAGE表示不是縮放分類器來檢測,而是縮放影象,Size(30, 30)為目標的 //最小最大尺寸 cascade.detectMultiScale( smallImg, faces, 1.1, 2, 0 //|CV_HAAR_FIND_BIGGEST_OBJECT //|CV_HAAR_DO_ROUGH_SEARCH |CV_HAAR_SCALE_IMAGE ,Size(30, 30)); //如果使能,翻轉影象繼續檢測 if( tryflip ) { flip(smallImg, smallImg, 1); // imshow("反轉影象",smallImg); cascade.detectMultiScale( smallImg, faces2, 1.1, 2, 0 //|CV_HAAR_FIND_BIGGEST_OBJECT //|CV_HAAR_DO_ROUGH_SEARCH |CV_HAAR_SCALE_IMAGE ,Size(30, 30) ); for( vector<Rect>::const_iterator r = faces2.begin(); r != faces2.end(); r++ ) { faces.push_back(Rect(smallImg.cols - r->x - r->width, r->y, r->width, r->height)); } } t = (double)cvGetTickCount() - t; // qDebug( "detection time = %g ms\n", t/((double)cvGetTickFrequency()*1000.) ); for( vector<Rect>::const_iterator r = faces.begin(); r != faces.end(); r++, i++ ) { Mat smallImgROI; vector<Rect> nestedObjects; Point center; Scalar color = colors[i%8]; int radius; double aspect_ratio = (double)r->width/r->height; if( 0.75 < aspect_ratio && aspect_ratio < 1.3 ) { //標示人臉時在縮小之前的影象上標示,所以這裡根據縮放比例換算回去 center.x = cvRound((r->x + r->width*0.5)*scale); center.y = cvRound((r->y + r->height*0.5)*scale); radius = cvRound((r->width + r->height)*0.25*scale); circle( img, center, radius, color, 3, 8, 0 ); } else rectangle( img, cvPoint(cvRound(r->x*scale), cvRound(r->y*scale)), cvPoint(cvRound((r->x + r->width-1)*scale), cvRound((r->y + r->height-1)*scale)), color, 3, 8, 0); if( nestedCascade.empty() ) continue; smallImgROI = smallImg(*r); //同樣方法檢測人眼 nestedCascade.detectMultiScale( smallImgROI, nestedObjects, 1.1, 2, 0 //|CV_HAAR_FIND_BIGGEST_OBJECT //|CV_HAAR_DO_ROUGH_SEARCH //|CV_HAAR_DO_CANNY_PRUNING |CV_HAAR_SCALE_IMAGE ,Size(30, 30) ); for( vector<Rect>::const_iterator nr = nestedObjects.begin(); nr != nestedObjects.end(); nr++ ) { center.x = cvRound((r->x + nr->x + nr->width*0.5)*scale); center.y = cvRound((r->y + nr->y + nr->height*0.5)*scale); radius = cvRound((nr->width + nr->height)*0.25*scale); circle( img, center, radius, color, 3, 8, 0 ); } } // imshow( "識別結果", img ); } 收藏