您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“C++如何利用opencv實(shí)現(xiàn)人臉檢測(cè)”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“C++如何利用opencv實(shí)現(xiàn)人臉檢測(cè)”這篇文章吧。
Linux系統(tǒng)下安裝opencv我就再啰嗦一次,防止有些人沒(méi)有安裝沒(méi)調(diào)試出來(lái)噴小編的程序是個(gè)坑,
sudo apt-get install libcv-dev
sudo apt-get install libopencv-dev
看看你的usr/share/opencv/haarcascades目錄下有沒(méi)有出現(xiàn)幾個(gè)訓(xùn)練集.XML文件,接下來(lái)我拿人臉和眼睛檢測(cè)作為實(shí)例玩一下,程序如下:
好多人不會(huì)編譯opencv,我再多寫幾句解決一下好多菜鳥(niǎo)的困難吧
copy完代碼之后,保存為xiaorun.cpp哦,記得編譯試用個(gè)g++ -o xiaorun ./xiaorun.cpp -lopencv_highgui -lopenc_imgproc -lopencv_core -lopencv_objdetect
即可實(shí)現(xiàn)
#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); //打開(kāi)默認(rèn)攝像頭 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; //訓(xùn)練好的文件名稱,放置在可執(zhí)行文件同目錄下 // 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; //定義一些顏色,用來(lái)標(biāo)示不同的人臉 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)} ; //建立縮小的圖片,加快檢測(cè)速度 //nt cvRound (double value) 對(duì)一個(gè)double型的數(shù)進(jìn)行四舍五入,并返回一個(gè)整型數(shù)! Mat gray, smallImg( cvRound (img.rows/scale), cvRound(img.cols/scale), CV_8UC1 ); //轉(zhuǎn)成灰度圖像,Harr特征基于灰度圖 cvtColor( img, gray, CV_BGR2GRAY ); // imshow("灰度",gray); //改變圖像大小,使用雙線性差值 resize( gray, smallImg, smallImg.size(), 0, 0, INTER_LINEAR ); // imshow("縮小尺寸",smallImg); //變換后的圖像進(jìn)行直方圖均值化處理 equalizeHist( smallImg, smallImg ); //imshow("直方圖均值處理",smallImg); //程序開(kāi)始和結(jié)束插入此函數(shù)獲取時(shí)間,經(jīng)過(guò)計(jì)算求得算法執(zhí)行時(shí)間 t = (double)cvGetTickCount(); //檢測(cè)人臉 //detectMultiScale函數(shù)中smallImg表示的是要檢測(cè)的輸入圖像為smallImg,faces表示檢測(cè)到的人臉目標(biāo)序列,1.1表示 //每次圖像尺寸減小的比例為1.1,2表示每一個(gè)目標(biāo)至少要被檢測(cè)到3次才算是真的目標(biāo)(因?yàn)橹車南袼睾筒煌拇翱诖? //小都可以檢測(cè)到人臉),CV_HAAR_SCALE_IMAGE表示不是縮放分類器來(lái)檢測(cè),而是縮放圖像,Size(30, 30)為目標(biāo)的 //最小最大尺寸 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)); //如果使能,翻轉(zhuǎn)圖像繼續(xù)檢測(cè) if( tryflip ) { flip(smallImg, smallImg, 1); // imshow("反轉(zhuǎn)圖像",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 ) { //標(biāo)示人臉時(shí)在縮小之前的圖像上標(biāo)示,所以這里根據(jù)縮放比例換算回去 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); //同樣方法檢測(cè)人眼 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( "識(shí)別結(jié)果", img ); }
以上是“C++如何利用opencv實(shí)現(xiàn)人臉檢測(cè)”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。