forked from BUPT-RobotTeam/cameras
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcameras.hpp
More file actions
320 lines (279 loc) · 12.3 KB
/
cameras.hpp
File metadata and controls
320 lines (279 loc) · 12.3 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#pragma once
#include <iostream>
#include <algorithm>
#include <chrono>
#include <string>
#include <opencv2/opencv.hpp>
#include <librealsense2/rs.hpp>
#include <opencv2/core/utils/logger.hpp>
#include "hikcam.hpp"
#define CAMERAS_CHECK(state, str) \
do { \
if (!(state)) { \
std::cerr << (str) << std::endl; \
return -1; \
} \
} while (false)
class cameras{
public:
enum MarkType {
MARK_CROSS = 0x00, // 正十字
MARK_X = 0x01, // "X"
};
public:
//------------------------------初始化函数------------------------------
cameras() : _intv{0}, _last_read(std::chrono::high_resolution_clock::now()), _idx(0){
std::fill_n(this->_intv, sizeof(this->_intv) / sizeof(this->_intv[0]), 0x7f7f7f7f);
cv::utils::logging::setLogLevel(cv::utils::logging::LOG_LEVEL_FATAL); // 设置opencv打印日志的等级, 不再打印warn信息
this->_cam_type = "x";
//------------------------------设置摄像头基本参数------------------------------
this->_cam_realsense_frame_width = 640;
this->_cam_realsense_frame_height = 480;
this->_cam_realsense_frame_fps = 30;
this->auto_detect_cam();
this->print_cam_info(this->type_is_useful(this->_cam_type));
}
cameras(std::string cam_type) : _intv{0}, _last_read(std::chrono::high_resolution_clock::now()), _idx(0){
std::fill_n(this->_intv, sizeof(this->_intv) / sizeof(this->_intv[0]), 0x7f7f7f7f);
cv::utils::logging::setLogLevel(cv::utils::logging::LOG_LEVEL_FATAL); // 设置opencv打印日志的等级, 不再打印warn信息
this->_cam_type = "x";
//------------------------------设置摄像头基本参数------------------------------
this->_cam_realsense_frame_width = 640;
this->_cam_realsense_frame_height = 480;
this->_cam_realsense_frame_fps = 30;
if (!this->type_is_useful(cam_type)) {
std::cerr << "param: " << cam_type << " is not useful" << std::endl;
std::cerr << "automatically find available devices" << std::endl;
this->auto_detect_cam();
}
else
this->_cam_type = cam_type;
this->print_cam_info(this->type_is_useful(this->_cam_type));
}
//------------------------------打开摄像头------------------------------
bool open() {
if (this->_cam_type == "i") {
this->_cam_industry = cv::VideoCapture(0); // TODO: 修改成可以处理多摄像头
return this->_cam_industry.isOpened();
}
else if (this->_cam_type == "h") {
return this->_cam_hik.open(0); // TODO: 修改成可以处理多摄像头
}
else if (this->_cam_type == "d") {
rs2::context ctx;
auto list = ctx.query_devices();
if (list.size() > 0) {
this->_cam_realsense_pipe = rs2::pipeline(ctx);
return true;
}
}
return false;
}
//------------------------------开始采集------------------------------
bool start() {
if (this->_cam_type == "i") {
return true;
}
else if (this->_cam_type == "h") {
return this->_cam_hik.start();
}
else if (this->_cam_type == "d") {
this->_cam_realsense_cfg.enable_stream(RS2_STREAM_COLOR, this->_cam_realsense_frame_width, this->_cam_realsense_frame_height, RS2_FORMAT_RGB8, this->_cam_realsense_frame_fps);
this->_cam_realsense_cfg.enable_stream(RS2_STREAM_DEPTH, this->_cam_realsense_frame_width, this->_cam_realsense_frame_height, RS2_FORMAT_Z16, this->_cam_realsense_frame_fps);
this->_cam_realsense_pipe.start(this->_cam_realsense_cfg);
return true;
}
return false;
}
//------------------------------停止采集------------------------------
bool stop() {
if (this->_cam_type == "i") {
this->_cam_industry.release();
return !this->_cam_industry.isOpened();
}
else if (this->_cam_type == "h") {
return this->_cam_hik.stop();
}
else if (this->_cam_type == "d") {
this->_cam_realsense_pipe.stop();
return true;
}
return false;
}
//------------------------------获取一帧图片------------------------------
cv::Mat get_frame() {
if (this->_cam_type == "i") {
this->_cam_industry.read(this->_frame);
}
else if (this->_cam_type == "h") {
this->_frame = this->_cam_hik.read();
}
else if (this->_cam_type == "d") {
this->_frame_realsense = this->_cam_realsense_pipe.wait_for_frames();
rs2::frame color_frame = this->_frame_realsense.get_color_frame();
const int w = color_frame.as<rs2::video_frame>().get_width();
const int h = color_frame.as<rs2::video_frame>().get_height();
cv::Mat image(cv::Size(w, h), CV_8UC3, (void*)color_frame.get_data(), cv::Mat::AUTO_STEP);
cv::cvtColor(image, this->_frame, cv::COLOR_RGB2BGR);
}
else {
this->_frame.release();
}
auto endtime = std::chrono::high_resolution_clock::now();
auto us = std::chrono::duration_cast<std::chrono::microseconds>(endtime - this->_last_read);
this->_intv[this->_idx++] = us.count();
this->_last_read = endtime;
if (this->_idx == sizeof(this->_intv) / sizeof(this->_intv[0]))
this->_idx = 0;
return this->_frame;
}
//------------------------------获取深度图像指定坐标的数据------------------------------
double get_depth(int x, int y) {
if (this->_cam_type == "d") {
rs2::depth_frame depth = this->_frame_realsense.get_depth_frame();
return depth.get_distance(std::clamp(x, 0, depth.get_width()), std::clamp(y, 0, depth.get_height()));
}
return 0.0;
}
//------------------------------获取fps------------------------------
double get_fps()
{
uint64_t total = 0;
uint64_t size = 0;
for (auto x : this->_intv)
{
if (x != 0x7f7f7f7f)
{
total += x;
size++;
}
}
return 1.0e6 / (total / double(size));
}
//------------------------------画图形------------------------------
void draw_mark(int x, int y, MarkType mark_type = MARK_CROSS, cv::Scalar color = cv::Scalar(0, 0, 255), int line_length = 20, int line_width = 3) {
const int frame_widht = this->_frame.cols;
const int frame_height = this->_frame.rows;
if (mark_type == MARK_CROSS) {
cv::line(this->_frame, cv::Point(x, y - line_length), cv::Point(x, y + line_length), color, line_width);
cv::line(this->_frame, cv::Point(x - line_length, y), cv::Point(x + line_length, y), color, line_width);
}
else if (mark_type == MARK_X) {
cv::line(this->_frame, cv::Point(x - line_length, y - line_length), cv::Point(x + line_length, y + line_length), color, line_width);
cv::line(this->_frame, cv::Point(x + line_length, y - line_length), cv::Point(x - line_length, y + line_length), color, line_width);
}
}
private:
bool type_is_useful(std::string cam_type) {
if (cam_type == "h" || cam_type == "i" || cam_type == "d")
return true;
return false;
}
bool auto_detect_cam() {
this->_cam_type = "x";
do {
if (this->cam_is_accessible_industry()) {
this->_cam_type = "i";
break;
}
else if (this->cam_is_accessible_hik()) {
this->_cam_type = "h";
break;
}
else if (this->cam_is_accessible_realsense()) {
this->_cam_type = "d";
break;
}
}while(0);
return type_is_useful(this->_cam_type);
}
bool cam_is_accessible_industry() {
//------------------------------检查工业摄像头是否可用------------------------------
cv::VideoCapture cap(0);
bool is_accessible = false;
if (cap.isOpened()) {
is_accessible = true;
cap.release();
}
return is_accessible;
}
bool cam_is_accessible_hik() {
//------------------------------检查海康摄像头是否可用------------------------------
void* handle = NULL;
MV_CC_DEVICE_INFO_LIST stDeviceList;
memset(&stDeviceList, 0, sizeof(MV_CC_DEVICE_INFO_LIST));
int nRet = MV_OK;
nRet = MV_CC_EnumDevices(MV_GIGE_DEVICE | MV_USB_DEVICE, &stDeviceList);
if (MV_OK != nRet)
{
printf("MV_CC_EnumDevices fail! nRet [%x]\n", nRet);
return false;
}
bool is_accessible = false;
if (stDeviceList.nDeviceNum > 0) {
do {
// 尝试打开摄像头
MV_CC_CreateHandle(&handle, stDeviceList.pDeviceInfo[0]);
MV_CC_OpenDevice(handle);
is_accessible = MV_CC_IsDeviceConnected(handle);
// 关闭摄像头并删除句柄
MV_CC_CloseDevice(handle);
MV_CC_DestroyHandle(handle);
}while(0);
}
return is_accessible;
}
bool cam_is_accessible_realsense() {
//------------------------------检查深度摄像头是否可用------------------------------
rs2::context ctx;
rs2::device_list dev_list = ctx.query_devices();
bool is_accessible = false;
if (dev_list.size() > 0) {
rs2::device dev = dev_list[0]; // TODO: 修改为更加灵活的版本
try {
auto pipeline = rs2::pipeline(ctx);
pipeline.start(); // start 失败的话就不用 stop了
pipeline.stop();
is_accessible = true;
} catch (const rs2::error &e) {
is_accessible = false;
}
}
return is_accessible;
}
void print_cam_info(bool state) {
if (state) {
if (this->_cam_type == "i") {
std::cout << "Now you are using industrial camera" << std::endl;
}
else if (this->_cam_type == "h") {
std::cout << "Now you are using hik camera" << std::endl;
}
else if (this->_cam_type == "d") {
std::cout << "Now you are using realsense camera" << std::endl;
}
}
else {
std::cout << "No cameras are available" << std::endl;
}
}
private:
//------------------------------摄像头类型------------------------------
hikcam _cam_hik; // 海康摄像头
cv::VideoCapture _cam_industry; // 工业摄像头
rs2::pipeline _cam_realsense_pipe; // 深度摄像头的pipeline
std::string _cam_type; // 摄像头的类型: h - hik摄像头; i - 工业摄像头; d - 深度摄像头
//------------------------------摄像头参数设置------------------------------
rs2::config _cam_realsense_cfg; // 深度摄像头的设置
//------------------------------摄像头参数------------------------------
int _cam_realsense_frame_width; // 深度摄像头帧宽
int _cam_realsense_frame_height; // 深度摄像头帧高
int _cam_realsense_frame_fps; // 深度摄像头帧数
//------------------------------数据帧------------------------------
cv::Mat _frame; // 图片处理后的opencv frame
rs2::frameset _frame_realsense; // 深度相机视频帧
//------------------------------计算fps------------------------------
uint32_t _intv[10]; // 以微秒为单位的间隔
uint32_t _idx;
decltype(std::chrono::high_resolution_clock::now()) _last_read;
};