/
common_functions.cpp
802 lines (730 loc) · 28.3 KB
/
common_functions.cpp
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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
/**
* Copyright (c) 2017, California Institute of Technology.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of the California Institute of
* Technology.
*/
#include "apriltag_ros/common_functions.h"
#include "image_geometry/pinhole_camera_model.h"
#include "common/homography.h"
#include "tagStandard52h13.h"
#include "tagStandard41h12.h"
#include "tag36h11.h"
#include "tag25h9.h"
#include "tag16h5.h"
#include "tagCustom48h12.h"
#include "tagCircle21h7.h"
#include "tagCircle49h12.h"
namespace apriltag_ros
{
TagDetector::TagDetector(ros::NodeHandle pnh) :
family_(getAprilTagOption<std::string>(pnh, "tag_family", "tag36h11")),
threads_(getAprilTagOption<int>(pnh, "tag_threads", 0)),
decimate_(getAprilTagOption<double>(pnh, "tag_decimate", 1.0)),
blur_(getAprilTagOption<double>(pnh, "tag_blur", 0.0)),
refine_edges_(getAprilTagOption<int>(pnh, "tag_refine_edges", 1)),
debug_(getAprilTagOption<int>(pnh, "tag_debug", 0)),
max_hamming_distance_(getAprilTagOption<int>(pnh, "max_hamming_dist", 2)),
publish_tf_(getAprilTagOption<bool>(pnh, "publish_tf", false))
{
// Parse standalone tag descriptions specified by user (stored on ROS
// parameter server)
XmlRpc::XmlRpcValue standalone_tag_descriptions;
if(!pnh.getParam("standalone_tags", standalone_tag_descriptions))
{
ROS_WARN("No april tags specified");
}
else
{
try
{
standalone_tag_descriptions_ =
parseStandaloneTags(standalone_tag_descriptions);
}
catch(XmlRpc::XmlRpcException e)
{
// in case any of the asserts in parseStandaloneTags() fail
ROS_ERROR_STREAM("Error loading standalone tag descriptions: " <<
e.getMessage().c_str());
}
}
// parse tag bundle descriptions specified by user (stored on ROS parameter
// server)
XmlRpc::XmlRpcValue tag_bundle_descriptions;
if(!pnh.getParam("tag_bundles", tag_bundle_descriptions))
{
ROS_WARN("No tag bundles specified");
}
else
{
try
{
tag_bundle_descriptions_ = parseTagBundles(tag_bundle_descriptions);
}
catch(XmlRpc::XmlRpcException e)
{
// In case any of the asserts in parseStandaloneTags() fail
ROS_ERROR_STREAM("Error loading tag bundle descriptions: " <<
e.getMessage().c_str());
}
}
// Optionally remove duplicate detections in scene. Defaults to removing
if(!pnh.getParam("remove_duplicates", remove_duplicates_))
{
ROS_WARN("remove_duplicates parameter not provided. Defaulting to true");
remove_duplicates_ = true;
}
// Define the tag family whose tags should be searched for in the camera
// images
if (family_ == "tagStandard52h13")
{
tf_ = tagStandard52h13_create();
}
else if (family_ == "tagStandard41h12")
{
tf_ = tagStandard41h12_create();
}
else if (family_ == "tag36h11")
{
tf_ = tag36h11_create();
}
else if (family_ == "tag25h9")
{
tf_ = tag25h9_create();
}
else if (family_ == "tag16h5")
{
tf_ = tag16h5_create();
}
else if (family_ == "tagCustom48h12")
{
tf_ = tagCustom48h12_create();
}
else if (family_ == "tagCircle21h7")
{
tf_ = tagCircle21h7_create();
}
else if (family_ == "tagCircle49h12")
{
tf_ = tagCircle49h12_create();
}
else
{
ROS_WARN("Invalid tag family specified! Aborting");
exit(1);
}
if (threads_ == 0)
{
threads_ = std::max(std::thread::hardware_concurrency() - 1U, 1U);
ROS_INFO("Thread count not specified. Using %d threads", threads_);
}
// Create the AprilTag 2 detector
td_ = apriltag_detector_create();
apriltag_detector_add_family_bits(td_, tf_, max_hamming_distance_);
td_->quad_decimate = (float)decimate_;
td_->quad_sigma = (float)blur_;
td_->nthreads = threads_;
td_->debug = debug_;
td_->refine_edges = refine_edges_;
detections_ = NULL;
}
// destructor
TagDetector::~TagDetector() {
// free memory associated with tag detector
apriltag_detector_destroy(td_);
// Free memory associated with the array of tag detections
if(detections_)
{
apriltag_detections_destroy(detections_);
}
// free memory associated with tag family
if (family_ == "tagStandard52h13")
{
tagStandard52h13_destroy(tf_);
}
else if (family_ == "tagStandard41h12")
{
tagStandard41h12_destroy(tf_);
}
else if (family_ == "tag36h11")
{
tag36h11_destroy(tf_);
}
else if (family_ == "tag25h9")
{
tag25h9_destroy(tf_);
}
else if (family_ == "tag16h5")
{
tag16h5_destroy(tf_);
}
else if (family_ == "tagCustom48h12")
{
tagCustom48h12_destroy(tf_);
}
else if (family_ == "tagCircle21h7")
{
tagCircle21h7_destroy(tf_);
}
else if (family_ == "tagCircle49h12")
{
tagCircle49h12_destroy(tf_);
}
}
AprilTagDetectionArray TagDetector::detectTags (
const cv_bridge::CvImagePtr& image,
const sensor_msgs::CameraInfoConstPtr& camera_info) {
// Convert image to AprilTag code's format
cv::Mat gray_image;
if (image->image.channels() == 1)
{
gray_image = image->image;
}
else
{
cv::cvtColor(image->image, gray_image, CV_BGR2GRAY);
}
image_u8_t apriltag_image = { .width = gray_image.cols,
.height = gray_image.rows,
.stride = gray_image.cols,
.buf = gray_image.data
};
image_geometry::PinholeCameraModel camera_model;
camera_model.fromCameraInfo(camera_info);
// Get camera intrinsic properties for rectified image.
double fx = camera_model.fx(); // focal length in camera x-direction [px]
double fy = camera_model.fy(); // focal length in camera y-direction [px]
double cx = camera_model.cx(); // optical center x-coordinate [px]
double cy = camera_model.cy(); // optical center y-coordinate [px]
ROS_INFO_STREAM_ONCE("Camera model: fx = " << fx << ", fy = " << fy << ", cx = " << cx << ", cy = " << cy);
// Check if camera intrinsics are not available - if not the calculated
// transforms are meaningless.
if (fx == 0 && fy == 0) ROS_WARN_STREAM_THROTTLE(5, "fx and fy are zero. Are the camera intrinsics set?");
// Run AprilTag 2 algorithm on the image
if (detections_)
{
apriltag_detections_destroy(detections_);
detections_ = NULL;
}
detections_ = apriltag_detector_detect(td_, &apriltag_image);
// If remove_duplicates_ is set to true, then duplicate tags are not allowed.
// Thus any duplicate tag IDs visible in the scene must include at least 1
// erroneous detection. Remove any tags with duplicate IDs to ensure removal
// of these erroneous detections
if (remove_duplicates_)
{
removeDuplicates();
}
// Compute the estimated translation and rotation individually for each
// detected tag
AprilTagDetectionArray tag_detection_array;
std::vector<std::string > detection_names;
tag_detection_array.header = image->header;
std::map<std::string, std::vector<cv::Point3d > > bundleObjectPoints;
std::map<std::string, std::vector<cv::Point2d > > bundleImagePoints;
for (int i=0; i < zarray_size(detections_); i++)
{
// Get the i-th detected tag
apriltag_detection_t *detection;
zarray_get(detections_, i, &detection);
// Bootstrap this for loop to find this tag's description amongst
// the tag bundles. If found, add its points to the bundle's set of
// object-image corresponding points (tag corners) for cv::solvePnP.
// Don't yet run cv::solvePnP on the bundles, though, since we're still in
// the process of collecting all the object-image corresponding points
int tagID = detection->id;
bool is_part_of_bundle = false;
for (unsigned int j=0; j<tag_bundle_descriptions_.size(); j++)
{
// Iterate over the registered bundles
TagBundleDescription bundle = tag_bundle_descriptions_[j];
if (bundle.id2idx_.find(tagID) != bundle.id2idx_.end())
{
// This detected tag belongs to the j-th tag bundle (its ID was found in
// the bundle description)
is_part_of_bundle = true;
std::string bundleName = bundle.name();
//===== Corner points in the world frame coordinates
double s = bundle.memberSize(tagID)/2;
addObjectPoints(s, bundle.memberT_oi(tagID),
bundleObjectPoints[bundleName]);
//===== Corner points in the image frame coordinates
addImagePoints(detection, bundleImagePoints[bundleName]);
}
}
// Find this tag's description amongst the standalone tags
// Print warning when a tag was found that is neither part of a
// bundle nor standalone (thus it is a tag in the environment
// which the user specified no description for, or Apriltags
// misdetected a tag (bad ID or a false positive)).
StandaloneTagDescription* standaloneDescription;
if (!findStandaloneTagDescription(tagID, standaloneDescription,
!is_part_of_bundle))
{
continue;
}
//=================================================================
// The remainder of this for loop is concerned with standalone tag
// poses!
double tag_size = standaloneDescription->size();
// Get estimated tag pose in the camera frame.
//
// Note on frames:
// The raw AprilTag 2 uses the following frames:
// - camera frame: looking from behind the camera (like a
// photographer), x is right, y is up and z is towards you
// (i.e. the back of camera)
// - tag frame: looking straight at the tag (oriented correctly),
// x is right, y is down and z is away from you (into the tag).
// But we want:
// - camera frame: looking from behind the camera (like a
// photographer), x is right, y is down and z is straight
// ahead
// - tag frame: looking straight at the tag (oriented correctly),
// x is right, y is up and z is towards you (out of the tag).
// Using these frames together with cv::solvePnP directly avoids
// AprilTag 2's frames altogether.
// TODO solvePnP[Ransac] better?
std::vector<cv::Point3d > standaloneTagObjectPoints;
std::vector<cv::Point2d > standaloneTagImagePoints;
addObjectPoints(tag_size/2, cv::Matx44d::eye(), standaloneTagObjectPoints);
addImagePoints(detection, standaloneTagImagePoints);
Eigen::Isometry3d transform = getRelativeTransform(standaloneTagObjectPoints,
standaloneTagImagePoints,
fx, fy, cx, cy);
geometry_msgs::PoseWithCovarianceStamped tag_pose =
makeTagPose(transform, image->header);
// Add the detection to the back of the tag detection array
AprilTagDetection tag_detection;
tag_detection.pose = tag_pose;
tag_detection.id.push_back(detection->id);
tag_detection.size.push_back(tag_size);
tag_detection_array.detections.push_back(tag_detection);
detection_names.push_back(standaloneDescription->frame_name());
}
//=================================================================
// Estimate bundle origin pose for each bundle in which at least one
// member tag was detected
for (unsigned int j=0; j<tag_bundle_descriptions_.size(); j++)
{
// Get bundle name
std::string bundleName = tag_bundle_descriptions_[j].name();
std::map<std::string,
std::vector<cv::Point3d> >::iterator it =
bundleObjectPoints.find(bundleName);
if (it != bundleObjectPoints.end())
{
// Some member tags of this bundle were detected, get the bundle's
// position!
TagBundleDescription& bundle = tag_bundle_descriptions_[j];
Eigen::Isometry3d transform =
getRelativeTransform(bundleObjectPoints[bundleName],
bundleImagePoints[bundleName], fx, fy, cx, cy);
geometry_msgs::PoseWithCovarianceStamped bundle_pose =
makeTagPose(transform, image->header);
// Add the detection to the back of the tag detection array
AprilTagDetection tag_detection;
tag_detection.pose = bundle_pose;
tag_detection.id = bundle.bundleIds();
tag_detection.size = bundle.bundleSizes();
tag_detection_array.detections.push_back(tag_detection);
detection_names.push_back(bundle.name());
}
}
// If set, publish the transform /tf topic
if (publish_tf_) {
for (unsigned int i=0; i<tag_detection_array.detections.size(); i++) {
geometry_msgs::PoseStamped pose;
pose.pose = tag_detection_array.detections[i].pose.pose.pose;
pose.header = tag_detection_array.detections[i].pose.header;
tf::Stamped<tf::Transform> tag_transform;
tf::poseStampedMsgToTF(pose, tag_transform);
tf_pub_.sendTransform(tf::StampedTransform(tag_transform,
tag_transform.stamp_,
image->header.frame_id,
detection_names[i]));
}
}
return tag_detection_array;
}
int TagDetector::idComparison (const void* first, const void* second)
{
int id1 = (*(apriltag_detection_t**)first)->id;
int id2 = (*(apriltag_detection_t**)second)->id;
return (id1 < id2) ? -1 : ((id1 == id2) ? 0 : 1);
}
void TagDetector::removeDuplicates ()
{
zarray_sort(detections_, &idComparison);
int count = 0;
bool duplicate_detected = false;
while (true)
{
if (count > zarray_size(detections_)-1)
{
// The entire detection set was parsed
return;
}
apriltag_detection_t *next_detection, *current_detection;
zarray_get(detections_, count, ¤t_detection);
int id_current = current_detection->id;
// Default id_next value of -1 ensures that if the last detection
// is a duplicated tag ID, it will get removed
int id_next = -1;
if (count < zarray_size(detections_)-1)
{
zarray_get(detections_, count+1, &next_detection);
id_next = next_detection->id;
}
if (id_current == id_next || (id_current != id_next && duplicate_detected))
{
duplicate_detected = true;
// Remove the current tag detection from detections array
int shuffle = 0;
apriltag_detection_destroy(current_detection);
zarray_remove_index(detections_, count, shuffle);
if (id_current != id_next)
{
ROS_WARN_STREAM("Pruning tag ID " << id_current << " because it "
"appears more than once in the image.");
duplicate_detected = false; // Reset
}
continue;
}
else
{
count++;
}
}
}
void TagDetector::addObjectPoints (
double s, cv::Matx44d T_oi, std::vector<cv::Point3d >& objectPoints) const
{
// Add to object point vector the tag corner coordinates in the bundle frame
// Going counterclockwise starting from the bottom left corner
objectPoints.push_back(T_oi.get_minor<3, 4>(0, 0)*cv::Vec4d(-s,-s, 0, 1));
objectPoints.push_back(T_oi.get_minor<3, 4>(0, 0)*cv::Vec4d( s,-s, 0, 1));
objectPoints.push_back(T_oi.get_minor<3, 4>(0, 0)*cv::Vec4d( s, s, 0, 1));
objectPoints.push_back(T_oi.get_minor<3, 4>(0, 0)*cv::Vec4d(-s, s, 0, 1));
}
void TagDetector::addImagePoints (
apriltag_detection_t *detection,
std::vector<cv::Point2d >& imagePoints) const
{
// Add to image point vector the tag corners in the image frame
// Going counterclockwise starting from the bottom left corner
double tag_x[4] = {-1,1,1,-1};
double tag_y[4] = {1,1,-1,-1}; // Negated because AprilTag tag local
// frame has y-axis pointing DOWN
// while we use the tag local frame
// with y-axis pointing UP
for (int i=0; i<4; i++)
{
// Homography projection taking tag local frame coordinates to image pixels
double im_x, im_y;
homography_project(detection->H, tag_x[i], tag_y[i], &im_x, &im_y);
imagePoints.push_back(cv::Point2d(im_x, im_y));
}
}
Eigen::Isometry3d TagDetector::getRelativeTransform(
const std::vector<cv::Point3d >& objectPoints,
const std::vector<cv::Point2d >& imagePoints,
double fx, double fy, double cx, double cy) const
{
Eigen::Isometry3d T = Eigen::Isometry3d::Identity(); // homogeneous transformation matrix
// perform Perspective-n-Point camera pose estimation using the
// above 3D-2D point correspondences
cv::Mat rvec, tvec;
cv::Matx33d cameraMatrix(fx, 0, cx,
0, fy, cy,
0, 0, 1);
cv::Vec4f distCoeffs(0,0,0,0); // distortion coefficients
// TODO Perhaps something like SOLVEPNP_EPNP would be faster? Would
// need to first check WHAT is a bottleneck in this code, and only
// do this if PnP solution is the bottleneck.
cv::solvePnP(objectPoints, imagePoints, cameraMatrix, distCoeffs, rvec, tvec);
cv::Matx33d R;
cv::Rodrigues(rvec, R);
// rotation
T.linear() << R(0,0), R(0,1), R(0,2), R(1,0), R(1,1), R(1,2), R(2,0), R(2,1), R(2,2);
// translation
T.translation() = Eigen::Vector3d::Map(reinterpret_cast<const double*>(tvec.data));
return T;
}
geometry_msgs::PoseWithCovarianceStamped TagDetector::makeTagPose(
const Eigen::Isometry3d& transform,
const std_msgs::Header& header)
{
geometry_msgs::PoseWithCovarianceStamped pose;
pose.header = header;
Eigen::Quaterniond rot_quaternion(transform.linear());
//===== Position and orientation
pose.pose.pose.position.x = transform.translation().x();
pose.pose.pose.position.y = transform.translation().y();
pose.pose.pose.position.z = transform.translation().z();
pose.pose.pose.orientation.x = rot_quaternion.x();
pose.pose.pose.orientation.y = rot_quaternion.y();
pose.pose.pose.orientation.z = rot_quaternion.z();
pose.pose.pose.orientation.w = rot_quaternion.w();
return pose;
}
void TagDetector::drawDetections (cv_bridge::CvImagePtr image)
{
for (int i = 0; i < zarray_size(detections_); i++)
{
apriltag_detection_t *det;
zarray_get(detections_, i, &det);
// Check if this ID is present in config/tags.yaml
// Check if is part of a tag bundle
int tagID = det->id;
bool is_part_of_bundle = false;
for (unsigned int j=0; j<tag_bundle_descriptions_.size(); j++)
{
TagBundleDescription bundle = tag_bundle_descriptions_[j];
if (bundle.id2idx_.find(tagID) != bundle.id2idx_.end())
{
is_part_of_bundle = true;
break;
}
}
// If not part of a bundle, check if defined as a standalone tag
StandaloneTagDescription* standaloneDescription;
if (!is_part_of_bundle &&
!findStandaloneTagDescription(tagID, standaloneDescription, false))
{
// Neither a standalone tag nor part of a bundle, so this is a "rogue"
// tag, skip it.
continue;
}
// Draw tag outline with edge colors green, blue, blue, red
// (going counter-clockwise, starting from lower-left corner in
// tag coords). cv::Scalar(Blue, Green, Red) format for the edge
// colors!
line(image->image, cv::Point((int)det->p[0][0], (int)det->p[0][1]),
cv::Point((int)det->p[1][0], (int)det->p[1][1]),
cv::Scalar(0, 0xff, 0)); // green
line(image->image, cv::Point((int)det->p[0][0], (int)det->p[0][1]),
cv::Point((int)det->p[3][0], (int)det->p[3][1]),
cv::Scalar(0, 0, 0xff)); // red
line(image->image, cv::Point((int)det->p[1][0], (int)det->p[1][1]),
cv::Point((int)det->p[2][0], (int)det->p[2][1]),
cv::Scalar(0xff, 0, 0)); // blue
line(image->image, cv::Point((int)det->p[2][0], (int)det->p[2][1]),
cv::Point((int)det->p[3][0], (int)det->p[3][1]),
cv::Scalar(0xff, 0, 0)); // blue
// Print tag ID in the middle of the tag
std::stringstream ss;
ss << det->id;
cv::String text = ss.str();
int fontface = cv::FONT_HERSHEY_SCRIPT_SIMPLEX;
double fontscale = 0.5;
int baseline;
cv::Size textsize = cv::getTextSize(text, fontface,
fontscale, 2, &baseline);
cv::putText(image->image, text,
cv::Point((int)(det->c[0]-textsize.width/2),
(int)(det->c[1]+textsize.height/2)),
fontface, fontscale, cv::Scalar(0xff, 0x99, 0), 2);
}
}
// Parse standalone tag descriptions
std::map<int, StandaloneTagDescription> TagDetector::parseStandaloneTags (
XmlRpc::XmlRpcValue& standalone_tags)
{
// Create map that will be filled by the function and returned in the end
std::map<int, StandaloneTagDescription> descriptions;
// Ensure the type is correct
ROS_ASSERT(standalone_tags.getType() == XmlRpc::XmlRpcValue::TypeArray);
// Loop through all tag descriptions
for (int32_t i = 0; i < standalone_tags.size(); i++)
{
// i-th tag description
XmlRpc::XmlRpcValue& tag_description = standalone_tags[i];
// Assert the tag description is a struct
ROS_ASSERT(tag_description.getType() ==
XmlRpc::XmlRpcValue::TypeStruct);
// Assert type of field "id" is an int
ROS_ASSERT(tag_description["id"].getType() ==
XmlRpc::XmlRpcValue::TypeInt);
// Assert type of field "size" is a double
ROS_ASSERT(tag_description["size"].getType() ==
XmlRpc::XmlRpcValue::TypeDouble);
int id = (int)tag_description["id"]; // tag id
// Tag size (square, side length in meters)
double size = (double)tag_description["size"];
// Custom frame name, if such a field exists for this tag
std::string frame_name;
if(tag_description.hasMember("name"))
{
// Assert type of field "name" is a string
ROS_ASSERT(tag_description["name"].getType() ==
XmlRpc::XmlRpcValue::TypeString);
frame_name = (std::string)tag_description["name"];
}
else
{
std::stringstream frame_name_stream;
frame_name_stream << "tag_" << id;
frame_name = frame_name_stream.str();
}
StandaloneTagDescription description(id, size, frame_name);
ROS_INFO_STREAM("Loaded tag config: " << id << ", size: " <<
size << ", frame_name: " << frame_name.c_str());
// Add this tag's description to map of descriptions
descriptions.insert(std::make_pair(id, description));
}
return descriptions;
}
// parse tag bundle descriptions
std::vector<TagBundleDescription > TagDetector::parseTagBundles (
XmlRpc::XmlRpcValue& tag_bundles)
{
std::vector<TagBundleDescription > descriptions;
ROS_ASSERT(tag_bundles.getType() == XmlRpc::XmlRpcValue::TypeArray);
// Loop through all tag bundle descritions
for (int32_t i=0; i<tag_bundles.size(); i++)
{
ROS_ASSERT(tag_bundles[i].getType() == XmlRpc::XmlRpcValue::TypeStruct);
// i-th tag bundle description
XmlRpc::XmlRpcValue& bundle_description = tag_bundles[i];
std::string bundleName;
if (bundle_description.hasMember("name"))
{
ROS_ASSERT(bundle_description["name"].getType() ==
XmlRpc::XmlRpcValue::TypeString);
bundleName = (std::string)bundle_description["name"];
}
else
{
std::stringstream bundle_name_stream;
bundle_name_stream << "bundle_" << i;
bundleName = bundle_name_stream.str();
}
TagBundleDescription bundle_i(bundleName);
ROS_INFO("Loading tag bundle '%s'",bundle_i.name().c_str());
ROS_ASSERT(bundle_description["layout"].getType() ==
XmlRpc::XmlRpcValue::TypeArray);
XmlRpc::XmlRpcValue& member_tags = bundle_description["layout"];
// Loop through each member tag of the bundle
for (int32_t j=0; j<member_tags.size(); j++)
{
ROS_ASSERT(member_tags[j].getType() == XmlRpc::XmlRpcValue::TypeStruct);
XmlRpc::XmlRpcValue& tag = member_tags[j];
ROS_ASSERT(tag["id"].getType() == XmlRpc::XmlRpcValue::TypeInt);
int id = tag["id"];
ROS_ASSERT(tag["size"].getType() == XmlRpc::XmlRpcValue::TypeDouble);
double size = tag["size"];
// Make sure that if this tag was specified also as standalone,
// then the sizes match
StandaloneTagDescription* standaloneDescription;
if (findStandaloneTagDescription(id, standaloneDescription, false))
{
ROS_ASSERT(size == standaloneDescription->size());
}
// Get this tag's pose with respect to the bundle origin
double x = xmlRpcGetDoubleWithDefault(tag, "x", 0.);
double y = xmlRpcGetDoubleWithDefault(tag, "y", 0.);
double z = xmlRpcGetDoubleWithDefault(tag, "z", 0.);
double qw = xmlRpcGetDoubleWithDefault(tag, "qw", 1.);
double qx = xmlRpcGetDoubleWithDefault(tag, "qx", 0.);
double qy = xmlRpcGetDoubleWithDefault(tag, "qy", 0.);
double qz = xmlRpcGetDoubleWithDefault(tag, "qz", 0.);
Eigen::Quaterniond q_tag(qw, qx, qy, qz);
q_tag.normalize();
Eigen::Matrix3d R_oi = q_tag.toRotationMatrix();
// Build the rigid transform from tag_j to the bundle origin
cv::Matx44d T_mj(R_oi(0,0), R_oi(0,1), R_oi(0,2), x,
R_oi(1,0), R_oi(1,1), R_oi(1,2), y,
R_oi(2,0), R_oi(2,1), R_oi(2,2), z,
0, 0, 0, 1);
// Register the tag member
bundle_i.addMemberTag(id, size, T_mj);
ROS_INFO_STREAM(" " << j << ") id: " << id << ", size: " << size << ", "
<< "p = [" << x << "," << y << "," << z << "], "
<< "q = [" << qw << "," << qx << "," << qy << ","
<< qz << "]");
}
descriptions.push_back(bundle_i);
}
return descriptions;
}
double TagDetector::xmlRpcGetDouble (XmlRpc::XmlRpcValue& xmlValue,
std::string field) const
{
ROS_ASSERT((xmlValue[field].getType() == XmlRpc::XmlRpcValue::TypeDouble) ||
(xmlValue[field].getType() == XmlRpc::XmlRpcValue::TypeInt));
if (xmlValue[field].getType() == XmlRpc::XmlRpcValue::TypeInt)
{
int tmp = xmlValue[field];
return (double)tmp;
}
else
{
return xmlValue[field];
}
}
double TagDetector::xmlRpcGetDoubleWithDefault (XmlRpc::XmlRpcValue& xmlValue,
std::string field,
double defaultValue) const
{
if (xmlValue.hasMember(field))
{
ROS_ASSERT((xmlValue[field].getType() == XmlRpc::XmlRpcValue::TypeDouble) ||
(xmlValue[field].getType() == XmlRpc::XmlRpcValue::TypeInt));
if (xmlValue[field].getType() == XmlRpc::XmlRpcValue::TypeInt)
{
int tmp = xmlValue[field];
return (double)tmp;
}
else
{
return xmlValue[field];
}
}
else
{
return defaultValue;
}
}
bool TagDetector::findStandaloneTagDescription (
int id, StandaloneTagDescription*& descriptionContainer, bool printWarning)
{
std::map<int, StandaloneTagDescription>::iterator description_itr =
standalone_tag_descriptions_.find(id);
if (description_itr == standalone_tag_descriptions_.end())
{
if (printWarning)
{
ROS_WARN_THROTTLE(10.0, "Requested description of standalone tag ID [%d],"
" but no description was found...",id);
}
return false;
}
descriptionContainer = &(description_itr->second);
return true;
}
} // namespace apriltag_ros