OpenCV人脸检测例程分析
目录
代码预览
// cv3_face_detection.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "opencv2/objdetect.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
/* Function Headers */
void detectAndDisplay(Mat frame);
/* Global variables */
String face_cascade_name = "haarcascade_frontalface_alt.xml";
String eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;
String window_name = "Capture - Face detection";
/* @function main */
int main(void)
{
VideoCapture capture;
Mat frame;
//-- 1. Load the cascades
if (!face_cascade.load(face_cascade_name)){ printf("--(!)Error loading face cascade\n"); return -1; };
if (!eyes_cascade.load(eyes_cascade_name)){ printf("--(!)Error loading eyes cascade\n"); return -1; };
//-- 2. Read the video stream
capture.open(0);
if (!capture.isOpened()) { printf("--(!)Error opening video capture\n"); return -1; }
while (capture.read(frame))
{
if (frame.empty())
{
printf(" --(!) No captured frame -- Break!");
break;
}
//-- 3. Apply the classifier to the frame
detectAndDisplay(frame);
int c = waitKey(10);
if ((char)c == 27) { break; } // escape
}
return 0;
}
/* @function detectAndDisplay */
void detectAndDisplay(Mat frame)
{
std::vector<Rect> faces;
Mat frame_gray;
cvtColor(frame, frame_gray, COLOR_BGR2GRAY);
equalizeHist(frame_gray, frame_gray);
//-- Detect faces
face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(30, 30));
for (size_t i = 0; i < faces.size(); i++)
{
Point center(faces[i].x + faces[i].width / 2, faces[i].y + faces[i].height / 2); //求人脸中心
ellipse(frame, center, Size(faces[i].width / 2, faces[i].height / 2), 0, 0, 360, Scalar(255, 0, 255), 4, 8, 0);
Mat faceROI = frame_gray(faces[i]);
std::vector<Rect> eyes;
//-- In each face, detect eyes
eyes_cascade.detectMultiScale(faceROI, eyes, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(30, 30)); //在人脸区域检测眼
for (size_t j = 0; j < eyes.size(); j++)
{
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(frame, eye_center, radius, Scalar(255, 0, 0), 4, 8, 0);
}
}
//-- Show what you got
imshow(window_name, frame);
}
注意:要将opencv安装路径下的data目录下的haarcascade_frontalface_alt.xml,和haarcascade_eye_tree_eyeglasses.xml复制到程序所在目录。
程序流程分析
主程序
detectAndDisplay函数流程
该函数接受的参数:Mat frame(一帧图像)
功能:在此frame数据上利用检测器进行检测,并进行标记,最后显示。因为主程序不断的循环执行,读取一帧,标记一帧,并显示一帧,所以是动态检测,即人脸跟踪。
注:检测眼睛与检测人脸过程类似,不同之处是,检测人眼时,输入的Mat是已经检测出的人脸的矩形区域。
细节分析
级联分类器对象CascadeClassifier
函数
bool cv::CascadeClassifier:: load(const string & filename)
功能:为CascadeClassifier对象初始化,告诉它你要检测什么。
filename:已经训练好的检测器数据,XML文件
void cv::CascadeClassifier::detectMultiScale ( InputArray image,
std::vector< Rect > & objects,
double scaleFactor = 1.1,
int minNeighbors = 3,
int flags = 0,
Size minSize = Size(),
Size maxSize = Size()
)
功能:在frame中检测尺度可变的目标区域,将检测到的Rect添加到vector中。
scaleFactor:每次变尺度扫描,图像尺度放大的倍数。要大于1,越接近1,结果可能越准确,但是运算量增大。
minSize::目标图像可能的最小尺寸
maxSize:目标图像可能的最大尺寸
VideoCapture对象
函数
//将VideoCapture对象与视频文件或摄像头关联
virtual bool cv::VideoCapture::open ( int device )
virtual bool cv::VideoCapture::open ( const String & filename )
//判断VideoCapture是否初始化
virtual bool cv::VideoCapture::isOpened () const
//读取一帧图像到image
virtual bool cv::VideoCapture::read ( OutputArray image )
//读取摄像头示例
VideoCapture cap(0); // 建立VideoCapture对象并初始化
if(!cap.isOpened()) // 检查是否已经被初始化
return -1;
namedWindow("window");
for(;;)
{
Mat frame;
cap >> frame; //读取图像
imshow("window", frame);
if(waitKey(0) == 27) break;
}
OpenCV函数
//色彩空间转化
void cv::cvtColor ( InputArray src,
OutputArray dst,
int code, //转换模式
int dstCn = 0 //通道
)
//直方图均衡化,用于增强图像对比度
void cv::equalizeHist ( InputArray src,
OutputArray dst
)
//四舍五入取整
int cvRound ( double value )