OpenCV Tutorial 11 - Chapter 13

( 由於收到附件大小的限制,此篇的圖片無法上傳,請大家見諒,可登陸原網站查看:http://dasl.mem.drexel.edu/~noahKuntz/openCVTut11.html)

Author: Noah Kuntz (2009)
Contact: [email protected]

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:

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.

 

發佈了30 篇原創文章 · 獲贊 3 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章