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

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:


    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.

时间: 2024-07-30 06:55:41

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

opencv下的人脸检测总是无法加载级联分类器文件

问题描述 opencv下的人脸检测总是无法加载级联分类器文件 代码本身没有错,debug下无法加载级联分类器文件,release下读不到图片,配置应该没有问题 求问,谢谢! #include "opencv2/core/core.hpp" #include "opencv2/objdetect/objdetect.hpp" #include "opencv2/highgui/highgui.hpp" #include "opencv2/

如何用OpenCV训练自己的分类器

最近要做一个性别识别的项目,在人脸检测与五官定位上我采用OPENCV的haartraining进行定位,这里介绍下这两天我学习的如何用opencv训练自己的分类器.在这两天的学习里,我遇到了不少问题,不过我遇到了几个好心的大侠帮我解决了不少问题,特别是无忌,在这里我再次感谢他的帮助. 一.简介 目标检测方法最初由Paul Viola [Viola01]提出,并由Rainer Lienhart [Lienhart02]对这一方法进行了改善.该方法的基本步骤为: 首先,利用样本(大约几百幅样本图片)

在Hibernate中正确实现关联关系中的级联操作(cascading)

关系数据库系统本身就比较复杂,加上Hibernate的O/R映射层,复杂度加重了,很容易出现问题,本人将最近遇到的问题和解决方法做一个总结,整理在下面的一系列文章中 正确理解Hibernate的聚合类型(collection)的使用 在Hibernate中正确实现关联关系中的级联操作(cascading) 在Hibernate框架中编写持久对象类实现外键关联的几点注意事项 本文是第二篇,讲解在one-to-many(一对多)和many-to-one(多对一)关联关系中的cascade特性的声明方

oracle中使用on delete cascade和on delete set null来建立外键

oracle中使用on delete cascade和on delete set null来建立外键 其面我们介绍了创建外键约束时如果使用oracle默认的创建方式,在删除被参照的数据时,将无法被删除,这一点在oracle9i中给了我们更多灵活的选择,我们可是使用on delete cascade和 on delete set null关键字来决定删除被参照数据时是否要将参照这个数据的那些数据一并删除,还是将那些参照这条数据的数据的对应值赋空. 例如下面这两个表中分别存的时员工的基本信息和公司的

opencv中的kmeans函数

问题描述 opencv中的kmeans函数 LBPimage是一个3通道的灰度图像.通过kmeans函数之后,分的两类,labels的各个分量都是0,只有第一个和最后一个元素是1.请问大神们,错在哪里. Mat LBPimage=LBP(imgs); int nl = LBPimage.rows; int nc = LBPimage.cols; Mat points; points.create(nc*nl, 1, CV_32FC1); int n = 0; for (int i = 0; i

OpenCV中读取视频帧像素值的一般方法

OpenCV中读取视频帧像素值的一般方法可以分为以下几个步骤: 1.打开AVI格式的视频 CvCapture *capture=NULL; IplImage *img=NULL; capture = cvCaptureFromFile("E:\\Sequence_mask\\crossroad.avi"); if(!capture) { cout<<endl<<"failed to open mask file"<<endl; r

OpenCV中保存视频的一般方法

在OpenCV中,保存视频一般分为三步骤: 1.初始化工作 //初始化保存视频文件 CvVideoWriter* video=NULL; video=cvCreateVideoWriter("E:\\Sequence_mask\\out.avi",CV_FOURCC('X', 'V', 'I', 'D'), m_inputfps,cvSize(m_inputwidth,m_inputheight),0);//1代表彩色视频,0代表黑白视频,即mask if(video) cout<

c++和opencv-利用opencv中的张正友方法进行摄像机标定

问题描述 利用opencv中的张正友方法进行摄像机标定 利用opencv和c++进行摄像机标定,采用的程序是网上的基于张正友的标定方法,网上说的结果和我的执行结果不同,是采用了不同角度的20张图片,理论上这20张图片的旋转向量和平移向量应该不相同,但是我最后求出来的旋转矩阵和平移向量竟然都是相同的,结果如下,,,,,,不知道是怎么回事儿??请教高手解答啊 第1幅图像的旋转向量: {{-2.65698e+303,-2.65698e+303,-2.65698e+303}} 第1幅图像的旋转矩阵: {

opencv 区域检测-Opencv中cvfindcontours原理

问题描述 Opencv中cvfindcontours原理 Opencv中的cvfindcontours原理是什么啊,有没有什么比较著名的算法在里面,自己看不懂代码,网上也没有介绍~但是想学习一下轮廓检测-- 解决方案 OpenCV人脸识别的原理 .OpenCV人脸识别的原理 .opencv cvFindContours---------------------- 解决方案二: opencv文档中的内容有写: "") The function retrieves contours fro