我试图检测只落在面部区域内的眼睛,因此我对代码做了一些小的改动:
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 );
CvMat small_img_roi;
CvSeq* nested_objects;
CvPoint center;
CvPoint center_1;
CvScalar color = colors[i%8];
int radius,radius_1;
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, color, 3, 8, 0 );
if( !nested_cascade )
continue;
else
printf("not continuing!\n");
cvGetSubRect( small_img, &small_img_roi, *r );
nested_objects = cvHaarDetectObjects( &small_img_roi, nested_cascade, storage,
1.1, 2, 0
|CV_HAAR_DO_CANNY_PRUNING, ,
cvSize(0, 0) );
for( j = 0; j < (nested_objects ? nested_objects->total : 0); j++ )
{
printf("start of nested objects loop!\n");
CvRect* nr = (CvRect*)cvGetSeqElem( nested_objects, j );
center_1.x = cvRound((r->x + nr->x + nr->width*0.5)*scale);
center_1.y = cvRound((r->y + nr->y + nr->height*0.5)*scale);
radius_1 = cvRound((nr->width + nr->height)*0.25*scale);
if(center_1.x+radius_1<center.x+radius&& center.x-radius<center_1.x-radius_1 && center_1.y<center.y+radius&& center.y<center_1.y+radius )
{
cvCircle( img, center_1, radius_1, color, 3, 8, 0 );
}
else
printf("cant find thy eyes!\n");
}
然而,它并不是很成功。在尝试调试它时,我尝试注释掉他们为脸部画一个圆圈的部分,这导致根本没有画出任何圆圈。这使我得出结论,可能嵌套对象的部分不起作用。因此,我在代码中实现了几个 printfs 并监控了控制台。但是在观察了控制台之后,我得出的结论是部分嵌套对象确实不起作用。但是,我仍然不知道为什么会这样,因为嵌套对象部分类似于人脸检测部分。因此,如果人脸检测部分有效,嵌套的目标代码不也应该有效吗?
(>_<)