【原】训练自己的haar-like特征分类器并识别物体(3)

在前两篇文章中,我介绍了《训练自己的haar-like特征分类器并识别物体》的前三个步骤:

1.准备训练样本图片,包括正例及反例样本

2.生成样本描述文件

3.训练样本

4.目标识别

==============

本文将着重说明最后一个阶段——目标识别,也即利用前面训练出来的分类器文件(.xml文件)对图片中的物体进行识别,并在图中框出在该物体。由于逻辑比较简单,这里直接上代码:

int _tmain(int argc, _TCHAR* argv[])
{
	char *cascade_name = CASCADE_HEAD_MY; //上文最终生成的xml文件命名为"CASCADE_HEAD_MY.xml"
	cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 ); //加载xml文件

	if( !cascade ) 
	{ 
		fprintf( stderr, "ERROR: Could not load classifier cascade\n" );
		system("pause");
		return -1;
	}
	storage = cvCreateMemStorage(0); 
	cvNamedWindow( "face", 1 ); 

	const char* filename = "(12).bmp"; 
	IplImage* image = cvLoadImage( filename, 1 );

	if( image ) 
	{ 
		detect_and_draw( image ); //函数见下方
		cvWaitKey(0); 
		cvReleaseImage( &image );   
	}
	cvDestroyWindow("result");
	return 0;
}
 1 void detect_and_draw(IplImage* img ) 
 2 { 
 3     double scale=1.2; 
 4     static CvScalar colors[] = { 
 5         {{0,0,255}},{{0,128,255}},{{0,255,255}},{{0,255,0}}, 
 6         {{255,128,0}},{{255,255,0}},{{255,0,0}},{{255,0,255}} 
 7     };//Just some pretty colors to draw with
 8 
 9     //Image Preparation 
10     // 
11     IplImage* gray = cvCreateImage(cvSize(img->width,img->height),8,1); 
12     IplImage* small_img=cvCreateImage(cvSize(cvRound(img->width/scale),cvRound(img->height/scale)),8,1); 
13     cvCvtColor(img,gray, CV_BGR2GRAY); 
14     cvResize(gray, small_img, CV_INTER_LINEAR);
15 
16     cvEqualizeHist(small_img,small_img); //直方图均衡
17 
18     //Detect objects if any 
19     // 
20     cvClearMemStorage(storage); 
21     double t = (double)cvGetTickCount(); 
22     CvSeq* objects = cvHaarDetectObjects(small_img, 
23         cascade, 
24         storage, 
25         1.1, 
26         2, 
27         0/*CV_HAAR_DO_CANNY_PRUNING*/, 
28         cvSize(30,30));
29 
30     t = (double)cvGetTickCount() - t; 
31     printf( "detection time = %gms\n", t/((double)cvGetTickFrequency()*1000.) );
32 
33     //Loop through found objects and draw boxes around them 
34     for(int i=0;i<(objects? objects->total:0);++i) 
35     { 
36         CvRect* r=(CvRect*)cvGetSeqElem(objects,i); 
37         cvRectangle(img, cvPoint(r->x*scale,r->y*scale), cvPoint((r->x+r->width)*scale,(r->y+r->height)*scale), colors[i%8]); 
38     } 
39     for( int i = 0; i < (objects? objects->total : 0); i++ ) 
40     { 
41         CvRect* r = (CvRect*)cvGetSeqElem( objects, i ); 
42         CvPoint center; 
43         int radius; 
44         center.x = cvRound((r->x + r->width*0.5)*scale); 
45         center.y = cvRound((r->y + r->height*0.5)*scale); 
46         radius = cvRound((r->width + r->height)*0.25*scale); 
47         cvCircle( img, center, radius, colors[i%8], 3, 8, 0 ); 
48     }
49 
50     cvShowImage( "result", img ); 
51     cvReleaseImage(&gray); 
52     cvReleaseImage(&small_img); 
53 }
detect_and_draw

===================================

其实上面的代码可以运用于大部分模式识别问题,无论是自己生成的xml文件还是opencv自带的xml文件。在opencv的工程目录opencv\data文件夹下有大量的xml文件,这些都是opencv开源项目中的程序员们自己训练出来的。然而,效果一般不会合你预期,所以才有了本系列文章。天下没有免费的午餐,想要获得更高的查准率与查全率,不付出点努力是不行的!

 

posted @ 2014-07-21 18:16  编程小翁  阅读(4203)  评论(1编辑  收藏  举报
我是来自厦门的Jilon. 翁,请关注我的微博:真实的weng,或关注微信:Jilon