使用配置在Qt5环境下的OpenCV4实现调用摄像头获取图像进行人正脸识别与眼睛识别
本文所涉程序为OpenCV4自带例程,仅为记录本人学习中所碰到的问题与对程序的理解。
该例程延迟可调,延迟越大越准确。但仅支持人正脸识别,稍有遮挡便无法正确识别
先在.pro文件中声明如下变量
INCLUDEPATH +=D:\Opencv\OpenCV-MinGW-Build-OpenCV-4.1.1-x64\include
LIBS +=D:\Opencv\OpenCV-MinGW-Build-OpenCV-4.1.1-x64\x64\mingCVw\bin\libopencv_core411.dll
LIBS +=D:\Opencv\OpenCV-MinGW-Build-OpenCV-4.1.1-x64\x64\mingw\bin\libopencv_highgui411.dll
LIBS +=D:\Opencv\OpenCV-MinGW-Build-OpenCV-4.1.1-x64\x64\mingw\bin\libopencv_imgproc411.dll
LIBS +=D:\Opencv\OpenCV-MinGW-Build-OpenCV-4.1.1-x64\x64\mingw\bin\libopencv_calib3d411.dll
LIBS +=D:\Opencv\OpenCV-MinGW-Build-OpenCV-4.1.1-x64\x64\mingw\bin\libopencv_imgcodecs411.dll
LIBS +=D:\Opencv\OpenCV-MinGW-Build-OpenCV-4.1.1-x64\x64\mingw\bin\libopencv_videoio411.dll
LIBS +=D:\Opencv\OpenCV-MinGW-Build-OpenCV-4.1.1-x64\x64\mingw\bin\libopencv_video411.dll
//以上放在.pro文件原有代码的最后就好,前面的路径根据自己安装位置不同而改变
所涉程序如下
#include "opencv2/objdetect.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/videoio.hpp"
#include <iostream>
using namespace std;
using namespace cv;
static void help(const char** argv)//这是帮助文件
{
cout << "\nThis program demonstrates the use of cv::CascadeClassifier class to detect objects (Face + eyes). You can use Haar or LBP features.\n"
"This classifier can recognize many kinds of rigid objects, once the appropriate classifier is trained.\n"
"It's most known use is for faces.\n"
"Usage:\n"
<< argv[0]
<< " [--cascade=<cascade_path> this is the primary trained classifier such as frontal face]\n"
" [--nested-cascade[=nested_cascade_path this an optional secondary classifier such as eyes]]\n"
" [--scale=<image scale greater or equal to 1, try 1.3 for example>]\n"
" [--try-flip]\n"
" [filename|camera_index]\n\n"
"example:\n"
<< argv[0]
<< " --cascade=\"data/haarcascades/haarcascade_frontalface_alt.xml\" --nested-cascade=\"data/haarcascades/haarcascade_eye_tree_eyeglasses.xml\" --scale=1.3\n\n"
"During execution:\n\tHit any key to quit.\n"
"\tUsing OpenCV version " << CV_VERSION << "\n" << endl;
}
void detectAndDraw( Mat& img, CascadeClassifier& cascade,
CascadeClassifier& nestedCascade,
double scale, bool tryflip );//定义函数detectAndDraw
string cascadeName;
string nestedCascadeName;
int main( int argc, const char** argv )
{
VideoCapture capture;
Mat frame, image;
string inputName;
bool tryflip;
CascadeClassifier cascade, nestedCascade;//设定两个分类器文件名:cascade、nestedCascade
double scale;
cv::CommandLineParser parser(argc, argv,
"{help h||}"
"{cascade|haarcascade_frontalface_alt.xml|}"
//这里改为了相对路径,文件放在了构建后的文件夹第
//一层,原分类器文件存放位置应是oencv安装路径 \sources\data\haarcascades
"{nested-cascade|haarcascade_eye_tree_eyeglasses.xml|}"
"{scale|1|}{try-flip||}{@filename||}"//第一个参数决定了识别的速度
//第二个参数存在会使图像进行翻转
//第三个参数是在识别图片中人脸与眼睛时对象的路径
);
if (parser.has("help"))
{
help(argv);
return 0;
}
cascadeName = parser.get<string>("cascade");
nestedCascadeName = parser.get<string>("nested-cascade");
scale = parser.get<double>("scale");
if (scale < 1)
scale = 1;//保险
tryfli p = parser.has("try-flip");
inputName = parser.