日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

人脸位置识别

發布時間:2025/3/20 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 人脸位置识别 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

haar classifier cascade是opencv下自帶的人臉檢測的級聯分類器,支持haar特征,新版本的CascadeClassifier好像支持LBP特征,沒試過。

HaarDetect.h

[cpp]?view plaincopy print?
  • #ifndef?HAARDETECT_H??
  • #define?HAARDETECT_H??
  • #include?<opencv2\highgui\highgui.hpp>??
  • #include?<opencv2\imgproc\imgproc.hpp>??
  • #include?<opencv2\objdetect\objdetect.hpp>??
  • using?namespace?std;??
  • using?namespace?cv;??
  • ??
  • CvHaarClassifierCascade*?Load_Haar_Cascade();??
  • ??
  • void?detectFace(IplImage*?frame,CvHaarClassifierCascade?*faceCascade,CvRect?*faceRect,int?&count);??
  • ??
  • #endif??


  • HaarDetect.cpp

    [cpp]?view plaincopy print?
  • #include?"HaarDetect.h"??
  • #include?<iostream>??
  • #include?<opencv2/opencv.hpp>??
  • using?namespace?std;??
  • CvHaarClassifierCascade*?Load_Haar_Cascade()??
  • {??
  • ????CvHaarClassifierCascade*?faceCascade?=?NULL;??
  • ????const?char?*faceCascadeFilename?=?"D:\\軟件\\opencv\\sources\\data\\cascades\\haarcascade_frontalface_alt.xml";??
  • ????faceCascade?=?(CvHaarClassifierCascade*)cvLoad(faceCascadeFilename,0,0,0);??
  • ????if(faceCascade==NULL)??
  • ????{??
  • ????????cout<<"error?in?Load_Haar_Cascade:faceCascade?is?NULL"<<endl;??
  • ????}??
  • ????return?faceCascade;??
  • }??
  • void?detectFace(IplImage*?frame,CvHaarClassifierCascade?*faceCascade,CvRect?*faceRect,int?&count)??
  • {??
  • ??????
  • ????const?int?flag?=?CV_HAAR_DO_CANNY_PRUNING;??
  • ????CvSeq?*detectedFaces;??
  • ????CvMemStorage*?storage;??
  • ????const?double?scale_factore?=?1.1f;??
  • ????storage?=?cvCreateMemStorage(0);??
  • ????cvClearMemStorage(storage);??
  • ????if(faceCascade==NULL)??
  • ????{??
  • ????????cout<<"error?in?detectFace:faceCascade?is?NULL!"<<endl;??
  • ????????exit(1);??
  • ????}??
  • ????IplImage*?grayFrame?=?NULL;??
  • ????grayFrame?=?cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U,1);??
  • ????cvCvtColor(frame,grayFrame,CV_RGB2GRAY);??
  • ????detectedFaces?=?cvHaarDetectObjects(grayFrame,faceCascade,storage,scale_factore,3,flag,cvSize(20,20));??
  • ????count?=?detectedFaces->total;??
  • ????if(detectedFaces==NULL)??
  • ????{??
  • ????????cout<<"error?in?detectFace:detectedFaces?is?NULL"<<endl;??
  • ????}??
  • ????for(int?i=0;i<(detectedFaces->total);i++)??
  • ????{??
  • ????????faceRect[i]?=?*(CvRect?*)cvGetSeqElem(detectedFaces,i);??
  • ????}??
  • ????if(faceRect?==?NULL)??
  • ????{??
  • ????????cout<<"error?in?detectFace:faceRect?is?NULL!"<<endl;??
  • ????}??
  • ????cvReleaseMemStorage(&storage);??
  • ????cvReleaseImage(&grayFrame);??
  • }??
  • main.cpp

    [cpp]?view plaincopy print?
  • #include?<iostream>??
  • #include?<windows.h>??
  • //#include?<conio.h>??
  • #include?<opencv2\highgui\highgui.hpp>??
  • #include?<opencv2\imgproc\imgproc.hpp>??
  • #include?<opencv2\objdetect\objdetect.hpp>??
  • #include?"HaarDetect.h"??
  • using?namespace?std;??
  • using?namespace?cv;??
  • ??
  • int?main()??
  • {??
  • ????CvCapture?*camera;??
  • ????CvHaarClassifierCascade*?faceCascade?=?NULL;??
  • ????CvRect?faceRect[30];??
  • ????int?faceCount;??
  • ????faceCascade?=?Load_Haar_Cascade();??
  • ????if(!faceCascade)??
  • ????????return?0;??
  • ????camera?=?cvCreateCameraCapture(0);??
  • ????if(camera==NULL)??
  • ????{??
  • ????????cout<<"camera?is?NULL"<<endl;??
  • ????????return?0;??
  • ????}??
  • ????IplImage?*frame;??
  • ????/*IplImage?*frame?=?cvCreateImage(??
  • ????cvSize(?cvGetCaptureProperty(camera,CV_CAP_PROP_FRAME_WIDTH),cvGetCaptureProperty(camera,CV_CAP_PROP_FRAME_HEIGHT)?),?
  • ????IPL_DEPTH_8U,1);*/??
  • ????Sleep(5000);????//wait?for?init?the?camera??
  • ????while(true)??
  • ????{??
  • ????????/*faceRect?=?NULL;*/??
  • ????????faceCount?=?0;??
  • ????????frame?=?cvQueryFrame(camera);??
  • ????????if(frame==NULL)??
  • ????????{??
  • ????????????cout<<"frame?is?NULL"<<endl;??
  • ????????????break;??
  • ????????}??
  • ????????detectFace(frame,faceCascade,faceRect,faceCount);??
  • ????????//if(faceRect?==?NULL)??
  • ????????//{??
  • ????????//??exit(1);??
  • ????????//}??
  • ????????for(int?i=0;i?<?faceCount;i++)??
  • ????????{??
  • ????????????cvRectangle(frame,cvPoint(faceRect[i].x,faceRect[i].y),cvPoint(faceRect[i].x+faceRect[i].width-1,faceRect[i].y+faceRect[i].height-1),?CV_RGB(0,255,0),?1,?8,?0);??
  • ????????}??
  • ????????cvNamedWindow("Camera",CV_WINDOW_AUTOSIZE);??
  • ????????cvShowImage("Camera",frame);??
  • ????????cvWaitKey(10);??
  • ????}??
  • ????destroyAllWindows();??
  • ????cvReleaseCapture(&camera);??
  • ????return?0;??
  • } ?
  • 總結

    以上是生活随笔為你收集整理的人脸位置识别的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。