调用Open CV自带人脸检测

来源:互联网 发布:insanity 瘦身 知乎 编辑:程序博客网 时间:2024/06/09 15:02
#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 = "C:\\opencv\\sources\\data\\lbpcascades\\lbpcascade_frontalface.xml";String eyes_cascade_name = "C:\\opencv\\sources\\data\\haarcascades\\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 cascadesif( !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 streamcapture = cvCaptureFromCAM( 0 );if( capture ){while( true ){frame = cvQueryFrame( capture );//-- 3. Apply the classifier to the frameif( !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 facesface_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 eyeseyes_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 gotimshow( window_name, frame );}

以上代码来自官方文档:http://docs.opencv.org/2.4.11/doc/tutorials/objdetect/cascade_classifier/cascade_classifier.html#cascade-classifier

#include <opencv2\opencv.hpp>#include <iostream>#include <string>using namespace cv;using namespace std;int main(){    Mat img = imread("a.jpg");    if(img.empty())    {        cout<<"error";        return -1;    }    imshow("The original image",img);    vector<Rect> found, found_filtered;    HOGDescriptor people_dectect_hog;      //hog描述子在opencv中为HOGDescriptor    //采用默认的已经训练好了的svm系数作为此次检测的模型//可以调用该描述子setSVMDetector方法给用于对hog特征进行分类的svm模型的系数赋值,//这里的参数为HOGDescriptor::getDefaultPeopleDetector()时表示采用系统默认的参数,因为这些参数是用很多图片训练而来的。    people_dectect_hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());    //对输入的图片img进行多尺度行人检测//该函数表示对输入的图片img进行多尺度行人检测 img为输入待检测的图片;found_locations为检测到目标区域列表;参数3为程序内部计算为行人目标的阈值,//也就是检测到的特征到SVM分类超平面的距离;参数4为滑动窗口每次移动的距离。它必须是块移动的整数倍;参数5为图像扩充的大小;参数6为比例系数,//即滑动窗口每次增加的比例;参数7为组阈值,即校正系数,当一个目标被多个窗口检测出来时,该参数此时就起了调节作用,为0时表示不起调节作用。*/    people_dectect_hog.detectMultiScale(img,           //img为输入待检测的图片;                               found,          //found为检测到目标区域列表;   0,              //参数3为程序内部计算为行人目标的阈值,也就是检测到的特征到SVM分类超平面的距离;   Size(8, 8),     //参数4为滑动窗口每次移动的距离。它必须是块移动的整数倍;   Size(32, 32),   //参数5为图像扩充的大小;   1.05,           //参数6为比例系数,即测试图片每次尺寸缩放增加的比例;   2);             //参数7为组阈值,即校正系数,当一个目标被多个窗口检测出来时,该参数此时就起了调节作用,为0时表示不起调节作用。    //从源码中可以看出:    //#define __SIZE_TYPE__ long unsigned int    //typedef __SIZE_TYPE__ size_t;    //因此,size_t是一个long unsigned int类型    size_t i, j;    for (i = 0; i < found.size(); i++ )        {            Rect r = found[i];            //下面的这个for语句是找出所有没有嵌套的矩形框r,并放入found_filtered中,如果有嵌套的           //话,则取外面最大的那个矩形框放入found_filtered中            for(j = 0; j <found.size(); j++)                if(j != i && (r&found[j])==r)                    break;               if(j == found.size())               found_filtered.push_back(r);        }    //在图片img上画出矩形框,因为hog检测出的矩形框比实际人体框要稍微大些,所以这里需要    //做一些调整    for(i = 0; i <found_filtered.size(); i++)        {            Rect r = found_filtered[i];            r.x += cvRound(r.width*0.1);            r.width = cvRound(r.width*0.8);            r.y += cvRound(r.height*0.07);            r.height = cvRound(r.height*0.8);//绘制矩形            rectangle(img,                //图像.          r.tl(),             //矩形的一个顶点。  r.br(),             //矩形对角线上的另一个顶点  Scalar(0, 255, 0),  //线条颜色 (RGB) 或亮度(灰度图像 )(grayscale image)  3);                 //组成矩形的线条的粗细程度。取负值时(如 CV_FILLED)函数绘制填充了色彩的矩形        }   imshow("After processing the figure",img);    waitKey();    return 0;}


0 0
原创粉丝点击