OpenCV的人脸检测主要是调用训练好的cascade(Haar分类器)来进行模式匹配。
1. 新建项目
启动Code::Blocks,选择File->New->Project, 项目类型选择:Console Application,项目名为:MyFace,其他按默认选择。
2. 代码
打开main.cpp文件,输入以下代码:
#include
using namespace std;
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
static CvMemStorage* storage = 0;
static CvHaarClassifierCascade* cascade = 0;
void detect_and_draw(IplImage* image);
const char* cascade_name = "haarcascade_frontalface_alt.xml";
int main(int argc, char** argv)
{
int width = 640;
int height = 400;
videoInput video;//创建视频捕获对象
video.setupDevice(0, width, height);//配置设备
//video.showSettingsWindow(0);//该语句可以显示视频设置窗口,可以去掉
IplImage* frame, *frame_copy = 0;
int optlen = strlen("--cascade=");
const char* input_name;
if(argc > 1 && strncmp(argv[1], "--cascade=", optlen) == 0)
{
cascade_name = argv[1] + optlen;
input_name = argc > 2 ? argv[2] : 0;
}
else
{
input_name = argc > 1 ? argv[1] : 0;
}
cascade = (CvHaarClassifierCascade*) cvLoad(cascade_name, 0, 0, 0);
if(!cascade)
{
fprintf(stderr, "ERROR: Could not load classifier cascade/n");
fprintf(stderr, "Usage: myface --cascade=/"
return -1;
}
storage = cvCreateMemStorage(0);
cvNamedWindow("result", 1);
frame = cvCreateImage(cvSize(width,height), IPL_DEPTH_8U, 3);
for(;;)
{
if(video.isFrameNew(0))
{
video.getPixels(0, (unsigned char *)frame->imageData, false, true);//获取一帧
}
if(!frame)
break;
if(!frame_copy)
frame_copy = cvCreateImage(cvSize(frame->width, frame->height), IPL_DEPTH_8U, frame->nChannels);
if(frame->origin == IPL_ORIGIN_TL)
cvCopy(frame, frame_copy, 0);
else
cvFlip(frame, frame_copy, 0);
detect_and_draw(frame_copy);
if(cvWaitKey(10) >= 0)
break;
}
cvReleaseImage(&frame_copy);
cvReleaseImage(&frame);
return 0;
}
void detect_and_draw( IplImage* img )
{
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}}
};
double scale = 1.3;
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 );
int i;
cvCvtColor( img, gray, CV_BGR2GRAY );
cvResize( gray, small_img, CV_INTER_LINEAR );
cvEqualizeHist( small_img, small_img );
cvClearMemStorage( storage );
if( cascade )
{
double t = (double)cvGetTickCount();
CvSeq* faces = 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.) );
for( i = 0; i < (faces ? faces->total : 0); i++ )
{
CvRect* r = (CvRect*)cvGetSeqElem( faces, 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 );
}
3. 设置
打开Project-〉Build Options选项卡
选择Search directories,在Compiler里Add进以下目录:
C:/OpenCV2.2/include
C:/OpenCV2.2/3rdparty/include
在Linker里Add进以下目录:
C:/OpenCV2.2/lib
C:/OpenCV2.2/3rdparty/lib
C:/Program Files/Microsoft SDKs/Windows/v7.0A/Lib
选择Linker settings,添加以下文件:
C:/OpenCV2.2/lib 此目录下的所有文件
C:/OpenCV2.2/3rdparty/lib 此目录下的所有文件
C:/Program Files/Microsoft SDKs/Windows/v7.0A/Lib 此目录下所有ole开头的文件
4. 编译
点击Build->Build编译项目。
5. 运行
将C:/OpenCV2.2/data/haarcascades目录下的haarcascade_frontalface_alt.xml复制到MyFace项目根目录下。
或者打开Project-〉Set programs‘ arguments,在Program arguments里输入haarcascade_frontalface_alt.xml的全路径地址。
点击Build-〉Run
第一次运行的时候会出现一个摄像头选择界面,点确定后就可以看到视频,如果有人脸进入画面,将被标示出来。