【opencv】示例-peopledetect.cpp HOG(方向梯度直方图)描述子和SVM(支持向量机)进行行人检测...

6087888a7f92c9bd70c624febf3cc9e5.png

f1228373c681cbda839d58058f9ba266.png

d98bfec8a728ac7d058355634f270226.png

// 包含OpenCV项目所需的objdetect模块头文件
#include <opencv2/objdetect.hpp>
// 包含OpenCV项目所需的highgui模块头文件,用于图像的显示和简单操作
#include <opencv2/highgui.hpp>
// 包含OpenCV项目所需的imgproc模块头文件,用于图像处理
#include <opencv2/imgproc.hpp>
// 包含OpenCV项目所需的videoio模块头文件,用于视频的读写
#include <opencv2/videoio.hpp>
#include <iostream> // 包含输入输出流的标准头文件
#include <iomanip> // 包含输入输出流格式化的标准头文件


// 使用OpenCV和标准命名空间下的所有实体
using namespace cv;
using namespace std;


// 定义一个Detector类,用于行人检测
class Detector
{
    enum Mode { Default, Daimler } m; // 定义两种模式的枚举类型
    HOGDescriptor hog, hog_d;         // 定义两个HOG描述子对象
public:
    // 构造函数,初始化模式为Default和两个描述子hog与hog_d
    Detector() : m(Default), hog(), hog_d(Size(48, 96), Size(16, 16), Size(8, 8), Size(8, 8), 9)
    {
        // 设置HOG描述子的SVM检测器为默认的行人检测器
        hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());
        // 设置hog_d描述子的SVM检测器为Daimler行人检测器
        hog_d.setSVMDetector(HOGDescriptor::getDaimlerPeopleDetector());
    }
    // 切换检测模式的方法
    void toggleMode() { m = (m == Default ? Daimler : Default); }
    // 获取当前模式名称的方法
    string modeName() const { return (m == Default ? "Default" : "Daimler"); }
    // 执行检测的方法
    vector<Rect> detect(InputArray img)
    {
        // 创建一个向量来存储检测到的矩形
        vector<Rect> found;
        if (m == Default)
            // 默认模式下使用hog描述子进行多尺度检测
            hog.detectMultiScale(img, found, 0, Size(8,8), Size(), 1.05, 2, false);
        else if (m == Daimler)
            // Daimler模式下使用hog_d描述子进行多尺度检测
            hog_d.detectMultiScale(img, found, 0, Size(8,8), Size(), 1.05, 2, true);
        return found; // 返回检测结果
    }
    // 调整检测矩形的方法
    void adjustRect(Rect & r) const
{
        // HOG检测器返回的矩形稍大于真实的物体,故稍微缩小矩形以获得更好的效果
        r.x += cvRound(r.width*0.1);
        r.width = cvRound(r.width*0.8);
        r.y += cvRound(r.height*0.07);
        r.height = cvRound(r.height*0.8);
    }
};


// 定义命令行参数的keys字符串
static const string keys = "{ help h   |   | print help message }"
                           "{ camera c | 0 | capture video from camera (device index starting from 0) }"
                           "{ video v  |   | use video as input }";


// main函数,程序的入口
int main(int argc, char** argv)
{
    // 创建CommandLineParser对象来解析命令行参数
    CommandLineParser parser(argc, argv, keys);
    parser.about("This sample demonstrates the use of the HoG descriptor.");
    if (parser.has("help"))
    {
        // 如果存在help参数,则打印帮助信息并退出
        parser.printMessage();
        return 0;
    }
    // 获取camera和video参数
    int camera = parser.get<int>("camera");
    string file = parser.get<string>("video");
    if (!parser.check())
    {
        // 检查参数解析是否有误,如果有则打印错误并退出
        parser.printErrors();
        return 1;
    }


    VideoCapture cap; // 创建一个VideoCapture对象来捕获视频
    if (file.empty())
        // 如果video参数为空则从相机捕获视频
        cap.open(camera);
    else
    {
        // 否则打开指定的视频文件
        file = samples::findFileOrKeep(file);
        cap.open(file);
    }
    if (!cap.isOpened())
    {
        // 如果视频流打不开则打印错误信息并退出
        cout << "Can not open video stream: '" << (file.empty() ? "<camera>" : file) << "'" << endl;
        return 2;
    }


    cout << "Press 'q' or <ESC> to quit." << endl;
    cout << "Press <space> to toggle between Default and Daimler detector" << endl;
    Detector detector; // 创建一个Detector对象
    Mat frame;         // 创建一个Mat对象来存储帧
    for (;;)           // 无限循环
    {
        cap >> frame; // 从视频流中读取一帧到frame中
        if (frame.empty())
        {
            // 如果帧为空则打印信息并退出循环
            cout << "Finished reading: empty frame" << endl;
            break;
        }
        int64 t = getTickCount(); // 获取当前的tick计数
        vector<Rect> found = detector.detect(frame); // 使用detector检测行人
        t = getTickCount() - t; // 计算检测所用的时间


        // 显示窗口
        {
            ostringstream buf;
            // 将模式名称和FPS信息打印到视频帧上
            buf << "Mode: " << detector.modeName() << " ||| "
                << "FPS: " << fixed << setprecision(1) << (getTickFrequency() / (double)t);
            putText(frame, buf.str(), Point(10, 30), FONT_HERSHEY_PLAIN, 2.0, Scalar(0, 0, 255), 2, LINE_AA);
        }
        for (vector<Rect>::iterator i = found.begin(); i != found.end(); ++i)
        {
            // 迭代找到的矩形,并在视频帧上画出矩形框
            Rect &r = *i;
            detector.adjustRect(r);
            rectangle(frame, r.tl(), r.br(), cv::Scalar(0, 255, 0), 2);
        }
        imshow("People detector", frame); // 显示带有检测框的视频帧


        // 与用户交互
        const char key = (char)waitKey(1);
        // 如果用户按下ESC或'q'键,则退出循环
        if (key == 27 || key == 'q') // ESC
        {
            cout << "Exit requested" << endl;
            break;
        }
        // 如果用户按下空格键,则切换检测模式
        else if (key == ' ')
        {
            detector.toggleMode();
        }
    }
    return 0; // 程序正常退出
}

本段代码是一个使用OpenCV库的HOG(Histogram of Oriented Gradients,方向梯度直方图)描述子和SVM(Support Vector Machines,支持向量机)进行行人检测的程序。程序定义了Detector类来执行行人检测,可以在两种模式(默认模式和戴姆勒模式)之间切换。通过命令行参数,用户可以选择是从相机实时捕获视频还是读取视频文件进行检测。本程序还支持与用户的简单交互,比如按键切换模式和退出程序。最后在视频中实时标记检测到的行人,并显示当前的模式和帧率(FPS)。

hog.detectMultiScale(img, found, 0, Size(8,8), Size(), 1.05, 2, false);

3e24e5f03481fb2e8b945b2de2ca6dd3.png

624e1d57b12324aada57f0917d909eca.png

相关推荐

  1. 支持向量SVM

    2024-04-14 06:54:04       31 阅读
  2. 支持向量SVM

    2024-04-14 06:54:04       33 阅读
  3. 支持向量SVM

    2024-04-14 06:54:04       31 阅读
  4. 支持向量SVM

    2024-04-14 06:54:04       30 阅读
  5. SVM支持向量

    2024-04-14 06:54:04       23 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-14 06:54:04       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-14 06:54:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-14 06:54:04       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-14 06:54:04       20 阅读

热门阅读

  1. 微信小程序相关

    2024-04-14 06:54:04       13 阅读
  2. KDTree和Octree的区别

    2024-04-14 06:54:04       19 阅读
  3. nuxt.config.js配置

    2024-04-14 06:54:04       17 阅读
  4. React状态与引用(Refs)- 差异和使用场景

    2024-04-14 06:54:04       14 阅读
  5. js实现webp转png/jpg

    2024-04-14 06:54:04       13 阅读
  6. Spring Boot+Vue的高校食材采供管理系统

    2024-04-14 06:54:04       15 阅读
  7. git合并冲突

    2024-04-14 06:54:04       12 阅读
  8. GStreamer插件开发的环境搭建记录

    2024-04-14 06:54:04       14 阅读
  9. 浏览器提示存在不安全脚本如何解决

    2024-04-14 06:54:04       14 阅读
  10. 【C++】模拟list

    2024-04-14 06:54:04       15 阅读
  11. Docker - MongoDB

    2024-04-14 06:54:04       16 阅读