OpenCV Tutorial 11 - Chapter 13

来源:互联网 发布:电脑按java干什么 编辑:程序博客网 时间:2024/05/08 08:08

( 由于收到附件大小的限制,此篇的图片无法上传,请大家见谅,可登陆原网站查看:http://dasl.mem.drexel.edu/~noahKuntz/openCVTut11.html)

Author: Noah Kuntz (2009)
Contact: nk752@drexel.edu

Keywords: OpenCV, computer vision, machine learning, haar classifier, face detection

My Vision Tutorials Index

This tutorial assumes the reader:
(1) Has a basic knowledge of Visual C++
(2) Has some familiarity with computer vision concepts
(3) Has read the previous tutorials in this series

The rest of the tutorial is presented as follows:

  • Step 1: Face Detection with the Haar Classifier
  • Final Words

Important Note!

More information on the topics of these tutorials can be found in this book:Learning OpenCV: Computer Vision with the OpenCV Library

Step 1: Face Detection with the Haar Classifier(人脸检测和Haar分类器)


Face Detection(人脸检测)


Machine learning is a powerful set of algorithms that can be used to improve computer vision algorithms by allowing the program to build on the knowledge of many example cases(机器学习是一套功能强大的可以用来提高计算机视觉算法的算法,该算法允许该程序在许多例子案件知识的基础上建立). For face detection, this example uses a Haar classifier, which is a statistically boosted classifier supported by training on many sample images of faces(对于人脸检测,这个例子使用了haar分类器,这是一个统计上提高了的分类器,它支持许多人脸图像样本). Much more detail of machine learning theory can be found in the book(更详细的机器学习理论可以在这本书中找到). This code uses a CvHaarClassifierCascade* matrix that should be copied from your OpenCV directory to your working directory (see the code comments), and loaded withcvLoad(此代码使用一个CvHaarClassifierCascade *矩阵,应该从你的OpenCV的目录复制到你的工作目录(见代码注释),并用cvLoad加载). ThencvHaarDetectObjects takes the input image, the loaded cascade, the empty memory storage and a few options and performs the face detection(然后cvHaarDetectObjects接受输入图像,加载的级联,空的记忆存储和一些选项并执行人脸检测). The important option here is the minimum size defined incvSize(这里最重要的选项是用cvSize定义的最小规模).Base this on the size of the faces in your image, in pixels(在你的图像中,立足于脸的大小,以像素为单位). Then once the detected faces are stored in a sequence, a loop can be used to go through them and draw aCvRect object around each one(那么一旦被检测的脸被存储在一个序列中,一个循环可用于通过他们和绘制一个CvRect对象包括周围的每人一个). Here is the code(以下是代码):


 

 

int _tmain(int argc, _TCHAR* argv[])

{

        IplImage* img;

        img = cvLoadImage( "dasl_hubo.jpg" );

        CvMemStorage* storage = cvCreateMemStorage(0);

        // Note that you must copy C:\Program Files\OpenCV\data\haarcascades\haarcascade_frontalface_alt2.xml

        // to your working directory

        CvHaarClassifierCascade* cascade = (CvHaarClassifierCascade*)cvLoad( "haarcascade_frontalface_alt2.xml" );

        double scale = 1.3;

 

        static CvScalar colors[] = { {{0,0,255}}, {{0,128,255}}, {{0,255,255}},

        {{0,255,0}}, {{255,128,0}}, {{255,255,0}}, {{255,0,0}}, {{255,0,255}} };

 

        // Detect objects

        cvClearMemStorage( storage );

        CvSeq* objects = cvHaarDetectObjects( img, cascade, storage, 1.1, 4, 0, cvSize( 40, 50 ));

 

        CvRect* r;

        // Loop through objects and draw boxes

        for( int i = 0; i < (objects ? objects->total : 0 ); i++ ){

               r = ( CvRect* )cvGetSeqElem( objects, i );

               cvRectangle( img, cvPoint( r->x, r->y ), cvPoint( r->x + r->width, r->y + r->height ),

                       colors[i%8]);

        }

 

        cvNamedWindow( "Output" );

        cvShowImage( "Output", img );

        cvWaitKey();

 

        cvReleaseImage( &img );

 

        return 0;

}

 


 

Final Words(结束语)

This tutorial's objective was to show how to use a basic machine learning classifier to detect faces(本教程的目标是展示如何使用基本的机器学习分类器来检测人脸).

Click here to email me.
Click here to return to my Tutorials page.