// 包含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:
// 構造函數(shù),初始化模式為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"); }
// 執(zhí)行檢測的方法
vector<Rect> detect(InputArray img)
{
// 創(chuàng)建一個向量來存儲檢測到的矩形
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);
}
};
// 定義命令行參數(shù)的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函數(shù),程序的入口
int main(int argc, char** argv)
{
// 創(chuàng)建CommandLineParser對象來解析命令行參數(shù)
CommandLineParser parser(argc, argv, keys);
parser.about("This sample demonstrates the use of the HoG descriptor.");
if (parser.has("help"))
{
// 如果存在help參數(shù),則打印幫助信息并退出
parser.printMessage();
return 0;
}
// 獲取camera和video參數(shù)
int camera = parser.get<int>("camera");
string file = parser.get<string>("video");
if (!parser.check())
{
// 檢查參數(shù)解析是否有誤,如果有則打印錯誤并退出
parser.printErrors();
return 1;
}
VideoCapture cap; // 創(chuàng)建一個VideoCapture對象來捕獲視頻
if (file.empty())
// 如果video參數(shù)為空則從相機捕獲視頻
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; // 創(chuàng)建一個Detector對象
Mat frame; // 創(chuàng)建一個Mat對象來存儲幀
for (;;) // 無限循環(huán)
{
cap >> frame; // 從視頻流中讀取一幀到frame中
if (frame.empty())
{
// 如果幀為空則打印信息并退出循環(huán)
cout << "Finished reading: empty frame" << endl;
break;
}
int64 t = getTickCount(); // 獲取當前的tick計數(shù)
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'鍵,則退出循環(huán)
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類來執(zhí)行行人檢測,可以在兩種模式(默認模式和戴姆勒模式)之間切換。通過命令行參數(shù),用戶可以選擇是從相機實時捕獲視頻還是讀取視頻文件進行檢測。本程序還支持與用戶的簡單交互,比如按鍵切換模式和退出程序。最后在視頻中實時標記檢測到的行人,并顯示當前的模式和幀率(FPS)。
hog.detectMultiScale(img, found, 0, Size(8,8), Size(), 1.05, 2, false);
文章來源:http://www.zghlxwxcb.cn/news/detail-851028.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-851028.html
到了這里,關于【opencv】示例-peopledetect.cpp HOG(方向梯度直方圖)描述子和SVM(支持向量機)進行行人檢測...的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!