OpenCV图像特征提取与检测C++(七) 级联分类器 HAAR与LBP– 人脸检测(图像和视频)
2018-08-17 12:53
811 查看
级联检测器(cascade):通过弱分类器检测到的人脸再次进行多次弱分类器检测,最终得到的人脸检测结果的准确率就非常高了。
使用级联检测器前,需要有训练好的数据。
对象检测,检测的是什么对象由训练的数据决定
API:
void CascadeClassifier::detectMultiScale( // 在不同尺度空间检测 输入图像中不同大小的对象。 检测到的对象作为矩形列表返回 InputArray image, // 待检测的图像,CV_8U类型 CV_OUT std::vector<Rect>& objects, // 保存检测到的对象 double scaleFactor = 1.1, // 不同尺度空间之间的差距 int minNeighbors = 3, // 表示构成检测目标的相邻矩形的最小个数(默认为3个)。如果组成检测目标的小矩形的个数和小于 min_neighbors - 1 都会被排除 int flags = 0, // 忽略 Size minSize = Size(), // 最小尺寸,小于这个尺寸的不检测,默认检测所有小尺寸,minNeighbors 设的很大,这个就设小点,minNeighbors 设的很小,这个就设大点 Size maxSize = Size() // 最大尺寸,大于这个尺寸的不检测,默认检测所有大尺寸 );
代码:(图片)
#include <opencv2/opencv.hpp> #include <opencv2/xfeatures2d.hpp> #include<opencv2/face.hpp> #include<iostream> #include<math.h> #include <string> #include<fstream> using namespace cv::face; using namespace cv; using namespace std; using namespace cv::xfeatures2d; int main() { CascadeClassifier face_cascade; // 级联分类检测器 face_cascade.load("D:/opencv/build/etc/haarcascades/haarcascade_frontalface_alt.xml"); // 加载训练的数据 Mat src, gray; src = imread("C:/Users/Administrator/Desktop/pic/HOG1.jpg"); cvtColor(src, gray, COLOR_BGR2GRAY); equalizeHist(gray, gray);//直方图均值化,提升对比度,提升图像特征提取的准确率 imshow("src", src); vector<Rect> faces;//保存检测到的人脸的矩形框 face_cascade.detectMultiScale(gray, faces, 1.1, 3, 0, Size(20, 20)); cout << ", faces.size=" << faces.size() << endl; for ( size_t t= 0; t < faces.size(); t++) { rectangle(src, faces[t], Scalar(0, 0, 255), 2); } imshow("faces", src); waitKey(0); }
结果:
级联分类器原理
Haar 特征与LBP特征
级联分类器原理,不光是人脸检测,只要是有明显特征的对象,都能通过此方式训练,检测
Viola和Jones – 2001在CVPR提出一种实时对象(人脸)检测框架算法,训练速度非常慢,检测速度非常快,5000个正向人脸样本与300万个非人脸负样本数据精准度非常高。
OpenCV中级联分类器使用- CascadeClassifier
AdaBoost(自适应分类器)
弱分类器 – weak classifier = Feature
强分类器 – 多个弱分类器的线性组合
级联分类器 – 多个强分类器组合
代码:(视频)
#include <opencv2/opencv.hpp> #include <opencv2/xfeatures2d.hpp> #include<opencv2/face.hpp> #include<iostream> #include<math.h> #include <string> #include<fstream> using namespace cv::face; using namespace cv; using namespace std; using namespace cv::xfeatures2d; int main() { CascadeClassifier face_cas, eye_cas; face_cas.load("D:/opencv/build/etc/haarcascades/haarcascade_frontalface_alt.xml"); eye_cas.load("D:/opencv/build/etc/haarcascades/haarcascade_eye.xml"); // 除了从相机中读取帧数据,还能读取本地视频文件 VideoCapture capture; capture.open("C:/Users/Administrator/Desktop/pic/4.avi"); Mat frame; // 每一帧图像 Mat gray; vector<Rect> faces; // 保存检测到的人脸 vector<Rect> eyes; // 保存检测到的人眼 while (capture.read(frame)) // 从相机中读取每一帧数据 { cvtColor(frame, gray, COLOR_BGR2GRAY); equalizeHist(gray, gray); face_cas.detectMultiScale(frame, faces, 1.1, 3, 0, Size(30, 30)); for (size_t t = 0; t < faces.size(); t++) { Rect re; re.x = faces[static_cast<int>(t)].x; re.y = faces[static_cast<int>(t)].y; re.width = faces[static_cast<int>(t)].width; re.height = faces[static_cast<int>(t)].height / 2;//眼睛在上半脸 Mat faceRe = frame(re); eye_cas.detectMultiScale(faceRe, eyes, 1.1, 3, 0, Size(10, 10)); for (size_t k = 0; k < eyes.size(); k++) { Rect rect;//由于是在人脸框内检测人眼,所以需要在整帧数据上重新定位人眼框的位置 rect.x = faces[static_cast<int>(t)].x+eyes[k].x; rect.y = faces[static_cast<int>(t)].y + eyes[k].y; rect.width = eyes[k].width; rect.height = eyes[k].height; rectangle(frame, rect, Scalar(0, 255, 0)); } rectangle(frame, faces[static_cast<int>(t)], Scalar(0, 0, 255), 2); } imshow("faces", frame); } waitKey(0); }
结果:
HAAR与LBP级联分类器是使用
HAAR与LBP的区别:
- HAAR特征是浮点计算,LBP是整数计算
- LBP训练样本比HAAR要大
- 同样的样本空间,HAAR比LBP准确
- LBP比HAAR速度要快
LBP比HAAR速度对比
代码:
#include <opencv2/opencv.hpp> #include <opencv2/xfeatures2d.hpp> #include<opencv2/face.hpp> #include<iostream> #include<math.h> #include <string> #include<fstream> using namespace cv::face; using namespace cv; using namespace std; using namespace cv::xfeatures2d; int main() { CascadeClassifier haar_cas, lbp_cas; haar_cas.load("D:/opencv/build/etc/haarcascades/haarcascade_frontalface_alt.xml"); lbp_cas.load("D:/opencv/build/etc/lbpcascades/lbpcascade_frontalface.xml"); Mat haarsrc = imread("C:/Users/Administrator/Desktop/pic/5.jpg"); Mat haargray; cvtColor(haarsrc, haargray, COLOR_BGR2GRAY); equalizeHist(haargray, haargray); vector<Rect>faces1; double t1 = getTickCount(); haar_cas.detectMultiScale(haargray, faces1, 1.1, 3, 0, Size(30, 30)); double t2 = (getTickCount() - t1) / getTickFrequency(); cout << "haar time=" << t2 << endl; for (size_t t = 0; t < faces1.size(); t++) { rectangle(haarsrc, faces1[t], Scalar(0, 0, 255), 2); } imshow("haarface", haarsrc); Mat lbpsrc = imread("C:/Users/Administrator/Desktop/pic/5.jpg"); Mat lbpgray; cvtColor(lbpsrc, lbpgray, COLOR_BGR2GRAY); equalizeHist(lbpgray, lbpgray); vector<Rect>faces2; t1 = getTickCount(); lbp_cas.detectMultiScale(lbpgray, faces2, 1.1, 3, 0, Size(30, 30)); t2 = (getTickCount() - t1) / getTickFrequency(); cout << "lbp time=" << t2 << endl; for (size_t t = 0; t < faces2.size(); t++) { rectangle(lbpsrc, faces2[t], Scalar(0, 0, 255), 2); } imshow("lbpface", lbpsrc); waitKey(0); }
结果:
读取视频中人脸
#include <opencv2/opencv.hpp> #include <opencv2/xfeatures2d.hpp> #include<opencv2/face.hpp> #include<iostream> #include<math.h> #include <string> #include<fstream> using namespace cv::face; using namespace cv; using namespace std; using namespace cv::xfeatures2d; int main() { CascadeClassifier haar_cas, lbp_cas; haar_cas.load("D:/opencv/build/etc/haarcascades/haarcascade_frontalface_alt.xml"); lbp_cas.load("D:/opencv/build/etc/lbpcascades/lbpcascade_frontalface.xml"); VideoCapture capture;//如果是摄像头则 VideoCapture capture(0); capture.open("C:/Users/Administrator/Desktop/pic/4.mp4"); Mat frame; Mat haargray; while (capture.read(frame)) { cvtColor(frame, haargray, COLOR_BGR2GRAY); equalizeHist(haargray, haargray); vector<Rect>faces1; lbp_cas.detectMultiScale(haargray, faces1, 1.1, 3, 0, Size(30, 30)); for (size_t t = 0; t < faces1.size(); t++) { rectangle(frame, faces1[t], Scalar(0, 0, 255), 2); } imshow("haarface", frame); cout << "hh"; } waitKey(0); }阅读更多
相关文章推荐
- OpenCV图像特征提取与检测C++(八)视频中人脸检测与眼睛跟踪
- 关于人脸检测中的Haar特征提取
- 目标检测的图像特征提取之(二)LBP特征
- 目标检测的图像特征提取之(三)Haar特征
- 目标检测的图像特征提取之(二)LBP特征
- opencv 利用类haar小波特征的人脸、人眼等特征检测(带代码)
- 目标检测的图像特征提取之(二)LBP特征
- 目标检测的图像特征提取之(二)LBP特征
- 目标检测的图像特征提取之(三)Haar特征
- 目标检测的图像特征提取之(二)LBP特征
- OpenCV中基于Haar特征和级联分类器的人脸检测
- 目标检测的图像特征提取之LBP特征
- 结合OpenCV摄像头使用Dlib库进行人脸检测及标注特征点和提取人脸特征-Python
- AdaBoost中利用Haar特征进行人脸识别算法分析与总结2——级联分类器与检测过程
- 目标检测的图像特征提取——Haar特征
- 目标检测的图像特征提取之(三)Haar特征
- 目标检测的图像特征提取之(四)OpenCV中BLOB特征提取与几何形状分类
- 目标检测的图像特征提取之(三)Haar特征
- 目标检测的图像特征提取之(二)LBP特征
- AdaBoost中利用Haar特征进行人脸识别算法分析与总结2——级联分类器与检测过程