日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

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

發(fā)布時(shí)間:2025/7/25 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 基于摄像头使用Cascade Classifier做人脸检测的方法及例程 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

參考文檔:http://docs.opencv.org/doc/tutorials/objdetect/cascade_classifier/cascade_classifier.html

目標(biāo):

使用CascadeClassifier(Opencv中的級(jí)聯(lián)分類器)類在視頻流中進(jìn)行Object(例如,人臉)檢測(cè)。使用的函數(shù)如下:

1) load  加載一個(gè) .xml 分類器文件. 可以是Haar分類器或者LBP分類器

2) detectMultiScale 執(zhí)行檢測(cè)功能


代碼:

代碼下載地址 here .? LBP 版本的人臉檢測(cè)下載地址 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 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( -1 );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 );}


其他參考代碼:
/** Global variables */ String face_cascade_name = "lbpcascade_frontalface.xml";//導(dǎo)入級(jí)聯(lián)分類器xml文件,并作文件是否存在的判斷 if( !face_cascade.load( face_cascade_name ) ) { printf("--(!)Error loading\n");return -1; }//-- 2. Read the video streamcapture.open( 0 );//參數(shù)為“0”,表示打開默認(rèn)的攝像機(jī)if( capture.isOpened() ){for(;;){//代碼段略}}** ----------------- 主要代碼 ---------- **//轉(zhuǎn)換成灰度圖cvtColor( frame, frame_gray, COLOR_BGR2GRAY );//轉(zhuǎn)換直方圖equalizeHist( frame_gray, frame_gray );//-- Detect facesface_cascade.detectMultiScale( frame_gray, faces,6,Size(66, 66),Size(300, 300) );** ----------------- 主要函數(shù)結(jié)構(gòu) ----------- ** **CascadeClassifier::detectMultiScale** C++下的原形: void CascadeClassifier::detectMultiScale( const Mat& image, vector<Rect>& objects, double scaleFactor=1.1, int minNeighbors=3, int flags=0, Size minSize=Size(), Size maxSize=Size())參數(shù): Image – 原圖像; Object – 存放檢測(cè)結(jié)果的目標(biāo)矩陣; ScaleFactor – 圖像衰減比例系數(shù); minNeighbors – 保留最小目標(biāo)鄰近的長(zhǎng)方形; flags – 僅僅用于舊版本的cascade,新版本未用到。 MinSize - 最小的目標(biāo)尺寸,小于這個(gè)尺寸將不會(huì)檢測(cè)。 MaxSize – 最大的目標(biāo)尺寸,大于這個(gè)尺寸將不會(huì)被檢測(cè)。**VideoCapture::open** C++:有兩個(gè)原行:bool VideoCapture::open(const string& filename)bool VideoCapture::open(int device) 參數(shù): filename - - 打開視頻或圖片文件的名稱,例如video.avi 、img_001.jpg device - - 設(shè)備名稱,如:a camera index。Device=0,表示打開默認(rèn)攝像設(shè)備。**VideoCapture::isOpened** isopened() - - 調(diào)用時(shí)返回一個(gè)已經(jīng)初始化好的的設(shè)備,正確返回1 ,錯(cuò)誤返回非零。 **VideoCapture::release** release() 關(guān)閉并釋放設(shè)備**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 – 中心點(diǎn) Axes - - 半徑 Angle - - 中心角度 startAngle - - 起始角度 endAngle - - 終點(diǎn)角度 color - - 顏色 thickness - - 厚度 linetype - - 線型


下載地址


總結(jié)

以上是生活随笔為你收集整理的基于摄像头使用Cascade Classifier做人脸检测的方法及例程的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。