您的位置:首页 > 运维架构

人脸识别在opencv下作人脸检测

2012-06-21 09:32 411 查看
人脸识别的第一步,就是人脸检测。把人的脸部从一张照片中用计算机自动识别出来,作为下一步人脸识别的基础。
在opencv中,库中自带了一个利用harr特征的人脸检测训练及检测函数:cvHaarDetectObjects。它利用训练好的检测器,在图片中间检测你想要的物体,如人脸。opencv自带了很多检测器,在%opencv%data/haarcascades目录下,你可以随意取用。或者你也可以自己用图片训练自己的检测器,之后拿来使用。
下面是检测人脸的源代码:
// DetectFaces.c
//
// Example code showing how to detect faces using
// OpenCV's CvHaarClassifierCascade
//
// See also, facedetect.c, in the samples directory.
//
// Usage: DetectFaces <imagefilename>
#include <stdio.h>
#include "cv.h"
#include "highgui.h"
// *** Change this to your install location! ***
// *********************************************
#define OPENCV_ROOT "C:/Program Files/OpenCV"
// *********************************************
void displayDetections(IplImage * pInpImg, CvSeq * pFaceRectSeq, char* FileName);
int main(int argc, char** argv)
{
// variables
IplImage * pInpImg = 0;
CvHaarClassifierCascade * pCascade = 0; // the face detector
CvMemStorage * pStorage = 0; // memory for detector to use
CvSeq * pFaceRectSeq; // memory-access interface
// usage check
if(argc < 2)
{
printf("Missing name of image file!/n"
   "Usage: %s <imagefilename>/n", argv[0]);
exit(-1);
}
// initializations
pInpImg = (argc > 1) ? cvLoadImage(argv[1], CV_LOAD_IMAGE_COLOR) : 0;
pStorage = cvCreateMemStorage(0);
pCascade = (CvHaarClassifierCascade *)cvLoad
   ((OPENCV_ROOT"/data/haarcascades/haarcascade_frontalface_default.xml"),
   0, 0, 0 );
// validate that everything initialized properly
if( !pInpImg || !pStorage || !pCascade )
{
printf("Initialization failed: %s/n",
(!pInpImg)? "can't load image file" :
(!pCascade)? "can't load haar-cascade -- "
   "make sure path is correct" :
"unable to allocate memory for data storage", argv[1]);
exit(-1);
}
// detect faces in image
pFaceRectSeq = cvHaarDetectObjects
(pInpImg, pCascade, pStorage,
1.1, // increase search scale by 10% each pass
3, // merge groups of three detections
CV_HAAR_DO_CANNY_PRUNING, // skip regions unlikely to contain a face
cvSize(40,40)); // smallest size face to detect = 40x40
// display detected faces
displayDetections(pInpImg, pFaceRectSeq, argv[1]);
// clean up and release resources
cvReleaseImage(&pInpImg);
if(pCascade) cvReleaseHaarClassifierCascade(&pCascade);
if(pStorage) cvReleaseMemStorage(&pStorage);
return 0;
}
void displayDetections(IplImage * pInpImg, CvSeq * pFaceRectSeq, char* FileName)
{
const char * DISPLAY_WINDOW = "Haar Window";
int i;
// create a window to display detected faces
cvNamedWindow(DISPLAY_WINDOW, CV_WINDOW_AUTOSIZE);
// draw a rectangular outline around each detection
for(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);
cvSetImageROI(pInpImg, *r);
//char* FileName = argv[1];
IplImage* dst = cvCreateImage( cvSize(92,112), pInpImg->depth, pInpImg->nChannels);
cvResize(pInpImg, dst, CV_INTER_LINEAR);
strcat(FileName,".pgm");
cvSaveImage(FileName, dst);
}
// display face detections
cvShowImage(DISPLAY_WINDOW, pInpImg);
cvWaitKey(0);
cvDestroyWindow(DISPLAY_WINDOW);
}
程序会将人脸检测出来,显示在屏幕上,并存回原来的文件将其覆盖。
在window xp, devcpp 4.9.9.2 下编译通过。
 
原帖地址:http://491352274.blog.163.com/blog/static/12796730420102431751446/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息