Qt&OpenCV练习(八)

使用配置在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.
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值