Cascade Classifier

Cascade Classifier

一.基础概念

1.Haar和LBP特征

参考博客:

https://blog.csdn.net/liudongdong19/article/details/81008160

2.主要函数

  • § CascadeClassifier() [1/2]

    cv::CascadeClassifier::CascadeClassifier ( )
    Python:
    = cv.CascadeClassifier( )
    = cv.CascadeClassifier( filename )

    § CascadeClassifier() [2/2]

    cv::CascadeClassifier::CascadeClassifier ( const String & filename )
    Python:
    = cv.CascadeClassifier( )
    = cv.CascadeClassifier( filename )

    Loads a classifier from a file.

    • Parameters

      filenameName of the file from which the classifier is loaded.

  • § load()

    bool cv::CascadeClassifier::load ( const String & filename )
    Python:
    retval = cv.CascadeClassifier.load( filename )
  • § detectMultiScale() [1/3]

    void cv::CascadeClassifier::detectMultiScale ( InputArray image,
    std::vector< Rect > & objects,
    double scaleFactor = 1.1,
    int minNeighbors = 3,
    int flags = 0,
    Size minSize = Size(),
    Size maxSize = Size()
    )

    image 输入图像

    objects 检测出的物体的矩形轮廓

    scaleFactor 这个是每次缩小图像的比例,默认是1.1

    minNeighbors 匹配成功所需要的周围矩形框的数目,每一个特征匹配到的区域都是一个矩形框,只有多个矩形框同时存在的时候,才认为是匹配成功,比如人脸,这个默认值是3。

    flags

    可以取如下这些值:
    CASCADE_DO_CANNY_PRUNING=1, 利用canny边缘检测来排除一些边缘很少或者很多的图像区域
    CASCADE_SCALE_IMAGE=2, 正常比例检测
    CASCADE_FIND_BIGGEST_OBJECT=4, 只检测最大的物体

    minObjectSize maxObjectSize:匹配物体的大小范围

    § detectMultiScale() [2/3]

    void cv::CascadeClassifier::detectMultiScale ( InputArray image,
    std::vector< Rect > & objects,
    std::vector< int > & numDetections,
    double scaleFactor = 1.1,
    int minNeighbors = 3,
    int flags = 0,
    Size minSize = Size(),
    Size maxSize = Size()
    )

    § detectMultiScale() [3/3]

    void cv::CascadeClassifier::detectMultiScale ( InputArray image,
    std::vector< Rect > & objects,
    std::vector< int > & rejectLevels,
    std::vector< double > & levelWeights,
    double scaleFactor = 1.1,
    int minNeighbors = 3,
    int flags = 0,
    Size minSize = Size(),
    Size maxSize = Size(),
    bool outputRejectLevels = false
    )

二.代码实现

#include "opencv2/objdetect.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"

#include 

using namespace std;
using namespace cv;

/** Function Headers */
void detectAndDisplay( Mat frame );

/** Global variables */
String face_cascade_name, eyes_cascade_name;
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;
String window_name = "Capture - Face detection";

/** @function main */
int main( int argc, const char** argv )
{
    CommandLineParser parser(argc, argv,
        "{help h||}"
        "{face_cascade|../../data/haarcascades/haarcascade_frontalface_alt.xml|}"
        "{eyes_cascade|../../data/haarcascades/haarcascade_eye_tree_eyeglasses.xml|}");

    parser.about( "\nThis program demonstrates using the cv::CascadeClassifier class to detect objects (Face + eyes) in a video stream.\n"
                  "You can use Haar or LBP features.\n\n" );
    parser.printMessage();

    face_cascade_name = parser.get<String>("face_cascade");
    eyes_cascade_name = parser.get<String>("eyes_cascade");
    VideoCapture capture;
    Mat frame;

    //-- 1. Load the cascades //加载级联分类器文件
    if( !face_cascade.load( face_cascade_name ) ){ printf("--(!)Error loading face cascade\n"); return -1; };
    if( !eyes_cascade.load( eyes_cascade_name ) ){ printf("--(!)Error loading eyes cascade\n"); return -1; };

    //-- 2. Read the video stream //读取视频流
    capture.open( 0 );
    if ( ! capture.isOpened() ) { printf("--(!)Error opening video capture\n"); return -1; }

    while ( capture.read(frame) )
    {
        if( frame.empty() )
        {
            printf(" --(!) No captured frame -- Break!");
            break;
        }

        //-- 3. Apply the classifier to the frame//用级联分类器来检测目标图片
        detectAndDisplay( frame );

        if( waitKey(10) == 27 ) { break; } // escape
    }
    return 0;
}

/** @function detectAndDisplay */
void detectAndDisplay( Mat frame )
{
    std::vector<Rect> faces;
    Mat frame_gray;

    cvtColor( frame, frame_gray, COLOR_BGR2GRAY );//颜色空间转换,由于haar和LBP均是对灰度进行处理,所以必须事先转换成灰度
    equalizeHist( frame_gray, frame_gray );//直方图均衡化

    //-- Detect faces //检测脸
    face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CASCADE_SCALE_IMAGE, Size(60, 60) );//在目标图像中检测出脸的矩形轮廓

    for ( size_t i = 0; i < faces.size(); i++ )
    {
        Point center( faces[i].x + faces[i].width/2, faces[i].y + faces[i].height/2 );
        ellipse( frame, center, Size( faces[i].width/2, faces[i].height/2 ), 0, 0, 360, Scalar( 255, 0, 255 ), 4, 8, 0 );//画出包围脸部的椭圆

        Mat faceROI = frame_gray( faces[i] );//确定脸部所在的矩形区域为感兴趣区域,然后进行后续的眼睛检测
        std::vector<Rect> eyes;//矩形向量

        //-- In each face, detect eyes
        eyes_cascade.detectMultiScale( faceROI, eyes, 1.1, 2, 0 |CASCADE_SCALE_IMAGE, Size(50, 50) );//检测眼部

        for ( size_t j = 0; j < eyes.size(); j++ )
        {
            Point eye_center( faces[i].x + eyes[j].x + eyes[j].width/2, faces[i].y + eyes[j].y + eyes[j].height/2 );
            int radius = cvRound( (eyes[j].width + eyes[j].height)*0.25 );
            circle( frame, eye_center, radius, Scalar( 255, 0, 0 ), 4, 8, 0 );//画出眼部所在的圆圈
        }
    }
    //-- Show what you got
    imshow( window_name, frame );
}

![](/home/mazh/Pictures/Screenshot from 2019-05-28 16-53-58.png)

你可能感兴趣的:(Cascade,Classifier,Object,detect)