ITPub博客

首页 > 应用开发 > IT综合 > opencv 人脸检测 源码解析

opencv 人脸检测 源码解析

原创 IT综合 作者:caodong8962 时间:2011-04-07 15:15:18 0 删除 编辑

opencv自带的人脸检测的例子,为了方便大家学习

我在关键代码处加了些注释,希望对大家有帮助


//opencv自带的人脸检测的列子,还可以检测出人眼
//使用这个算法不仅仅可以检测出人脸或者人眼只要我们定义了任何其他物体的结构信息
//可能是我们实现第三部分功能的关键
//(以xml文件存储)便可以找出图像中该物体所在的矩形区域,很方便
//物体检测大概步骤:
//1、要先定义识别物体的结构信息(xml文件),该例中用到的是opencv已经定义好的
// 人脸文件haarcascade_frontalface_alt.xml和人眼文件haarcascade_eye_tree_eyeglasses.xml
//2、将物体信息文件load到定义的CascadeClassifier的变量内
//3、调用CascadeClassifier的detectMultiScale函数,检测出物体所在巨型区域
//注:这部分深入了解CascadeClassifier即可,检测自定义物体要研究数据的定义格式

#define CV_NO_BACKWARD_COMPATIBILITY

#include "cv.h"
#include "highgui.h"

#include
#include

#ifdef _EiC
#define WIN32
#endif

using namespace std;
using namespace cv;

void detectAndDraw( Mat& img,
CascadeClassifier& cascade, CascadeClassifier& nestedCascade,
double scale);

//这两个文件是haarscale算法所用到的物体形状的xml数据
//我们自己实现识别生活物品的那部分,应该是对每个物体写出类似的xml数据文件就应该可以了。
//人脸的形状特征数据文件
String cascadeName =
"../../data/haarcascades/haarcascade_frontalface_alt.xml";
//眼睛的形状特征数据文件
String nestedCascadeName =
"../../data/haarcascades/haarcascade_eye_tree_eyeglasses.xml";

int main( int argc, const char** argv )
{
CvCapture* capture = 0;
Mat frame, frameCopy, image;
const String scaleOpt = "--scale=";
size_t scaleOptLen = scaleOpt.length();
const String cascadeOpt = "--cascade=";
size_t cascadeOptLen = cascadeOpt.length();
const String nestedCascadeOpt = "--nested-cascade";
size_t nestedCascadeOptLen = nestedCascadeOpt.length();
String inputName;

CascadeClassifier cascade, nestedCascade;//应该是存储haarcascade用到的形状文件
double scale = 1;//检测出物体后所画的圆圈的大小

//这部分是由命令行下启动时输入的参数捕获包括haarcascade_frontalface_alt.xml,
//和haarcascade_eye_tree_eyeglasses.xml的路径
//我们不用管这部分,直接用文件路径作参数
for( int i = 1; i < argc; i++ )
{
if( cascadeOpt.compare( 0, cascadeOptLen, argv[i], cascadeOptLen ) == 0 )
cascadeName.assign( argv[i] + cascadeOptLen );
else if( nestedCascadeOpt.compare( 0, nestedCascadeOptLen, argv[i], nestedCascadeOptLen ) == 0 )
{
if( argv[i][nestedCascadeOpt.length()] == '=' )
nestedCascadeName.assign( argv[i] + nestedCascadeOpt.length() + 1 );
if( !nestedCascade.load( nestedCascadeName ) )
cerr << "WARNING: Could not load classifier cascade for nested objects" << endl;
}
else if( scaleOpt.compare( 0, scaleOptLen, argv[i], scaleOptLen ) == 0 )
{
if( !sscanf( argv[i] + scaleOpt.length(), "%lf", &scale ) || scale < 1 )
scale = 1;
}
else if( argv[i][0] == '-' )
{
cerr << "WARNING: Unknown option %s" << argv[i] << endl;
}
else
inputName.assign( argv[i] );
}

if( !cascade.load( cascadeName ) )
{
cerr << "ERROR: Could not load classifier cascade" << endl;
cerr << "Usage: facedetect [--cascade=""]n"
" [--nested-cascade[="nested_cascade_path"]]n"
" [--scale[=n"
" [filename|camera_index]n" ;
return -1;
}

//这部分是当我们直接在samples/c路径下打开执行文件时inputName为空,默认的参数
//默认从摄像头获取图像

//cvCaptureFromCAM函数是用来从摄像头内获取图像返回一个CvCapture类型数据,参数是int型你的摄像头id,用-1就ok了
if( inputName.empty() || (isdigit(inputName.c_str()[0]) && inputName.c_str()[1] == '') )
capture = cvCaptureFromCAM( inputName.empty() ? 0 : inputName.c_str()[0] - '0' );
//从输入的参数中获取图像,image或者是avi
else if( inputName.size() )
{
image = imread( inputName, 1 );
if( image.empty() )
capture = cvCaptureFromAVI( inputName.c_str() );
}
else
image = imread( "lena.jpg", 1 );

cvNamedWindow( "result", 1 );

//检测部分,检测从摄像头获取的图像
if( capture )
{
for(;;)
{
//从摄像头获取的图像中获取一帧
IplImage* iplImg = cvQueryFrame( capture );
frame = iplImg;
if( frame.empty() )
break;
if( iplImg->origin == IPL_ORIGIN_TL )
frame.copyTo( frameCopy );
else
flip( frame, frameCopy, 0 );

detectAndDraw( frameCopy, cascade, nestedCascade, scale );

if( waitKey( 10 ) >= 0 )
goto _cleanup_;
}

waitKey(0);
_cleanup_:
cvReleaseCapture( &capture );
}
else
{
//检测图片中的图像,当参数输入时图片时用到这部分
if( !image.empty() )
{
detectAndDraw( image, cascade, nestedCascade, scale );
waitKey(0);
}
else if( !inputName.empty() )
{

FILE* f = fopen( inputName.c_str(), "rt" );
if( f )
{
char buf[1000+1];
while( fgets( buf, 1000, f ) )
{
int len = (int)strlen(buf), c;
while( len > 0 && isspace(buf[len-1]) )
len--;
buf[len] = '';
cout << "file " << buf << endl;
image = imread( buf, 1 );
if( !image.empty() )
{
detectAndDraw( image, cascade, nestedCascade, scale );
c = waitKey(0);
if( c == 27 || c == 'q' || c == 'Q' )
break;
}
}
fclose(f);
}
}
}

cvDestroyWindow("result");

return 0;
}

//检测的核心部分
void detectAndDraw( Mat& img,
CascadeClassifier& cascade, CascadeClassifier& nestedCascade,
double scale)
{
int i = 0;
double t = 0;//用来记录检测出人脸所用的时间
vector faces;//存储检测到的人脸所在矩形区域
const static Scalar colors[] = { CV_RGB(0,0,255),
CV_RGB(0,128,255),
CV_RGB(0,255,255),
CV_RGB(0,255,0),
CV_RGB(255,128,0),
CV_RGB(255,255,0),
CV_RGB(255,0,0),
CV_RGB(255,0,255)} ;//画圆圈的颜色
//存储图片的矩阵
Mat gray, smallImg( cvRound (img.rows/scale), cvRound(img.cols/scale), CV_8UC1 );

//将输入的图像装换成CV_BGR2GRAY,应该是灰度图
cvtColor( img, gray, CV_BGR2GRAY );
//调整图像大小
resize( gray, smallImg, smallImg.size(), 0, 0, INTER_LINEAR );
equalizeHist( smallImg, smallImg );//直方图均衡化

t = (double)cvGetTickCount();//记录检测所用时间,返回CPU时钟数


cascade.detectMultiScale(
smallImg,//Matrix of type CV 8U containing the image in which to detect objects.
faces,//Vector of rectangles such that each rectangle contains the detected object.
1.1,//Specifies how much the image size is reduced at each image scale.
2,//Speficifes how many neighbors should each candiate rectangle have to retain it.
0
//|CV_HAAR_FIND_BIGGEST_OBJECT
//|CV_HAAR_DO_ROUGH_SEARCH
|CV_HAAR_SCALE_IMAGE,//This parameter is not used for new cascade and have the same meaning
//for old cascade as in function cvHaarDetectObjects.
Size(30, 30) //The minimum possible object size. Objects smaller than that are ignored.
);

t = (double)cvGetTickCount() - t;

printf( "detection time = %g msn", t/((double)cvGetTickFrequency()*1000.) );
//在人脸里面检测人眼
for( vector::const_iterator r = faces.begin(); r != faces.end(); r++, i++ )
{
Mat smallImgROI;
vector nestedObjects;//存储人眼的矩形区域
Point center;
Scalar color = colors[i%8];
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);
circle( img, center, radius, color, 3, 8, 0 );

if( nestedCascade.empty() )
continue;
smallImgROI = smallImg(*r);
//找人眼
nestedCascade.detectMultiScale( smallImgROI, nestedObjects,
1.1, 2, 0
//|CV_HAAR_FIND_BIGGEST_OBJECT
//|CV_HAAR_DO_ROUGH_SEARCH
//|CV_HAAR_DO_CANNY_PRUNING
|CV_HAAR_SCALE_IMAGE
,
Size(30, 30) );
//画人眼
for( vector::const_iterator nr = nestedObjects.begin(); nr != nestedObjects.end(); nr++ )
{
center.x = cvRound((r->x + nr->x + nr->width*0.5)*scale);
center.y = cvRound((r->y + nr->y + nr->height*0.5)*scale);
radius = cvRound((nr->width + nr->height)*0.25*scale);
circle( img, center, radius, color, 3, 8, 0 );
}
}
cv::imshow( "result", img );
}

[@more@]

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/15704366/viewspace-1048329/,如需转载,请注明出处,否则将追究法律责任。

上一篇: GABOR
请登录后发表评论 登录
全部评论

注册时间:2011-03-19

  • 博文量
    17
  • 访问量
    33776