OpenCV是如何實(shí)現(xiàn)人臉檢測(cè)的?
OpenCV中有檢測(cè)人臉的函數(shù)(該函數(shù)還可以檢測(cè)一些其他物體), 甚至還包含一些預(yù)先訓(xùn)練好的物體識(shí)別文件。
所以利用這些現(xiàn)成的東西就可以很快做出一個(gè)人臉檢測(cè)的程序。
主要步驟為:
1.加載分類器。
用cvLoad函數(shù)讀入xml格式的文件。文件在OpenCV安裝目錄下的“data/haarcascades/”路徑下。
2.讀入待檢測(cè)圖像。讀入圖片或者視頻。
3.檢測(cè)人臉。
#include "cv.h"
#include "highgui.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include <float.h>
#include <limits.h>
#include <time.h>
#include <ctype.h>
#ifdef _EiC
#define WIN32
#endif
static CvMemStorage* storage = 0;
static CvHaarClassifierCascade* cascade = 0;
void detect_and_draw( IplImage* image );
const char* cascade_name =
"haarcascade_frontalface_alt.xml";
/* "haarcascade_profileface.xml";*/
int main( int argc, char** argv )
{
cascade_name = "haarcascade_frontalface_alt2.xml";
cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );
if( !cascade )
{
fprintf( stderr, "ERROR: Could not load classifier cascade\n" );
return -1;
}
storage = cvCreateMemStorage(0);
cvNamedWindow( "result", 1 );
const char* filename = "Lena.jpg";
IplImage* image = cvLoadImage( filename, 1 );
if( image )
{
detect_and_draw( image );
cvWaitKey(0);
cvReleaseImage( &image );
}
cvDestroyWindow("result");
return 0;
}
void detect_and_draw(IplImage* img )
{
double scale=1.2;
static CvScalar colors[] = {
{{0,0,255}},{{0,128,255}},{{0,255,255}},{{0,255,0}},
{{255,128,0}},{{255,255,0}},{{255,0,0}},{{255,0,255}}
};//Just some pretty colors to draw with
//Image Preparation
//
IplImage* gray = cvCreateImage(cvSize(img->width,img->height),8,1);
IplImage* small_img=cvCreateImage(cvSize(cvRound(img->width/scale),cvRound(img->height/scale)),8,1);
cvCvtColor(img,gray, CV_BGR2GRAY);
cvResize(gray, small_img, CV_INTER_LINEAR);
cvEqualizeHist(small_img,small_img); //直方圖均衡
//Detect objects if any
//
cvClearMemStorage(storage);
double t = (double)cvGetTickCount();
CvSeq* objects = cvHaarDetectObjects(small_img,
cascade,
storage,
1.1,
2,
0/*CV_HAAR_DO_CANNY_PRUNING*/,
cvSize(30,30));
t = (double)cvGetTickCount() - t;
printf( "detection time = %gms\n", t/((double)cvGetTickFrequency()*1000.) );
//Loop through found objects and draw boxes around them
for(int i=0;i<(objects? objects->total:0);++i)
{
CvRect* r=(CvRect*)cvGetSeqElem(objects,i);
cvRectangle(img, cvPoint(r->x*scale,r->y*scale), cvPoint((r->x+r->width)*scale,(r->y+r->height)*scale), colors[i%8]);
}
for( int i = 0; i < (objects? objects->total : 0); i++ )
{
CvRect* r = (CvRect*)cvGetSeqElem( objects, i );
CvPoint center;
int radius;
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);
cvCircle( img, center, radius, colors[i%8], 3, 8, 0 );
}
cvShowImage( "result", img );
cvReleaseImage(&gray);
cvReleaseImage(&small_img);
}
流程說(shuō)明:
原始待檢測(cè)圖像經(jīng)過(guò)resize,生成不同尺寸的圖像構(gòu)建圖像金字塔作為網(wǎng)絡(luò)的輸入。
構(gòu)建的圖像金字塔,其層數(shù)由兩個(gè)因素決定,第一個(gè)是設(shè)置的最小人臉minSize,第二個(gè)是縮放因子factor,最小人臉表示min(w,h),論文中說(shuō)明最小人臉不能小于12,給出的縮放因子0.709可以根據(jù)公式計(jì)算圖像金字塔的層數(shù)
minL=org_L*(12/minsize)*factor^(n),n={0,1,2,3,...,N}
其中n就是金字塔的層數(shù),org_L是輸入原始圖像的最小邊min(W,H),minisize是人為根據(jù)應(yīng)用場(chǎng)景設(shè)定,在保證minL大于12的情況下,所有的n就構(gòu)成金字塔的層。所以minsize的值越小,n的取值范圍就越大,計(jì)算量就相應(yīng)地增加,能夠檢測(cè)到的人臉越小