OpenCV 4.12.0
开源计算机视觉
加载中...
搜索中...
无匹配项
samples/cpp/peopledetect.cpp
// 此文件是 OpenCV 项目的一部分。
// 它受许可条款的约束,许可条款位于顶层目录中的 LICENSE 文件中
// 此发行的版本中,网址为 https://opencv.ac.cn/license.html
#include <iostream>
#include <iomanip>
using namespace cv;
using namespace std;
class Detector
{
enum Mode { Default, Daimler } m;
HOGDescriptor hog, hog_d;
public:
Detector() : m(Default), hog(), hog_d(Size(48, 96), Size(16, 16), Size(8, 8), Size(8, 8), 9)
{
hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());
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)
{
// 使用默认参数运行检测器。为了获得更高的命中率
// (以及更多的误报,分别),降低 hitThreshold 和
// groupThreshold(将 groupThreshold 设置为 0 以完全关闭分组)。
vector<Rect> found;
if (m == Default)
hog.detectMultiScale(img, found, 0, Size(8,8), Size(), 1.05, 2, false);
else if (m == Daimler)
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);
}
};
static const string keys = "{ help h | | 打印帮助信息 }"
"{ camera c | 0 | 从摄像头捕获视频(设备索引从 0 开始)}"
"{ video v | | 使用视频作为输入 }";
int main(int argc, char** argv)
{
CommandLineParser parser(argc, argv, keys);
parser.about("此示例演示了 HoG 描述符的使用。");
if (parser.has("help"))
{
parser.printMessage();
return 0;
}
int camera = parser.get<int>("camera");
string file = parser.get<string>("video");
if (!parser.check())
{
parser.printErrors();
return 1;
}
if (file.empty())
cap.open(camera);
else
{
file = samples::findFileOrKeep(file);
cap.open(file);
}
if (!cap.isOpened())
{
cout << "无法打开视频流:'" << (file.empty() ? "<camera>" : file) << "'" << endl;
return 2;
}
cout << "按 'q' 或 <ESC> 退出。" << endl;
cout << "按 <空格键> 在 Default 和 Daimler 检测器之间切换" << endl;
Detector detector;
Mat frame;
for (;;)
{
cap >> frame;
if (frame.empty())
{
cout << "读取完成:空帧" << endl;
break;
}
vector<Rect> found = detector.detect(frame);
t = getTickCount() - t;
// 显示窗口
{
ostringstream buf;
buf << "模式: " << 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("人体检测器", frame);
// 与用户交互
const char key = (char)waitKey(1);
if (key == 27 || key == 'q') // ESC
{
cout << "已请求退出" << endl;
break;
}
else if (key == ' ')
{
detector.toggleMode();
}
}
return 0;
}
如果数组没有元素,则返回 true。
int64_t int64
n 维密集数组类
定义 mat.hpp:830
2D 矩形的模板类。
定义 types.hpp:444
Point_< _Tp > tl() const
左上角
_Tp x
左上角的 x 坐标
定义 types.hpp:487
_Tp y
左上角的 y 坐标
定义 types.hpp:488
_Tp width
矩形的宽度
定义 types.hpp:489
_Tp height
矩形的高度
定义 types.hpp:490
Point_< _Tp > br() const
右下角
用于指定图像或矩形大小的模板类。
Definition types.hpp:335
用于从视频文件、图像序列或摄像头捕获视频的类。
Definition videoio.hpp:772
virtual bool open(const String &filename, int apiPreference=CAP_ANY)
打开视频文件或捕获设备或 IP 视频流以进行视频捕获。
virtual bool isOpened() const
如果视频捕获已初始化,则返回 true。
这是用于将只读输入数组传递到 OpenCV 函数中的代理类。
定义 mat.hpp:161
cv::getTickCount
int64 getTickCount()
int cvRound(double value)
将浮点数舍入为最接近的整数。
Definition fast_math.hpp:200
ximgproc.hpp
返回时钟周期数。
void imshow(const String &winname, InputArray mat)
在指定窗口中显示图像。
int waitKey(int delay=0)
等待按键按下。
void rectangle(InputOutputArray img, Point pt1, Point pt2, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
绘制一个简单、粗或填充的矩形。
void putText(InputOutputArray img, const String &text, Point org, int fontFace, double fontScale, Scalar color, int thickness=1, int lineType=LINE_8, bool bottomLeftOrigin=false)
绘制文本字符串。
int main(int argc, char *argv[])
定义 highgui_qt.cpp:3
定义 core.hpp:107
STL 命名空间。
HOG(定向梯度直方图)描述符和对象检测器的实现。
定义 objdetect.hpp:403
virtual void setSVMDetector(InputArray svmdetector)
设置线性 SVM 分类器的系数。
virtual void detectMultiScale(InputArray img, std::vector< Rect > &foundLocations, std::vector< double > &foundWeights, double hitThreshold=0, Size winStride=Size(), Size padding=Size(), double scale=1.05, double groupThreshold=2.0, bool useMeanshiftGrouping=false) const
检测输入图像中不同大小的对象。检测到的对象作为列表返回...