臉部偵測原始碼(OPENCV MAILING LIST)

來源:互聯網
上載者:User

以下原始碼是我在OPENCV MAILING LIST 發布的,記憶體流失問題得到更正:

===
#ifdef _CH_
#define WIN32
#error "The file needs cvaux, which is not wrapped yet. Sorry"
#endif

#ifndef _EiC
#include "cv.h"
#include "cvaux.h"
#include "highgui.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include <float.h>
#include <limits.h>
#include <time.h>
#include <ctype.h>
#endif

#ifdef _EiC
#define WIN32
#endif

#define ORIG_WIN_SIZE  24
static CvMemStorage* storage = 0;
static CvHidHaarClassifierCascade* hid_cascade = 0;

#define WINNAME  "Result"

void detect_and_draw( IplImage* image, IplImage* TempImage );

int main( int argc, char** argv )
{
    CvCapture* capture = 0;

    CvHaarClassifierCascade* cascade =
    cvLoadHaarClassifierCascade( "<default_face_cascade>",
                         cvSize( ORIG_WIN_SIZE, ORIG_WIN_SIZE ));
    hid_cascade = cvCreateHidHaarClassifierCascade( cascade, 0, 0, 0, 1 );
    cvReleaseHaarClassifierCascade( &cascade );

    cvNamedWindow( WINNAME, 1 );
    storage = cvCreateMemStorage(0);
   
    if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])))
        capture = cvCaptureFromCAM( argc == 2 ? argv[1][0] - '0' : 0 );
    else if( argc == 2 )
        capture = cvCaptureFromAVI( argv[1] );

    if( capture )
    {
        IplImage *frame, *temp;
        cvGrabFrame( capture );
        frame = cvRetrieveFrame( capture );
       
        temp = cvCreateImage( cvSize(frame->width/2,frame->height/2), 8, 3 );

        for(;;)
        {
            if( !cvGrabFrame( capture ))
                break;
            frame = cvRetrieveFrame( capture );
            if( !frame )
                break;

            detect_and_draw( frame, temp );

            if( cvWaitKey( 10 ) >= 0 )
            {
                //cvReleaseImage( &frame );
                //cvReleaseImage( &temp );
                cvReleaseCapture( &capture );
                cvDestroyWindow(WINNAME);
                return  0;
            }
        }
    }
    else 
    {
        char* filename = argc == 2 ? argv[1] : (char*)"lena.jpg";
        IplImage* image = cvLoadImage( filename, 1 );
        IplImage* temp = cvCreateImage( cvSize(image->width/2,image->height/2), 8, 3 );

        if( image )
        {
            cvFlip( image, image, 0 );
            image->origin = IPL_ORIGIN_BL;
            detect_and_draw( image, temp );
            cvWaitKey(0);
            cvReleaseImage( &image );
            cvReleaseImage( &temp );
        }
        cvDestroyWindow(WINNAME);
        return 0;
    }
    return 0;
}

void detect_and_draw( IplImage* img, IplImage* temp )
{
    int scale = 2;
    CvPoint pt1, pt2;
    int i;

    cvPyrDown( img, temp, CV_GAUSSIAN_5x5 );
#ifdef WIN32
    cvFlip( temp, temp, 0 );
#endif   
    cvClearMemStorage( storage );

    if( hid_cascade )
    {
        CvSeq* faces = cvHaarDetectObjects( temp, hid_cascade, storage,
                                            1.2, 2, CV_HAAR_DO_CANNY_PRUNING );
        for( i = 0; i < (faces ? faces->total : 0); i++ )
        {
            CvRect* r = (CvRect*)cvGetSeqElem( faces, i, 0 );
            pt1.x = r->x*scale;
            pt2.x = (r->x+r->width)*scale;
#ifdef WIN32           
            pt1.y = img->height - r->y*scale;
            pt2.y = img->height - (r->y+r->height)*scale;
#else
            pt1.y = r->y*scale;
            pt2.y = (r->y+r->height)*scale;
#endif           
            cvRectangle( img, pt1, pt2, CV_RGB(255,255,0), 3 );
        }
    }

    cvShowImage(WINNAME, img );
    //cvReleaseImage( &temp );
}

#ifdef _EiC
main(1,"facedetect.c");
#endif

-----------------
HUNNISH:
敕勒川,陰山下,天似穹廬,籠罩四野
天蒼蒼,野茫茫,風吹草低見牛羊

下面是演算法的簡單描述:

Rapid Object Detection using a Boosted Cascade of Simple Features

This method entails a machine learning approach for visual object detection, which is capable of processing images extremely rapidly and achieving high detection rates.

First it introduces a new image representation called “ Integral Image” which allows the features used by detector to be computed very quickly. The integral image can be computed from an image using a few operations per pixel. Once computed, any one of these Harr-like features can be computed at any scale or location in constant time.

The second is a learning algorithm, based on AdaBoost, which selects a small number of critical visual features from a large set and yields extremely efficient classifiers.

The third is a method for combining increasingly more complex classifiers in a “cascade” which allows background regions of the image to be quickly discarded while spending more computation on promising object-like regions.

The object detection classifies images based on the value of simple features. The simple features used are reminiscent of Haar basis functions. Here it uses three kinds of features: two-rectangle feature, three-rectangle feature and four-rectangle. Rectangle features are somewhat primitive when compared with alternatives such as steerable filters. Steerable filters are excellent for the detailed analysis of boundaries, image compression, and texture analysis. In order to use a small number of features to form an effective classifier, the weak learning algorithm is designed to select the single rectangle feature which best separates the positive and negative examples. For each feature, the weak learner determines the optimal threshold classification function, such that the minimum
number of examples is misclassified. The overall form of the detection process is that of a degenerate decision tree, called a “cascade”.

A positive result from the first classifier triggers the evaluation of a second classifier, which has also been adjusted to achieve very high detection rates. A positive result from the second classifier triggers a third classifiers, and so on. The cascade training process involves two types of tradeoffs. In most cases classifiers with more features will achieve higher detection rates and lower false positive rates. At the same time classifiers with more features require more time to compute.

-----------------
HUNNISH:
敕勒川,陰山下,天似穹廬,籠罩四野
天蒼蒼,野茫茫,風吹草低見牛羊 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.