您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“C++如何利用opencv實現人臉檢測”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“C++如何利用opencv實現人臉檢測”這篇文章吧。
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 ); }
以上是“C++如何利用opencv實現人臉檢測”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。