1. Face Detect
待提取
2. Code
#include "FaceDetect.h"
using namespace std;
int main(void)
{// variablesIplImage * pInpImg = 0;CvHaarClassifierCascade * pCascade = 0; // the face detectorCvMemStorage * pStorage = 0; // memory for detector to useCvSeq * pFaceRectSeq; // memory-access interfacechar ImageFilename[]="AKB48.jpg";// initializationspInpImg = cvLoadImage(ImageFilename, CV_LOAD_IMAGE_COLOR);pStorage = cvCreateMemStorage(0);pCascade = (CvHaarClassifierCascade *)cvLoad(("data/haarcascades/haarcascade_l"),0, 0, 0);// validate that everything initialized properlyif (!pInpImg || !pStorage || !pCascade){printf("Initialization failed: %sn",(!pInpImg) ? "can't load image file" :(!pCascade) ? "can't load haar-cascade -- ""make sure path is correct" :"unable to allocate memory for data storage", ImageFilename);exit(-1);}pFaceRectSeq = cvHaarDetectObjects(pInpImg, pCascade, pStorage,1.1, // increase search scale by 10% each pass3, // merge groups of three detectionsCV_HAAR_DO_CANNY_PRUNING, // skip regions unlikely to contain a facecvSize(200, 200), cvSize(300, 300)); // smallest size face to detect = 40x40,largest size face 使用默认;忽略;// display detected facesdisplayDetections(pInpImg, pFaceRectSeq);// clean up and release resourcescvReleaseImage(&pInpImg);if (pCascade) cvReleaseHaarClassifierCascade(&pCascade);if (pStorage) cvReleaseMemStorage(&pStorage);return 0;
}void displayDetections(IplImage * pInpImg, CvSeq * pFaceRectSeq)
{const char * DISPLAY_WINDOW = "Haar Window";int i;// create a window to display detected facescvNamedWindow(DISPLAY_WINDOW, CV_WINDOW_AUTOSIZE);// draw a rectangular outline around each detectionfor (i = 0; i<(pFaceRectSeq ? pFaceRectSeq->total : 0); i++){CvRect* r = (CvRect*)cvGetSeqElem(pFaceRectSeq, i);CvPoint pt1 = { r->x, r->y };CvPoint pt2 = { r->x + r->width, r->y + r->height };cvRectangle(pInpImg, pt1, pt2, CV_RGB(0, 255, 0), 3, 4, 0);}// display face detectionscvShowImage(DISPLAY_WINDOW, pInpImg);cvWaitKey(0);cvDestroyWindow(DISPLAY_WINDOW);
}
3. Parameters and Tuning
CVAPI(CvSeq*)cvHaarDetectObjects( const CvArr* image,
CvHaarClassifierCascade*cascade, CvMemStorage* storage,
double scale_factor CV_DEFAULT(1.1),
int min_neighbors CV_DEFAULT(3),int flags CV_DEFAULT(0),
CvSize min_size CV_DEFAULT(cvSize(0,0)),CvSize max_size CV_DEFAULT(cvSize(0,0)));
MinimumDetection Scale
一般使用默认值,在data/haarcascades/*.xml中有定义,如haarcascade_l
<size> 24 24 </size>.
可以根据需要将该参数设置的比默认值稍大些,可以使用包含人脸的输入图像的宽或高作对比进行修改;如1/4图像的宽。但是在修改时,需要保证此宽高比与默认保持一致,此处的宽高比为1:1。
ScaleIncrease Rate
规定以多快的速度增加尺度从而在图像中检测人脸,该因子越高,检测的速度越快,但若过高,则会漏掉人脸。一般使用默认值1.1(10%);
Canny Pruning Flag
若设置为CV_HAAR_DO_CANNY_PRUNING,检测时,将略过不太可能包含人脸的区域,减少运算量;
本文发布于:2024-02-02 14:29:43,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170685538544421.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |