#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>
#pragma comment(lib, "opencv_objdetect231d.lib")
CvHaarClassifierCascade* load_object_detector( const char* cascade_path )
{
return (CvHaarClassifierCascade*)cvLoad( cascade_path );
}
void detect_and_draw_objects( IplImage* image,
CvHaarClassifierCascade* cascade,
int do_pyramids )
{
IplImage* small_image = image;
CvMemStorage* storage = cvCreateMemStorage(0);
CvSeq* faces;
int i, scale = 1;
/* if the flag is specified, down-scale the 输入图像 to get a
performance boost w/o loosing quality (perhaps) */
if( do_pyramids )
{
small_image = cvCreateImage( cvSize(image->width/2,image->height/2), IPL_DEPTH_8U, 3 );
cvPyrDown( image, small_image, CV_GAUSSIAN_5x5 );
scale = 2;
}
//cvNamedWindow( "test0", 0 );
//cvShowImage( "test0", small_image );
//cvWaitKey(0);
/* use the fastest variant */
faces = cvHaarDetectObjects( small_image, cascade, storage, 1.2, 10, CV_HAAR_DO_CANNY_PRUNING );
/* draw all the rectangles */
for( i = 0; i < faces->total; i++ )
{
/* extract the rectanlges only */
CvRect face_rect = *(CvRect*)cvGetSeqElem( faces, i);
cvRectangle( image, cvPoint(face_rect.x*scale,face_rect.y*scale),
cvPoint((face_rect.x+face_rect.width)*scale,
(face_rect.y+face_rect.height)*scale),
CV_RGB(255,0,0), 3 );
}
printf("%d",faces->total);
if( small_image != image ){
cvReleaseImage( &small_image );
}
cvReleaseMemStorage( &storage );
}
/* takes image filename and cascade path from the command line */
int main( int argc, char** argv )
{
IplImage* image;
const char* cascade_path01="E:\\face\\data\\cascade.xml"; //分类器路径
//const char* imgpath="E:\\导出图片\\10480f.bmp";//待检验的图片
const char* imgpath="E:\\导出图片\\111.JPG"; //待检验的图片
//const char* imgpath="E:\\导出图片\\11800f.bmp";//待检验的图片
//const char* imgpath="E:\\face\\negdata\\30f.bmp";
if((image = cvLoadImage( imgpath, 1 )) != 0 )
{
CvHaarClassifierCascade* cascade = load_object_detector(cascade_path01);
detect_and_draw_objects( image, cascade, 1 );
cvNamedWindow( "test", 0 );
cvShowImage( "test", image );
cvWaitKey(0);
cvReleaseHaarClassifierCascade( &cascade );
cvReleaseImage( &image );
}
return 0;
}