采用opencv3.40
#include <string>
#include "opencv2\opencv.hpp"
#include <iostream>
using namespace std;
using namespace cv;
int main()
{
string xmlPath = "D:\\opencv\\build\\etc\\haarcascades\\haarcascade_frontalface_default.xml";
CascadeClassifier ccf; //创建分类器对象
Mat img = imread("bb.jpg");
if (!ccf.load(xmlPath)) //加载训练文件
{
cout << "不能加载指定的xml文件" << endl;
return 0;
}
vector<Rect> faces; //创建一个容器保存检测出来的脸
Mat gray;
cvtColor(img, gray, COLOR_BGR2GRAY); //转换成灰度图,因为harr特征从灰度图中提取
equalizeHist(gray, gray); //直方图均衡行
ccf.detectMultiScale(gray, faces, 1.1, 3, 0, Size(10, 10), Size(200, 200)); //检测人脸
for (vector<Rect>::const_iterator iter = faces.begin(); iter != faces.end(); iter++)
{
rectangle(img, *iter, Scalar(0, 0, 255), 2, 8); //画出脸部矩形
}
imshow("faces", img);
waitKey(0);
return 1;
}