OpenCV中的级联分类器Cascade Classifier(面部识别)



Goal

In this tutorial you will learn how to:

  • Use the CascadeClassifier class to detect objects in a video stream. Particularly, we will use the functions:
    • load to load a .xml classifier file. It can be either a Haar or a LBP classifer
    • detectMultiScale to perform the detection.

Code

This tutorial code’s is shown lines below. You can also download it from here . The second version (using LBP for face detection) can be found here

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

 #include <iostream>
 #include <stdio.h>

 using namespace std;
 using namespace cv;

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

 /** Global variables */
 String face_cascade_name = "haarcascade_frontalface_alt.xml";
 String eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";
 CascadeClassifier face_cascade;
 CascadeClassifier eyes_cascade;
 string window_name = "Capture - Face detection";
 RNG rng(12345);

 /** @function main */
 int main( int argc, const char** argv )
 {
   CvCapture* capture;
   Mat frame;

   //-- 1. Load the cascades
   if( !face_cascade.load( face_cascade_name ) ){ printf("--(!)Error loading\n"); return -1; };
   if( !eyes_cascade.load( eyes_cascade_name ) ){ printf("--(!)Error loading\n"); return -1; };

   //-- 2. Read the video stream
   capture = cvCaptureFromCAM( -1 );
   if( capture )
   {
     while( true )
     {
   frame = cvQueryFrame( capture );

   //-- 3. Apply the classifier to the frame
       if( !frame.empty() )
       { detectAndDisplay( frame ); }
       else
       { printf(" --(!) No captured frame -- Break!"); break; }

       int c = waitKey(10);
       if( (char)c == 'c' ) { break; }
      }
   }
   return 0;
 }

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

  cvtColor( frame, frame_gray, CV_BGR2GRAY );
  equalizeHist( frame_gray, frame_gray );

  //-- Detect faces
  face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );

  for( size_t i = 0; i < faces.size(); i++ )
  {
    Point center( faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5 );
    ellipse( frame, center, Size( faces[i].width*0.5, faces[i].height*0.5), 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 |CV_HAAR_SCALE_IMAGE, Size(30, 30) );

    for( size_t j = 0; j < eyes.size(); j++ )
     {
       Point center( faces[i].x + eyes[j].x + eyes[j].width*0.5, faces[i].y + eyes[j].y + eyes[j].height*0.5 );
       int radius = cvRound( (eyes[j].width + eyes[j].height)*0.25 );
       circle( frame, center, radius, Scalar( 255, 0, 0 ), 4, 8, 0 );
     }
  }
  //-- Show what you got
  imshow( window_name, frame );
 }
第 1 段(可获 2 积分)

Result

  1. Here is the result of running the code above and using as input the video stream of a build-in webcam:

    ../../../../_images/Cascade_Classifier_Tutorial_Result_Haar.jpg

    Remember to copy the files haarcascade_frontalface_alt.xml and haarcascade_eye_tree_eyeglasses.xml in your current directory. They are located in opencv/data/haarcascades

  2. This is the result of using the file lbpcascade_frontalface.xml (LBP trained) for the face detection. For the eyes we keep using the file used in the tutorial.

    ../../../../_images/Cascade_Classifier_Tutorial_Result_LBP.jpg

OpenCV是一个开源的计算机视觉库,它提了许多用于图像和视频处理的功能。其之一就是级联分类器Cascade Classifier),它是一种基于机器学习的目标检测算法。 级联分类器是通过训练得到的,可以用于检测特定对象或特征。在OpenCV,最常见的应用就是人脸检测。级联分类器通过使用Haar特征和AdaBoost算法来训练,可以在图像快速准确地检测出人脸。 在Python使用OpenCV级联分类器,首先需要加载已经训练好的分类器模型。OpenCV提供了一些预训练好的模型,可以直接使用。然后,将待检测的图像传入分类器进行检测,如果检测到目标对象,则返回目标的位置信息。 下面是使用OpenCV Python级联分类器的基本步骤: 1. 导入OpenCV库:`import cv2` 2. 加载分类器模型:`face_cascade = cv2.CascadeClassifier('path/to/haarcascade_frontalface_default.xml')` 3. 读取待检测的图像:`img = cv2.imread('path/to/image.jpg')` 4. 将图像转换为灰度图像:`gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)` 5. 使用级联分类器进行检测:`faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))` 6. 遍历检测到的目标,绘制矩形框标记:`for (x, y, w, h) in faces: cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)` 7. 显示检测结果:`cv2.imshow('Detected Faces', img)` 8. 等待按键退出:`cv2.waitKey(0)` 以上是一个简单的人脸检测示例,你可以根据需要调整参数和模型路径。除了人脸检测,级联分类器还可以用于其他目标的检测,如眼睛、车辆等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值