Haar与LBP分类器对象识别:
一、Haar特征:
Haar和LBP特征原理及实现在前面的博文中讲的很详细这里就不再赘述。 Haar特征原理及实现:https://www.cnblogs.com/Jack-Elvis/p/11640931.html
https://www.cnblogs.com/Jack-Elvis/p/11640970.html
二、LBP特征:
LBP特征原理及实现:https://www.cnblogs.com/Jack-Elvis/p/11813122.html
https://www.cnblogs.com/Jack-Elvis/p/11815849.html
三、AdaBoost分类器原理:
四、了解以上原理以后,进行Haar特征和LBP特征分类器的人脸检测:
1.Haar特征:
这里要用到opencv自带的haar检测器xml文件(后面可以自己训练检测器文件)
在自己安装opencv的文件路经 E:/opencv/build/etc/haarcascades/haarcascade_frontalface_alt.xml
#include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace std; CascadeClassifier face_cascader; //定义一个分类器变量 String haarfile = "E:/opencv/build/etc/haarcascades/haarcascade_frontalface_alt.xml"; String lbpfile = "E:/opencv/build/etc/lbpcascadeslbpcascade_frontalface.xml"; int main(int argc, char** argv) { if (!face_cascader.load(haarfile)) { //加载haar分类器xml文件 printf("could not load image... "); return -1; } namedWindow("demo-win", CV_WINDOW_AUTOSIZE); Mat src = imread("H:/123.jpg"); //读取图片 if (src.empty()) { printf("could not load image... "); return -1; } imshow("input image", src); Mat gray; vector<Rect> faces; cvtColor(src, gray, COLOR_BGR2GRAY); //转化为灰度图像 equalizeHist(gray, gray); //均值化 int st = getTickCount(); //st被赋值此时间 face_cascader.detectMultiScale(gray, faces, 1.1, 3, 0, Size(30, 30)); int et = (getTickCount() - st); // et=此时的时间-st (人脸检测时间) printf("time consume : %d", et); for (size_t t = 0; t < faces.size(); t++) { //画出检测出的每个人脸 rectangle(src, faces[t], Scalar(255, 0, 0), 2, 8, 0); } imshow("demo-win", src); waitKey(0); return 0; }
实验结果:
time consume:
2.LBP特征:
在自己安装opencv的文件路经:E:/opencv/build/etc/lbpcascadeslbpcascade_frontalface.xml
代码只有一个位置发生变化,
代码:
#include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace std; CascadeClassifier face_cascader; String haarfile = "E:/opencv/build/etc/haarcascades/haarcascade_frontalface_alt.xml"; String lbpfile = "E:/opencv/build/etc/lbpcascadeslbpcascade_frontalface.xml"; int main(int argc, char** argv) { if (!face_cascader.load(lbpfile)) { printf("could not load image... "); return -1; } namedWindow("demo-win", CV_WINDOW_AUTOSIZE); Mat src = imread("H:/123.jpg"); if (src.empty()) { printf("could not load image... "); return -1; } imshow("input image", src); Mat gray; vector<Rect> faces; cvtColor(src, gray, COLOR_BGR2GRAY); equalizeHist(gray, gray); int st = getTickCount(); face_cascader.detectMultiScale(gray, faces, 1.1, 3, 0, Size(30, 30)); int et = (getTickCount() - st); printf("time consume : %d", et); for (size_t t = 0; t < faces.size(); t++) { rectangle(src, faces[t], Scalar(255, 0, 0), 2, 8, 0); } imshow("demo-win", src); waitKey(0); return 0; }
实验结果:
time consume:
五、结论: