opencv中文文档
官方文档说明
官方文档说明
bool cv::CascadeClassifier::load(const String &filename);
功能 : 从文件中加载级联分类器
参数介绍
opencv安装包中包含以及训练好的分类器文件(opencv\sources\data\haarcascades)
分类器文件下载
void cv::putText(InputOutputArray img, const String& text, Point org, int fontFace, double fontScale, Scalar color, int thickness = 1, int lineType = LINE_8, bool bottomLeftOrigin = false);
功能 : 给视频图像添加文字说明
参数介绍
衬线体就是有边角装饰的字体, 无衬线字体通常是机械和统一粗细的线条, 没有边角的装饰
FONT_HERSHEY_SIMPLEX : 正常大小的无衬线字体
FONT_HERSHEY_PLAIN : 小号无衬线字体
FONT_HERSHEY_DUPLEX : 正常大小的无衬线字体(比FONT_HERSHEY_SIMPLEX更复杂)
FONT_HERSHEY_COMPLEX : 正常大小的衬线字体
FONT_HERSHEY_TRIPLEX : 正常大小的衬线字体(比FONT_HERSHEY_COMPLEX更复杂)
FONT_HERSHEY_COMPLEX_SMALL : FONT_HERSHEY_COMPLEX的较小版本
FONT_HERSHEY_SCRIPT_SIMPLEX : 手写样式字体
FONT_HERSHEY_SCRIPT_COMPLEX : 更复杂的FONT_HERSHEY_SCRIPT_SIMPLEX变体
FONT_ITALIC : 斜体字体的标志
#include
#include
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";
char* mouth_cascade_name = "D:\\opencv\\opencv\\sources\\data\\haarcascades\\haarcascade_mcs_mouth.xml";
void faceRecongize(cv::CascadeClassifier faceCascade, cv::CascadeClassifier eyesCascade, cv::CascadeClassifier mouthCascade, cv::Mat frame);
int main(){
cv::VideoCapture *videoCap = new cv::VideoCapture;
cv::CascadeClassifier faceCascade;
cv::CascadeClassifier eyesCascade;
cv::CascadeClassifier mouthCascade;
// 加载脸部分类器文件
if (!faceCascade.load(face_cascade_name)) {
std::cout << "load face_cascade_name failed. " << std::endl;
return -1;
}
// 加载眼睛部分分类器文件
if (!eyesCascade.load(eyes_cascade_name)) {
std::cout << "load eyes_cascade_name failed. " << std::endl;
return -1;
}
// 加载嘴部分类器文件
if (!mouthCascade.load(mouth_cascade_name)) {
std::cout << "load mouth_cascade_name failed. " << std::endl;
return -1;
}
// 打开摄像机
videoCap->open(0);
if (!videoCap->isOpened()) {
videoCap->release();
std::cout << "open camera failed"<< std::endl;
return -1;
}
std::cout << "open camera success"<< std::endl;
while(1){
cv::Mat frame;
//读取视频帧
videoCap->read(frame);
if (frame.empty()) {
videoCap->release();
return -1;
}
//进行人脸识别
faceRecongize(faceCascade, eyesCascade, mouthCascade, frame);
//窗口进行展示
imshow("face", frame);
//等待回车键按下退出程序
if (cv::waitKey(30) == 13) {
cv::destroyAllWindows();
return 0;
}
}
system("pause");
return 0;
}
void faceRecongize(cv::CascadeClassifier faceCascade, cv::CascadeClassifier eyesCascade, cv::CascadeClassifier mouthCascade, cv::Mat frame) {
std::vector faces;
// 检测人脸
faceCascade.detectMultiScale(frame, 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(frame, 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 = frame(faces[i]);
std::vector eyes;
// 检测眼睛
eyesCascade.detectMultiScale(faceROI, eyes, 1.1, 2, 0 | cv::CASCADE_SCALE_IMAGE, cv::Size(30, 30));
for (int 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(frame, eye_center, radius, cv::Scalar(255, 0, 0), 4, 8, 0);
}
cv::Mat mouthROI = frame(faces[i]);
std::vector mouth;
// 检测嘴部
mouthCascade.detectMultiScale(mouthROI, mouth, 1.1, 2, 0 | cv::CASCADE_SCALE_IMAGE, cv::Size(30, 30));
for (int k = 0; k < mouth.size(); k++)
{
//用长方形画出嘴部
cv::Rect rect(faces[i].x + mouth[k].x, faces[i].y + mouth[k].y, mouth[k].width, mouth[k].height);
rectangle(frame, rect, cv::Scalar(0, 255, 0), 2, 8, 0);
}
// 检测到两个眼睛和一个嘴巴, 可认为检测到有效人脸
if (eyes.size() >= 2 && mouth.size() >= 1) {
// 人脸上方区域写字进行标识
cv::Point centerText(faces[i].x + faces[i].width / 2 - 40, faces[i].y - 20);
cv::putText(frame, "face", centerText, cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(0, 0, 255), 2);
}
}
}
cmake_minimum_required (VERSION 3.5)
project (faceRecongize2015)
MESSAGE(STATUS "PROJECT_SOURCE_DIR " ${PROJECT_SOURCE_DIR})
SET(SRC_LISTS ${PROJECT_SOURCE_DIR}/src/main.cpp)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
# 配置头文件目录
include_directories(${PROJECT_SOURCE_DIR}/src)
include_directories("D:\\opencv\\opencv\\build\\include")
include_directories("D:\\opencv\\opencv\\build\\include\\opencv2")
# 设置不显示命令框
if(MSVC)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup")
endif()
# 添加库文件
set(PRO_OPENCV_LIB "D:\\opencv\\opencv\\build\\x64\\vc15\\lib\\opencv_world460.lib" "D:\\opencv\\opencv\\build\\x64\\vc15\\lib\\opencv_world460.lib")
IF(WIN32)
# 生成可执行程序
ADD_EXECUTABLE(faceRecongize2015 ${SRC_LISTS})
# 链接库文件
TARGET_LINK_LIBRARIES(faceRecongize2015 ${PRO_OPENCV_LIB})
ENDIF()
编译前先安装opencv, cmake, Visual Studio 2015
我的opencv的安装目录(D:\opencv), 源码安装比较麻烦, 建议直接下载安装包进行安装。
opencv安装包下载
opencv源码下载
目录结构
- src
- mian.cpp
- build_x64
- CMakeLists
cmake -G "Visual Studio 14 2015 Win64" ..
cmake --build ./ --config Release