内容说明
直接参考了OpenCV 3.0的示例程序,程序为opencv\sources\samples\cpp\tutorial_code\objectDetection目录下的objectDetection.cpp文件。本文使用了C++类进行封装
实验结果
原始图像与识别结果:
程序代码
1、 OpenCVTest.cpp
#include "stdafx.h"
#include "FaceRecognition.hpp"
int _tmain(int argc, _TCHAR* argv[])
{
char* path = "C:\\Users\\dhj555\\Desktop\\22.jpg";
cv::Mat img = cv::imread(path);
FaceRecognition* recog = new FaceRecognition();
recog->setImg(img);
recog->recognition();
cvWaitKey();
return 0;
}
2、FaceRecognition.hpp
#include "stdafx.h"
#include <opencv2/opencv.hpp>
class FaceRecognition
{
private:
cv::Mat m_mImg;
char* face_cascade_name = "D:\\OpenCV\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml";
char* eyes_cascade_name = "D:\\OpenCV\\opencv\\sources\\data\\haarcascades\\haarcascade_eye.xml";
cv::CascadeClassifier face_cascade;
cv::CascadeClassifier eyes_cascade;
public:
FaceRecognition();
~FaceRecognition();
void recognition();
void setImg(cv::Mat mat);
};
FaceRecognition::FaceRecognition(){}
FaceRecognition::~FaceRecognition()
{
delete face_cascade_name;
delete eyes_cascade_name;
}
void FaceRecognition::setImg(cv::Mat mat)
{
this->m_mImg = mat;
}
void FaceRecognition::recognition()
{
//-- 1. Load the cascades
if (!face_cascade.load(face_cascade_name)){ printf("--(!)Error loading face cascade\n"); return ; };
if (!eyes_cascade.load(eyes_cascade_name)){ printf("--(!)Error loading eyes cascade\n"); return ; };
std::vector<cv::Rect> faces;
cv::Mat img_gray;
cvtColor(m_mImg, img_gray, cv::COLOR_BGR2GRAY);
cv::equalizeHist(img_gray, img_gray);
//-- 2. Detect faces
face_cascade.detectMultiScale(img_gray, faces, 1.1, 2, 0 | cv::CASCADE_SCALE_IMAGE, cv::Size(30, 30));
for (int i = 0; i < faces.size(); i++)
{
cv::Point center(faces[i].x + faces[i].width / 2, faces[i].y + faces[i].height / 2);
ellipse(m_mImg, center, cv::Size(faces[i].width / 2, faces[i].height / 2), 0, 0, 360, cv::Scalar(255, 0, 255), 4, 8, 0);
cv::Mat faceROI = img_gray(faces[i]);
std::vector<cv::Rect> eyes;
//-- In each face, detect eyes
eyes_cascade.detectMultiScale(faceROI, eyes, 1.1, 2, 0 | cv::CASCADE_SCALE_IMAGE, cv::Size(30, 30));
for (size_t j = 0; j < eyes.size(); j++)
{
cv::Point eye_center(faces[i].x + eyes[j].x + eyes[j].width / 2, faces[i].y + eyes[j].y + eyes[j].height / 2);
int radius = cvRound((eyes[j].width + eyes[j].height)*0.25);
circle(m_mImg, eye_center, radius, cv::Scalar(255, 0, 0), 4, 8, 0);
}
}
//-- 3. Show result
cv::imshow("Res", m_mImg);
}