-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarker_detection_IT1.cpp
More file actions
518 lines (435 loc) · 14.2 KB
/
marker_detection_IT1.cpp
File metadata and controls
518 lines (435 loc) · 14.2 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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/**
* @file april_tags.cpp
* @brief Example application for April tags library
* @author: Michael Kaess
*
* Opens the first available camera (typically a built in camera in a
* laptop) and continuously detects April tags in the incoming
* images. Detections are both visualized in the live image and shown
* in the text console. Optionally allows selecting of a specific
* camera in case multiple ones are present and specifying image
* resolution as long as supported by the camera. Also includes the
* option to send tag detections via a serial port, for example when
* running on a Raspberry Pi that is connected to an Arduino.
*/
using namespace std;
#include <iostream>
#include <cstring>
#include <vector>
#include <list>
#include <sys/time.h>
const string usage = "\n"
"Usage:\n"
" apriltags_demo [OPTION...] [IMG1 [IMG2...]]\n"
"\n"
"Options:\n"
" -h -? Show help options\n"
" -a Arduino (send tag ids over serial port)\n"
" -d Disable graphics\n"
" -t Timing of tag extraction\n"
" -C <bbxhh> Tag family (default 36h11)\n"
" -D <id> Video device ID (if multiple cameras present)\n"
" -F <fx> Focal length in pixels\n"
" -W <width> Image width (default 640, availability depends on camera)\n"
" -H <height> Image height (default 480, availability depends on camera)\n"
" -S <size> Tag size (square black frame) in meters\n"
" -E <exposure> Manually set camera exposure (default auto; range 0-10000)\n"
" -G <gain> Manually set camera gain (default auto; range 0-255)\n"
" -B <brightness> Manually set the camera brightness (default 128; range 0-255)\n"
"\n";
const string intro = "\n"
"April tags test code\n"
"(C) 2012-2014 Massachusetts Institute of Technology\n"
"Michael Kaess\n"
"\n";
#ifndef __APPLE__
#define EXPOSURE_CONTROL // only works in Linux
#endif
#ifdef EXPOSURE_CONTROL
#include <libv4l2.h>
#include <linux/videodev2.h>
#include <fcntl.h>
#include <errno.h>
#endif
// OpenCV library for easy access to USB camera and drawing of images
// on screen
#include "opencv2/opencv.hpp"
// April tags detector and various families that can be selected by command line option
#include "AprilTags/TagDetector.h"
#include "AprilTags/Tag16h5.h"
#include "AprilTags/Tag25h7.h"
#include "AprilTags/Tag25h9.h"
#include "AprilTags/Tag36h9.h"
#include "AprilTags/Tag36h11.h"
// Needed for getopt / command line options processing
#include <unistd.h>
extern int optind;
extern char *optarg;
// For Arduino: locally defined serial port access class
#include "Serial.h"
const char* windowName = "apriltags_demo";
// utility function to provide current system time (used below in
// determining frame rate at which images are being processed)
double tic() {
struct timeval t;
gettimeofday(&t, NULL);
return ((double)t.tv_sec + ((double)t.tv_usec)/1000000.);
}
#include <cmath>
#ifndef PI
const double PI = 3.14159265358979323846;
#endif
const double TWOPI = 2.0*PI;
/**
* Normalize angle to be within the interval [-pi,pi].
*/
inline double standardRad(double t) {
if (t >= 0.) {
t = fmod(t+PI, TWOPI) - PI;
} else {
t = fmod(t-PI, -TWOPI) + PI;
}
return t;
}
/**
* Convert rotation matrix to Euler angles
*/
void wRo_to_euler(const Eigen::Matrix3d& wRo, double& yaw, double& pitch, double& roll) {
yaw = standardRad(atan2(wRo(1,0), wRo(0,0)));
double c = cos(yaw);
double s = sin(yaw);
pitch = standardRad(atan2(-wRo(2,0), wRo(0,0)*c + wRo(1,0)*s));
roll = standardRad(atan2(wRo(0,2)*s - wRo(1,2)*c, -wRo(0,1)*s + wRo(1,1)*c));
}
class Demo {
AprilTags::TagDetector* m_tagDetector;
AprilTags::TagCodes m_tagCodes;
bool m_draw; // draw image and April tag detections?
bool m_arduino; // send tag detections to serial port?
bool m_timing; // print timing information for each tag extraction call
int m_width; // image size in pixels
int m_height;
double m_tagSize; // April tag side length in meters of square black frame
double m_fx; // camera focal length in pixels
double m_fy;
double m_px; // camera principal point
double m_py;
int m_deviceId; // camera id (in case of multiple cameras)
list<string> m_imgNames;
cv::VideoCapture m_cap;
int m_exposure;
int m_gain;
int m_brightness;
Serial m_serial;
public:
// default constructor
Demo() :
// default settings, most can be modified through command line options (see below)
m_tagDetector(NULL),
m_tagCodes(AprilTags::tagCodes36h11),
m_draw(true),
m_arduino(false),
m_timing(false),
m_width(640),
m_height(480),
m_tagSize(0.166),
m_fx(600),
m_fy(600),
m_px(m_width/2),
m_py(m_height/2),
m_exposure(-1),
m_gain(-1),
m_brightness(-1),
m_deviceId(0)
{}
// changing the tag family
void setTagCodes(string s) {
if (s=="16h5") {
m_tagCodes = AprilTags::tagCodes16h5;
} else if (s=="25h7") {
m_tagCodes = AprilTags::tagCodes25h7;
} else if (s=="25h9") {
m_tagCodes = AprilTags::tagCodes25h9;
} else if (s=="36h9") {
m_tagCodes = AprilTags::tagCodes36h9;
} else if (s=="36h11") {
m_tagCodes = AprilTags::tagCodes36h11;
} else {
cout << "Invalid tag family specified" << endl;
exit(1);
}
}
// parse command line options to change default behavior
void parseOptions(int argc, char* argv[]) {
int c;
while ((c = getopt(argc, argv, ":h?adtC:F:H:S:W:E:G:B:D:")) != -1) {
// Each option character has to be in the string in getopt();
// the first colon changes the error character from '?' to ':';
// a colon after an option means that there is an extra
// parameter to this option; 'W' is a reserved character
switch (c) {
case 'h':
case '?':
cout << intro;
cout << usage;
exit(0);
break;
case 'a':
m_arduino = true;
break;
case 'd':
m_draw = false;
break;
case 't':
m_timing = true;
break;
case 'C':
setTagCodes(optarg);
break;
case 'F':
m_fx = atof(optarg);
m_fy = m_fx;
break;
case 'H':
m_height = atoi(optarg);
m_py = m_height/2;
break;
case 'S':
m_tagSize = atof(optarg);
break;
case 'W':
m_width = atoi(optarg);
m_px = m_width/2;
break;
case 'E':
#ifndef EXPOSURE_CONTROL
cout << "Error: Exposure option (-E) not available" << endl;
exit(1);
#endif
m_exposure = atoi(optarg);
break;
case 'G':
#ifndef EXPOSURE_CONTROL
cout << "Error: Gain option (-G) not available" << endl;
exit(1);
#endif
m_gain = atoi(optarg);
break;
case 'B':
#ifndef EXPOSURE_CONTROL
cout << "Error: Brightness option (-B) not available" << endl;
exit(1);
#endif
m_brightness = atoi(optarg);
break;
case 'D':
m_deviceId = atoi(optarg);
break;
case ':': // unknown option, from getopt
cout << intro;
cout << usage;
exit(1);
break;
}
}
if (argc > optind) {
for (int i=0; i<argc-optind; i++) {
m_imgNames.push_back(argv[optind+i]);
}
}
}
void setup() {
m_tagDetector = new AprilTags::TagDetector(m_tagCodes);
// prepare window for drawing the camera images
if (m_draw) {
cv::namedWindow(windowName, 1);
}
// optional: prepare serial port for communication with Arduino
if (m_arduino) {
m_serial.open("/dev/ttyACM0");
}
}
void setupVideo() {
#ifdef EXPOSURE_CONTROL
// manually setting camera exposure settings; OpenCV/v4l1 doesn't
// support exposure control; so here we manually use v4l2 before
// opening the device via OpenCV; confirmed to work with Logitech
// C270; try exposure=20, gain=100, brightness=150
string video_str = "/dev/video0";
video_str[10] = '0' + m_deviceId;
int device = v4l2_open(video_str.c_str(), O_RDWR | O_NONBLOCK);
if (m_exposure >= 0) {
// not sure why, but v4l2_set_control() does not work for
// V4L2_CID_EXPOSURE_AUTO...
struct v4l2_control c;
c.id = V4L2_CID_EXPOSURE_AUTO;
c.value = 1; // 1=manual, 3=auto; V4L2_EXPOSURE_AUTO fails...
if (v4l2_ioctl(device, VIDIOC_S_CTRL, &c) != 0) {
cout << "Failed to set... " << strerror(errno) << endl;
}
cout << "exposure: " << m_exposure << endl;
v4l2_set_control(device, V4L2_CID_EXPOSURE_ABSOLUTE, m_exposure*6);
}
if (m_gain >= 0) {
cout << "gain: " << m_gain << endl;
v4l2_set_control(device, V4L2_CID_GAIN, m_gain*256);
}
if (m_brightness >= 0) {
cout << "brightness: " << m_brightness << endl;
v4l2_set_control(device, V4L2_CID_BRIGHTNESS, m_brightness*256);
}
v4l2_close(device);
#endif
// find and open a USB camera (built in laptop camera, web cam etc)
m_cap = cv::VideoCapture(m_deviceId);
if(!m_cap.isOpened()) {
cerr << "ERROR: Can't find video device " << m_deviceId << "\n";
exit(1);
}
m_cap.set(CV_CAP_PROP_FRAME_WIDTH, m_width);
m_cap.set(CV_CAP_PROP_FRAME_HEIGHT, m_height);
cout << "Camera successfully opened (ignore error messages above...)" << endl;
cout << "Actual resolution: "
<< m_cap.get(CV_CAP_PROP_FRAME_WIDTH) << "x"
<< m_cap.get(CV_CAP_PROP_FRAME_HEIGHT) << endl;
}
void print_detection(AprilTags::TagDetection& detection) const {
cout << " Id: " << detection.id
<< " (Hamming: " << detection.hammingDistance << ")";
// recovering the relative pose of a tag:
// NOTE: for this to be accurate, it is necessary to use the
// actual camera parameters here as well as the actual tag size
// (m_fx, m_fy, m_px, m_py, m_tagSize)
Eigen::Vector3d translation;
Eigen::Matrix3d rotation;
detection.getRelativeTranslationRotation(m_tagSize, m_fx, m_fy, m_px, m_py,
translation, rotation);
Eigen::Matrix3d F;
F <<
1, 0, 0,
0, -1, 0,
0, 0, 1;
Eigen::Matrix3d fixed_rot = F*rotation;
double yaw, pitch, roll;
wRo_to_euler(fixed_rot, yaw, pitch, roll);
cout << " distance=" << translation.norm()
<< "m, x=" << translation(0)
<< ", y=" << translation(1)
<< ", z=" << translation(2)
<< ", yaw=" << yaw
<< ", pitch=" << pitch
<< ", roll=" << roll
<< endl;
// Also note that for SLAM/multi-view application it is better to
// use reprojection error of corner points, because the noise in
// this relative pose is very non-Gaussian; see iSAM source code
// for suitable factors.
}
void processImage(cv::Mat& image, cv::Mat& image_gray) {
// alternative way is to grab, then retrieve; allows for
// multiple grab when processing below frame rate - v4l keeps a
// number of frames buffered, which can lead to significant lag
// m_cap.grab();
// m_cap.retrieve(image);
// detect April tags (requires a gray scale image)
cv::cvtColor(image, image_gray, CV_BGR2GRAY);
double t0;
if (m_timing) {
t0 = tic();
}
vector<AprilTags::TagDetection> detections = m_tagDetector->extractTags(image_gray);
if (m_timing) {
double dt = tic()-t0;
cout << "Extracting tags took " << dt << " seconds." << endl;
}
// print out each detection
cout << detections.size() << " tags detected:" << endl;
for (int i=0; i<detections.size(); i++) {
print_detection(detections[i]);
}
// show the current image including any detections
if (m_draw) {
for (int i=0; i<detections.size(); i++) {
// also highlight in the image
detections[i].draw(image);
}
imshow(windowName, image); // OpenCV call
}
// optionally send tag information to serial port (e.g. to Arduino)
if (m_arduino) {
if (detections.size() > 0) {
// only the first detected tag is sent out for now
Eigen::Vector3d translation;
Eigen::Matrix3d rotation;
detections[0].getRelativeTranslationRotation(m_tagSize, m_fx, m_fy, m_px, m_py,
translation, rotation);
m_serial.print(detections[0].id);
m_serial.print(",");
m_serial.print(translation(0));
m_serial.print(",");
m_serial.print(translation(1));
m_serial.print(",");
m_serial.print(translation(2));
m_serial.print("\n");
} else {
// no tag detected: tag ID = -1
m_serial.print("-1,0.0,0.0,0.0\n");
}
}
}
// Load and process a single image
void loadImages() {
cv::Mat image;
cv::Mat image_gray;
for (list<string>::iterator it=m_imgNames.begin(); it!=m_imgNames.end(); it++) {
image = cv::imread(*it); // load image with opencv
processImage(image, image_gray);
while (cv::waitKey(100) == -1) {}
}
}
// Video or image processing?
bool isVideo() {
return m_imgNames.empty();
}
// The processing loop where images are retrieved, tags detected,
// and information about detections generated
void loop() {
cv::Mat image;
cv::Mat image_gray;
int frame = 0;
double last_t = tic();
while (true) {
// capture frame
m_cap >> image;
processImage(image, image_gray);
// print out the frame rate at which image frames are being processed
frame++;
if (frame % 10 == 0) {
double t = tic();
cout << " " << 10./(t-last_t) << " fps" << endl;
last_t = t;
}
// exit if any key is pressed
if (cv::waitKey(1) >= 0) break;
}
}
}; // Demo
// here is were everything begins
int main(int argc, char* argv[]) {
Demo demo;
// process command line options
demo.parseOptions(argc, argv);
demo.setup();
if (demo.isVideo()) {
cout << "Processing video" << endl;
// setup image source, window for drawing, serial port...
demo.setupVideo();
// the actual processing loop where tags are detected and visualized
demo.loop();
} else {
cout << "Processing image" << endl;
// process single image
demo.loadImages();
}
return 0;
}