基于摄像头使用Cascade Classifier做人脸检测的方法及例程

来源:互联网 发布:英文搜题软件 编辑:程序博客网 时间:2024/06/05 04:12

参考文档:http://docs.opencv.org/doc/tutorials/objdetect/cascade_classifier/cascade_classifier.html

目标:

使用CascadeClassifier(Opencv中的级联分类器)类在视频流中进行Object(例如,人脸)检测。使用的函数如下:

1) load  加载一个 .xml 分类器文件. 可以是Haar分类器或者LBP分类器

2) detectMultiScale 执行检测功能


代码:

代码下载地址 here .  LBP 版本的人脸检测下载地址 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 ); }



其他参考代码:
/** Global variables */String face_cascade_name = "lbpcascade_frontalface.xml";//导入级联分类器xml文件,并作文件是否存在的判断if( !face_cascade.load( face_cascade_name ) ){    printf("--(!)Error loading\n");   return -1;}//-- 2. Read the video stream      capture.open( 0 );//参数为“0”,表示打开默认的摄像机     if( capture.isOpened() )      {        for(;;)        {           //代码段略        }      }** ----------------- 主要代码 ---------- **   //转换成灰度图   cvtColor( frame, frame_gray, COLOR_BGR2GRAY );   //转换直方图   equalizeHist( frame_gray, frame_gray );   //-- Detect faces   face_cascade.detectMultiScale( frame_gray,                      faces, 6, Size(66, 66), Size(300, 300) );** ----------------- 主要函数结构 ----------- ****CascadeClassifier::detectMultiScale**C++下的原形: void CascadeClassifier::detectMultiScale(const Mat& image, vector<Rect>& objects, doublescaleFactor=1.1, int minNeighbors=3,int flags=0,Size minSize=Size(), Size maxSize=Size())参数:Image – 原图像;Object  – 存放检测结果的目标矩阵;ScaleFactor – 图像衰减比例系数;minNeighbors – 保留最小目标邻近的长方形;flags – 仅仅用于旧版本的cascade,新版本未用到。MinSize  - 最小的目标尺寸,小于这个尺寸将不会检测。MaxSize – 最大的目标尺寸,大于这个尺寸将不会被检测。**VideoCapture::open**C++:有两个原行: bool VideoCapture::open(const string& filename) bool VideoCapture::open(int device)参数:filename - - 打开视频或图片文件的名称,例如video.avi 、img_001.jpgdevice - - 设备名称,如:a camera index。Device=0,表示打开默认摄像设备。**VideoCapture::isOpened**isopened() - - 调用时返回一个已经初始化好的的设备,正确返回1 ,错误返回非零。**VideoCapture::release**release() 关闭并释放设备**Ellipse**C++: 原型1:void ellipse(Mat& img, Point center,Size axes, double angle, double startAngle, double endAngle,const Scalar& color, int thickness=1, int lineType=8, int shift=0)C++: 原型2:void ellipse(Mat& img, const RotatedRect& box, const Scalar& color, int thickness=1, int line-Type=8)Img - - 图像Center – 中心点Axes - - 半径Angle - - 中心角度startAngle - - 起始角度endAngle - - 终点角度color - - 颜色thickness - - 厚度linetype - - 线型



下载地址


1 0
原创粉丝点击