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 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 );}
Result
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
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.
OpenCV中的级联分类器Cascade Classifier(面部识别)相关推荐
- OpenCV级联分类器Cascade Classifier
OpenCV级联分类器Cascade Classifier 级联分类器Cascade Classifier 目标 理论 OpenCV中的Haar级联检测 结果 级联分类器Cascade Classif ...
- OpenCV56:级联分类器|Cascade Classifier
目标 在本教程中, 将学习 Haar级联对象检测的工作原理 将使用基于Haar Feature的Cascade分类器了解人脸检测和眼睛检测的基础知识 将使用cv::CascadeClassifier类 ...
- Python+OpenCV:训练级联分类器(Cascade Classifier Training)
Python+OpenCV:训练级联分类器(Cascade Classifier Training) Introduction Working with a boosted cascade of we ...
- 【机器学习】传统目标检测算法之级联分类器Cascade
先附上参考文章吧. 文章其实是"P. Viola, M. Jones. Rapid Object Detection using a Boosted Cascade of Simple Fe ...
- OpenCV中使用SVM分类器
在opencv中支持SVM分类器,过程就是:先训练再预测(python实现) # svm 对于数据的要求: 所有的数据都要有label # [155,48] -- 0 女生 [152,53] ---1 ...
- OpenCV系列之级联分类器 | 六十一
目标 在本教程中, 我们将学习Haar级联对象检测的工作原理. 我们将使用基于Haar Feature的Cascade分类器了解人脸检测和眼睛检测的基础知识. 我们将使用cv::CascadeClas ...
- 基于OpenCV Haar实战级联分类器的使用
点击上方"小白学视觉",选择加"星标"或"置顶" 重磅干货,第一时间送达 近年来,对象检测引起了广泛的关注.从智能手机到交通监控,目标检测已 ...
- opencv中traincascade训练分类器
1 分类器的训练 训练级联分类器traincascade需要OpenCV中的两个exe文件,这两个文件分别是opencv_createsamples.exe和opencv_traincascade.e ...
- OpenCV系列之级联分类器训练 | 六十二
简介 使用弱分类器的增强级联包括两个主要阶段:训练阶段和检测阶段.对象检测教程中介绍了使用基于HAAR或LBP模型的检测阶段.本文档概述了训练自己的弱分类器的级联所需的功能.当前指南将逐步完成所有不同 ...
最新文章
- Linux下远程访问mysql数据库
- WF4 持久化 第四篇
- Deep learning:二十二(linear decoder练习)
- git 修改远程仓库源
- .net mvc中级联的使用
- 分布式消息队列 NSQ 和 Kafka 对比
- innobackupex 恢复到mysql目录_innobackupex备份mysql恢复后迁移到新的mysql实例
- .net5 和 .net6 部署到 IIS 完整步骤
- unzip 命令巧用举例
- Dreamweaver/Flash CS4安装后打开时提示此产品的许可已停止工作
- 领取免费会员活动-各大平台不定时,欢迎自取
- KUI-金山界面库 自定义消息
- 学习日常英语(每天更新10+—)
- 微型计算机ccc认证样品测试,CCC认证、检测中国赛西64102188
- 多轴控制玻璃行业程序 相机 ST LAD SFC
- Ubuntu下安装拳皇97
- 股票数据API接口合集:腾讯股票接口、麦蕊智数股票接口、和讯网股票接口、新浪股票接口、雪球股票数据、网易股票数据
- 高校“保安哥”成“励志哥”
- 快播倒下后,电影站长该如何面对转型之痛
- CSDN新手教程之——积分
热门文章
- [置顶] 金山云存储解决企业办公难题
- Android编译环境(1) - 编译Native C的模块
- It's hard to say goodbye, everyone.
- luoguP4705 玩游戏
- 工业互联网方案商“全应科技”获明势领投Pre-A轮融资
- java 8 新特性 时间api使用实例
- 终于研究出如何设置新版paypal付款时汇率损失方的问题了
- 学习nodejs之hello world
- shell之for循环的3个简单脚本
- 用DataAdapter对象填充DataSet数据集。