cvHaarDetectObjects 解析(1)

首先编译一个函数:

void haar_dect_draw (IplImage* img)
{

    IplImage *gray, *small_img;
    int i, j;
    gray = cvCreateImage( cvSize(img->width,img->height), 8, 1 );
    small_img = cvCreateImage( cvSize( cvRound (img->width/1),
                         cvRound (img->height/1)), 8, 1 );
    //gray=img;
    cvCvtColor( img, gray, CV_BGR2GRAY );//灰度化

    cvResize( gray, small_img, CV_INTER_LINEAR );//大小重置
    cvEqualizeHist( small_img, small_img );//直方图均衡化
    cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
    
    CvMemStorage* storage1=0;
    storage1=cvCreateMemStorage(0);
    CvHaarClassifierCascade *cascade_f=0;
    const char *cascade_name1="data.xml";//制定XML路径及名称
    cascade_f=(CvHaarClassifierCascade*)cvLoad(cascade_name1,0,0,0);//读取XML文件
    assert(cascade_f!=NULL);
    CvSeq *faces=cvHaarDetectObjects(small_img,cascade_f,storage1,1.1,2,0,cvSize(24,24));//HaarDetect
      for(int ii=0;ii<(faces?faces->total:0);ii++)//判断几个目标区域,画几个框框
    {
          CvRect *face = (CvRect*)cvGetSeqElem(faces, ii);
          cvRectangle(img,cvPoint(face->x, face->y),cvPoint(face->x + face->width,face->y + face->height)
                      ,CV_RGB(255, 0, 0),1, 8, 0);//画框框
    }
    
        cvShowImage( "Example1", img );//显示图像
	cvReleaseImage(&img);
	cvReleaseImage(&gray);
	cvReleaseImage( &small_img);
       // cvDestroyWindow("Example1");

}


你可能感兴趣的:(cvHaarDetectObjects 解析(1))