说明
很多代码比较老了,这里是目前教新的版本,OpenCV2.4.10或者OpenCV3.X
人脸检测
效果
![1314054-1390f5d4198d8ad4.png](https://i-blog.csdnimg.cn/blog_migrate/4c64c03356a9226eae2388c96be9ae43.png)
image.png
要求
opencv\sources\data\haarcascades复制haarcascade_frontalface_alt2.xml到项目代码目录中
代码
//头文件
#include<opencv2/objdetect/objdetect.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
using namespace cv;
//人脸检测的类
CascadeClassifier faceCascade;
int main()
{
faceCascade.load("haarcascade_frontalface_alt2.xml"); //加载分类器,注意文件路径
Mat img = imread("PrettyGirl.jpg");
Mat imgGray;
vector<Rect> faces;
if(img.empty())
{
return 1;
}
if(img.channels() ==3)
{
cvtColor(img, imgGray, CV_RGB2GRAY);
}
else
{
imgGray = img;
}
faceCascade.detectMultiScale(imgGray, faces, 1.2, 6, 0, Size(0, 0)); //检测人脸
if(faces.size()>0)
{
for(int i =0; i<faces.size(); i++)
{
rectangle(img, Point(faces[i].x, faces[i].y), Point(faces[i].x + faces[i].width, faces[i].y + faces[i].height),
Scalar(0, 255, 0), 1, 8); //框出人脸位置
}
}
imshow("FacesOfPrettyGirl", img);
waitKey(0);
return 0;
}