OpenCV 4.12.0
開源計算機視覺
載入中...
搜尋中...
無匹配項
samples/cpp/peopledetect.cpp
// 此檔案是 OpenCV 專案的一部分。
// 它受許可條款的約束,許可條款位於頂層目錄中的 LICENSE 檔案中
// 此發行的版本中,網址為 https://opencv.tw/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
檢測輸入影像中不同大小的物件。檢測到的物件作為列表返回...