【opencv450-samples】DetectionBasedTracker LBP级联分类器测试 dbt_face_detection.cpp

46 篇文章 4 订阅

#if defined(__linux__) || defined(LINUX) || defined(__APPLE__) || defined(ANDROID) || (defined(_MSC_VER) && _MSC_VER>=1800)

#include <opencv2/imgproc.hpp>  // Gaussian Blur
#include <opencv2/core.hpp>        // Basic OpenCV structures (cv::Mat, Scalar)
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>  // OpenCV window I/O
#include <opencv2/features2d.hpp>
#include <opencv2/objdetect.hpp>

#include <stdio.h>

#include<windows.h>

using namespace std;
using namespace cv;

const string WindowName = "Face Detection example";

class CascadeDetectorAdapter : public DetectionBasedTracker::IDetector
{
public:
    CascadeDetectorAdapter(cv::Ptr<cv::CascadeClassifier> detector) :
        IDetector(),
        Detector(detector)
    {
        CV_Assert(detector);
    }

    void detect(const cv::Mat& Image, std::vector<cv::Rect>& objects) CV_OVERRIDE
    {
        Detector->detectMultiScale(Image, objects, scaleFactor, minNeighbours, 0, minObjSize, maxObjSize);//检测人脸
    }

    virtual ~CascadeDetectorAdapter() CV_OVERRIDE
    {}

private:
    CascadeDetectorAdapter();
    cv::Ptr<cv::CascadeClassifier> Detector;
};

int main(int, char**)
{
    namedWindow(WindowName);

    //VideoCapture VideoStream(0);//打开摄像头
    VideoCapture VideoStream(samples::findFile("facepeople.wmv"));//打开摄像头
    if (!VideoStream.isOpened())
    {
        printf("Error: Cannot open video stream from camera\n");
        return 1;
    }

    //std::string cascadeFrontalfilename = samples::findFile("data/lbpcascades/lbpcascade_frontalface_improved.xml");
    std::string cascadeFrontalfilename = "D:/Software/vs2019+pcl+opencv/cudaopencv/opencv4.5.0/data/lbpcascades/lbpcascade_frontalface_improved.xml";//只能检测正脸lbpcascades/lbpcascade_frontalface_improved.xml
    cv::Ptr<cv::CascadeClassifier> cascade = makePtr<cv::CascadeClassifier>(cascadeFrontalfilename);//构建级联分类器
    cv::Ptr<DetectionBasedTracker::IDetector> MainDetector = makePtr<CascadeDetectorAdapter>(cascade);//主检测器
    if (cascade->empty())
    {
        printf("Error: Cannot load %s\n", cascadeFrontalfilename.c_str());
        return 2;
    }

    cascade = makePtr<cv::CascadeClassifier>(cascadeFrontalfilename);
    cv::Ptr<DetectionBasedTracker::IDetector> TrackingDetector = makePtr<CascadeDetectorAdapter>(cascade);//级联检测器
    if (cascade->empty())
    {
        printf("Error: Cannot load %s\n", cascadeFrontalfilename.c_str());
        return 2;
    }

    DetectionBasedTracker::Parameters params;

    DetectionBasedTracker Detector(MainDetector, TrackingDetector, params);//检测器

    if (!Detector.run())//检测器初始化失败
    {
        printf("Error: Detector initialization failed\n");
        return 2;
    }

    Mat ReferenceFrame;//参考帧
    Mat GrayFrame;//灰度图
    vector<Rect> Faces;//人脸矩形 向量

    for (;;)
    {
        VideoStream >> ReferenceFrame;//读取一帧


        if (!ReferenceFrame.empty())
        {
            cvtColor(ReferenceFrame, GrayFrame, COLOR_BGR2GRAY);//转灰度图
            Detector.process(GrayFrame);//检测人脸
            Detector.getObjects(Faces);//获取人脸

            for (size_t i = 0; i < Faces.size(); i++)
            {
                rectangle(ReferenceFrame, Faces[i], Scalar(0, 255, 0));//绘制人脸矩形
            }

            imshow(WindowName, ReferenceFrame);
            Sleep(200);
        }

        if (waitKey(30) > 0)
        {
            break;
        }

    }// while (waitKey(30) < 0);

    Detector.stop();//停止检测

    return 0;
}

#else

#include <stdio.h>
int main()
{
    printf("This sample works for UNIX or ANDROID or Visual Studio 2013+ only\n");
    return 0;
}

#endif

参考:

基于Opencv的人脸定位 - 掘金 (juejin.cn)icon-default.png?t=M4ADhttps://juejin.cn/post/6844903704907218951

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值