Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
161 lines (126 loc) · 4.24 KB
/
Copy pathmain.cpp
File metadata and controls
161 lines (126 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "Core.h"
#include "camera/pmd/PMDCamera.h"
#include "camera/axon/AxonCamera.h"
using namespace ht;
int main() {
// 初始化相机实例
//DepthCamera::Ptr camera = std::make_shared<ht::camera::PMDCamera>(); // PMD camera
DepthCamera::Ptr camera = std::make_shared<ht::camera::AXonCamera>(); // AXon camera
// 初始化检测参数
DetectionParams::Ptr params = camera->getDefaultParams(); //使用默认参数
// 初始化检测器
HandDetector::Ptr handDetector = std::make_shared<HandDetector>();
// 操作选项
bool showBackground = true, useSVM = false, useEdgeConn = false;
cv::Mat scaledZImage;
// fps信息
const int FPS_CYCLE_FRAMES = 8; // 平均多少帧更新一次帧率
using ms = std::chrono::duration<float, std::milli>;
using time_point = std::chrono::high_resolution_clock::time_point;
std::chrono::high_resolution_clock timer = std::chrono::high_resolution_clock();
time_point currCycleStartTime = timer.now(); // 当前周期的开始时间
float currFPS = 0.0; // 当前的帧率
int currFrame = 0; // 当前位于第几帧
char chr[64] = { '0' };
// 主进程
while (true)
{
++currFrame;
// 从相机获取最新的XYZMap
cv::Mat xyzMap = camera->getXYZMap();
/**** 平面检测部分 ****/
// 查询当前帧中的检测对象
params->handUseSVM = useSVM;
params->handRequireEdgeConnected = useEdgeConn;
// 手部对象
std::vector<Hand::Ptr> hands;
// 设置检测参数
handDetector->setParams(params);
handDetector->update(*camera);
hands = handDetector->getHands();
/**** 可视化部分 ****/
// 构可视化图像
cv::Mat handVisual;
// 可视化背景
//xyzMap = cv::Scalar::all(0);
if (showBackground) {
Visualizer::visualizeXYZMap(xyzMap, handVisual, 1.5f);
}
else
{
cv::Mat clearBackground(cv::Size(xyzMap.cols, xyzMap.rows), CV_32FC3);
clearBackground = cv::Scalar::all(0);
Visualizer::visualizeXYZMap(clearBackground, handVisual, 1.5f);
}
Visualizer::visualizeXYZMap(xyzMap, xyzMap, 1.0f);
const cv::Scalar WHITE(255, 255, 255);
// 或者检测到的手的特征
int i = 0;
for (Hand::Ptr & hand : hands) {
if (!handVisual.empty()) {
// 可视化检测到的手
Visualizer::visualizeHand(handVisual, handVisual, hand.get());
std::map<std::string, Vec3f> LMap = hand->getLHlabelXYZ();
// 输出三维坐标
/*for (auto item : LMap)
{
BOOST_LOG_TRIVIAL(info) << item.first << ": " << item.second;
}*/
}
++i;
if (i >= 2) {
break;
}
}
if (hands.size() > 0) {
// 显示检测到的手的数量
cv::putText(handVisual, std::to_string(hands.size()) +
util::pluralize(" Hand", hands.size()),
Point2i(10, 25), 0, 0.5, WHITE);
}
// update FPS
if (currFrame % FPS_CYCLE_FRAMES == 0) {
time_point now = timer.now();
currFPS = (float)FPS_CYCLE_FRAMES * 1000.0f / std::chrono::duration_cast<ms>(now - currCycleStartTime).count();
currCycleStartTime = now;
}
sprintf(chr, "FPS: %02.3f", currFPS);
cv::putText(handVisual, chr, Point2i(handVisual.cols - 120, 25), 0, 0.5, cv::Scalar(0, 255, 255));
// 可视化手、平面、XYZMap
if (!xyzMap.empty()) {
scaledZImage.create(cv::Size(ht::camera::AXonCamera::Depth_Width * 1, ht::camera::AXonCamera::Depth_Height * 1), CV_8UC1);
resize(xyzMap, scaledZImage, scaledZImage.size());
cv::imshow(camera->getModelName() + " Depth Map", scaledZImage);
if (!handVisual.empty()) {
scaledZImage.create(cv::Size(ht::camera::AXonCamera::Depth_Width * 1, ht::camera::AXonCamera::Depth_Height * 1), CV_8UC1);
resize(handVisual, scaledZImage, scaledZImage.size());
cv::imshow("Demo Output", scaledZImage);
}
}
/**** 模式切换部分 ****/
int c = cv::waitKey(1);
// 不区分大小写
if (c >= 'a' && c <= 'z') c &= 0xdf;
// 按下ESC或者Q
if (c == 'Q' || c == 27) {
/*** 退出程序 ***/
break;
}
// 切换模式
switch (c) {
case 'H':
showBackground ^= 1;
BOOST_LOG_TRIVIAL(info) << "Show Background: " << (showBackground ? "True" : "False");
break;
case 'S':
useSVM ^= 1;
BOOST_LOG_TRIVIAL(info) << "USE SVM: " << (useSVM ? "True" : "False");
break;
case 'U':
useEdgeConn ^= 1;
break;
}
}
cv::destroyAllWindows();
return 0;
}