2#include <visp3/core/vpConfig.h>
3#ifdef VISP_HAVE_MODULE_SENSOR
4#include <visp3/sensor/vpRealSense2.h>
7#include <visp3/detection/vpDetectorAprilTag.h>
9#include <visp3/core/vpImageConvert.h>
10#include <visp3/gui/vpDisplayFactory.h>
11#include <visp3/vision/vpPose.h>
13void usage(
const char **argv,
int error);
15void usage(
const char **argv,
int error)
17 std::cout <<
"Synopsis" << std::endl
19 <<
" [--tag-size <size>]"
20 <<
" [--tag-family <family>]"
21 <<
" [--tag-decision-margin-threshold <threshold>]"
22 <<
" [--tag-hamming-distance-threshold <threshold>]"
23 <<
" [--tag-quad-decimate <factor>]"
24 <<
" [--tag-n-threads <number>]"
25 <<
" [--tag-z-aligned]"
26 <<
" [--tag-pose-method <method>]"
27#if defined(VISP_HAVE_DISPLAY)
31 <<
" [--thickness <thickness>"
34 <<
" [--help, -h]" << std::endl
36 std::cout <<
"Description" << std::endl
37 <<
" Compute the pose of an Apriltag in images acquired with a realsense camera." << std::endl
39 <<
" --tag-size <size>" << std::endl
40 <<
" Apriltag size in [m]." << std::endl
41 <<
" Default: 0.03" << std::endl
43 <<
" --tag-family <family>" << std::endl
44 <<
" Apriltag family. Supported values are:" << std::endl
45 <<
" 0: TAG_36h11" << std::endl
46 <<
" 1: TAG_36h10 (DEPRECATED)" << std::endl
47 <<
" 2: TAG_36ARTOOLKIT (DEPRECATED)" << std::endl
48 <<
" 3: TAG_25h9" << std::endl
49 <<
" 4: TAG_25h7 (DEPRECATED)" << std::endl
50 <<
" 5: TAG_16h5" << std::endl
51 <<
" 6: TAG_CIRCLE21h7" << std::endl
52 <<
" 7: TAG_CIRCLE49h12" << std::endl
53 <<
" 8: TAG_CUSTOM48h12" << std::endl
54 <<
" 9: TAG_STANDARD41h12" << std::endl
55 <<
" 10: TAG_STANDARD52h13" << std::endl
56 <<
" 11: TAG_ARUCO_4x4_50" << std::endl
57 <<
" 12: TAG_ARUCO_4x4_100" << std::endl
58 <<
" 13: TAG_ARUCO_4x4_250" << std::endl
59 <<
" 14: TAG_ARUCO_4x4_1000" << std::endl
60 <<
" 15: TAG_ARUCO_5x5_50" << std::endl
61 <<
" 16: TAG_ARUCO_5x5_100" << std::endl
62 <<
" 17: TAG_ARUCO_5x5_250" << std::endl
63 <<
" 18: TAG_ARUCO_5x5_1000" << std::endl
64 <<
" 19: TAG_ARUCO_6x6_50" << std::endl
65 <<
" 20: TAG_ARUCO_6x6_100" << std::endl
66 <<
" 21: TAG_ARUCO_6x6_250" << std::endl
67 <<
" 22: TAG_ARUCO_6x6_1000" << std::endl
68 <<
" 23: TAG_ARUCO_7x7_50" << std::endl
69 <<
" 24: TAG_ARUCO_7x7_100" << std::endl
70 <<
" 25: TAG_ARUCO_7x7_250" << std::endl
71 <<
" 26: TAG_ARUCO_7x7_1000" << std::endl
72 <<
" 27: TAG_ARUCO_MIP_36h12" << std::endl
73 <<
" Default: 0 (36h11)" << std::endl
75 <<
" --tag-decision-margin-threshold <threshold>" << std::endl
76 <<
" Threshold used to discard low-confident detections. A typical value is " << std::endl
77 <<
" around 100. The higher this value, the more false positives will be filtered" << std::endl
78 <<
" out. When this value is set to -1, false positives are not filtered out." << std::endl
79 <<
" Default: 50" << std::endl
81 <<
" --tag-hamming-distance-threshold <threshold>" << std::endl
82 <<
" Threshold used to discard low-confident detections with corrected bits." << std::endl
83 <<
" A typical value is between 0 and 3. The lower this value, the more false" << std::endl
84 <<
" positives will be filtered out." << std::endl
85 <<
" Default: 0" << std::endl
87 <<
" --tag-quad-decimate <factor>" << std::endl
88 <<
" Decimation factor used to detect a tag. " << std::endl
89 <<
" Default: 1" << std::endl
91 <<
" --tag-n-threads <number>" << std::endl
92 <<
" Number of threads used to detect a tag." << std::endl
93 <<
" Default: 1" << std::endl
95 <<
" --tag-z-aligned" << std::endl
96 <<
" When enabled, tag z-axis and camera z-axis are aligned." << std::endl
97 <<
" Default: false" << std::endl
99 <<
" --tag-pose-method <method>" << std::endl
100 <<
" Algorithm used to compute the tag pose from its 4 corners." << std::endl
101 <<
" Possible values are:" << std::endl
102 <<
" 0: HOMOGRAPHY" << std::endl
103 <<
" 1: HOMOGRAPHY_VIRTUAL_VS" << std::endl
104 <<
" 2: DEMENTHON_VIRTUAL_VS" << std::endl
105 <<
" 3: LAGRANGE_VIRTUAL_VS" << std::endl
106 <<
" 4: BEST_RESIDUAL_VIRTUAL_VS" << std::endl
107 <<
" 5: HOMOGRAPHY_ORTHOGONAL_ITERATION" << std::endl
108 <<
" Default: 1 (HOMOGRAPHY_VIRTUAL_VS)" << std::endl
110#if defined(VISP_HAVE_DISPLAY)
111 <<
" --display-tag" << std::endl
112 <<
" Flag used to enable displaying the edges of a tag." << std::endl
113 <<
" Default: disabled" << std::endl
115 <<
" --display-off" << std::endl
116 <<
" Flag used to turn display off." << std::endl
117 <<
" Default: enabled" << std::endl
119 <<
" --color <id>" << std::endl
120 <<
" Color id used to display the frame over each tag." << std::endl
121 <<
" Possible values are:" << std::endl
122 <<
" -1: R-G-B colors for X, Y, Z axis respectively" << std::endl
123 <<
" 0: all axis in black" << std::endl
124 <<
" 1: all axis in white" << std::endl
125 <<
" ..." << std::endl
126 <<
" Default: -1" << std::endl
128 <<
" --thickness <thickness>" << std::endl
129 <<
" Thickness of the drawings in overlay." << std::endl
130 <<
" Default: 2" << std::endl
133 <<
" --verbose, -v" << std::endl
134 <<
" Enable extra verbosity." << std::endl
136 <<
" --help, -h" << std::endl
137 <<
" Print this helper message." << std::endl
141 std::cout <<
"Error" << std::endl
143 <<
"Unsupported parameter " << argv[
error] << std::endl;
147int main(
int argc,
const char **argv)
150#if defined(VISP_HAVE_APRILTAG) && defined(VISP_HAVE_REALSENSE2)
152#ifdef ENABLE_VISP_NAMESPACE
158 double opt_tag_size = 0.053;
159 float opt_tag_quad_decimate = 1.0;
160 float opt_tag_decision_margin_threshold = 50;
161 int opt_tag_hamming_distance_threshold = 2;
162 int opt_tag_nThreads = 1;
163 bool opt_display_tag =
false;
164 int opt_color_id = -1;
165 unsigned int opt_thickness = 2;
166 bool opt_tag_z_align_frame =
false;
167 bool opt_verbose =
false;
169#if !(defined(VISP_HAVE_DISPLAY))
170 bool opt_display_off =
true;
171 std::cout <<
"Warning: There is no 3rd party to display images..." << std::endl;
173 bool opt_display_off =
false;
176 for (
int i = 1;
i < argc; ++
i) {
177 if (std::string(argv[i]) ==
"--tag-size" && i + 1 < argc) {
178 opt_tag_size = atof(argv[++i]);
180 else if (std::string(argv[i]) ==
"--tag-family" && i + 1 < argc) {
183 else if (std::string(argv[i]) ==
"--tag-quad-decimate" && i + 1 < argc) {
184 opt_tag_quad_decimate =
static_cast<float>(atof(argv[++i]));
186 else if (std::string(argv[i]) ==
"--tag-n-threads" && i + 1 < argc) {
187 opt_tag_nThreads = atoi(argv[++i]);
189 else if (std::string(argv[i]) ==
"--tag-z-aligned") {
190 opt_tag_z_align_frame =
true;
192 else if (std::string(argv[i]) ==
"--tag-pose-method" && i + 1 < argc) {
195 else if (std::string(argv[i]) ==
"--tag-decision-margin-threshold" && i + 1 < argc) {
196 opt_tag_decision_margin_threshold =
static_cast<float>(atof(argv[++i]));
198 else if (std::string(argv[i]) ==
"--tag-hamming-distance-threshold" && i + 1 < argc) {
199 opt_tag_hamming_distance_threshold = atoi(argv[++i]);
201#if defined(VISP_HAVE_DISPLAY)
202 else if (std::string(argv[i]) ==
"--display-tag") {
203 opt_display_tag =
true;
205 else if (std::string(argv[i]) ==
"--display-off") {
206 opt_display_off =
true;
208 else if (std::string(argv[i]) ==
"--color" && i + 1 < argc) {
209 opt_color_id = atoi(argv[++i]);
211 else if (std::string(argv[i]) ==
"--thickness" && i + 1 < argc) {
212 opt_thickness =
static_cast<unsigned int>(atoi(argv[++i]));
215 else if (std::string(argv[i]) ==
"--verbose" || std::string(argv[i]) ==
"-v") {
218 else if (std::string(argv[i]) ==
"--help" || std::string(argv[i]) ==
"-h") {
228#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
229 std::shared_ptr<vpDisplay> d1, d2, d3;
238 std::cout <<
"Use Realsense 2 grabber" << std::endl;
242 config.enable_stream(RS2_STREAM_COLOR,
static_cast<int>(width),
static_cast<int>(height), RS2_FORMAT_RGBA8, 30);
243 config.enable_stream(RS2_STREAM_DEPTH,
static_cast<int>(width),
static_cast<int>(height), RS2_FORMAT_Z16, 30);
244 config.enable_stream(RS2_STREAM_INFRARED,
static_cast<int>(width),
static_cast<int>(height), RS2_FORMAT_Y8, 30);
253 std::cout <<
"I_color: " << I_color.getWidth() <<
" " << I_color.getHeight() << std::endl;
254 std::cout <<
"I_depth_raw: " << I_depth_raw.getWidth() <<
" " << I_depth_raw.getHeight() << std::endl;
256 rs2::align align_to_color = RS2_STREAM_COLOR;
257 g.
acquire(
reinterpret_cast<unsigned char *
>(I_color.bitmap),
reinterpret_cast<unsigned char *
>(I_depth_raw.bitmap),
258 nullptr,
nullptr, &align_to_color);
260 std::cout <<
"Read camera parameters from Realsense device" << std::endl;
265 std::cout <<
cam << std::endl;
266 std::cout <<
"Tag detector settings" << std::endl;
267 std::cout <<
" Tag size [m] : " << opt_tag_size << std::endl;
268 std::cout <<
" Tag family : " << opt_tag_family << std::endl;
269 std::cout <<
" Quad decimate : " << opt_tag_quad_decimate << std::endl;
270 std::cout <<
" Decision margin threshold : " << opt_tag_decision_margin_threshold << std::endl;
271 std::cout <<
" Hamming distance threshold: " << opt_tag_hamming_distance_threshold << std::endl;
272 std::cout <<
" Num threads : " << opt_tag_nThreads << std::endl;
273 std::cout <<
" Z aligned : " << opt_tag_z_align_frame << std::endl;
274 std::cout <<
" Pose estimation : " << opt_tag_pose_estimation_method << std::endl;
280 if (!opt_display_off) {
281#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
297 detector.setAprilTagQuadDecimate(opt_tag_quad_decimate);
298 detector.setAprilTagPoseEstimationMethod(opt_tag_pose_estimation_method);
299 detector.setAprilTagNbThreads(opt_tag_nThreads);
301 detector.setZAlignedWithCameraAxis(opt_tag_z_align_frame);
302 detector.setAprilTagDecisionMarginThreshold(opt_tag_decision_margin_threshold);
303 detector.setAprilTagHammingDistanceThreshold(opt_tag_hamming_distance_threshold);
305 std::vector<double> time_vec, time_vec_detection;
310 g.
acquire(
reinterpret_cast<unsigned char *
>(I_color.bitmap),
311 reinterpret_cast<unsigned char *
>(I_depth_raw.bitmap),
nullptr,
nullptr, &align_to_color);
318 depthMap.
resize(I_depth_raw.getHeight(), I_depth_raw.getWidth());
319#ifdef VISP_HAVE_OPENMP
320#pragma omp parallel for
322 for (
int i = 0; i < static_cast<int>(I_depth_raw.getHeight());
i++) {
323 for (
int j = 0; j < static_cast<int>(I_depth_raw.getWidth());
j++) {
324 if (I_depth_raw[i][j]) {
339 std::vector<vpHomogeneousMatrix> cMo_vec;
340 detector.detect(I, opt_tag_size, cam, cMo_vec);
342 time_vec_detection.push_back(t_detection);
345 std::vector<std::vector<vpImagePoint> > tagsCorners = detector.getTagsCorners();
346 detector.displayTags(I_color, tagsCorners,
vpColor::none, opt_thickness);
347 detector.displayFrames(I_color, cMo_vec, cam, opt_tag_size / 2,
vpColor::none, opt_thickness);
348 detector.displayTags(I_color2, tagsCorners,
vpColor::none, opt_thickness);
349 detector.displayFrames(I_color2, cMo_vec, cam, opt_tag_size / 2,
vpColor::none, opt_thickness);
352 std::vector<std::vector<vpImagePoint> > tags_corners = detector.getPolygon();
353 std::vector<int> tags_id = detector.getTagsId();
354 std::map<int, double> tags_size;
355 tags_size[-1] = opt_tag_size;
356 std::vector<std::vector<vpPoint> > tags_points3d = detector.getTagsPoints3D(tags_id, tags_size);
357 for (
size_t i = 0;
i < tags_corners.size();
i++) {
359 double confidence_index;
361 &confidence_index)) {
362 if (confidence_index > 0.5) {
365 else if (confidence_index > 0.25) {
371 std::stringstream ss;
372 ss <<
"Tag id " << tags_id[
i] <<
" confidence: " << confidence_index;
376 std::cout << ss.str() << std::endl;
377 std::cout <<
"cMo[" <<
i <<
"]: \n" << cMo_vec[
i] << std::endl;
378 std::cout <<
"cMo[" <<
i <<
"] using depth: \n" <<
cMo << std::endl;
392 time_vec.push_back(t);
394 std::stringstream ss;
395 ss <<
"Detection time: " <<
t <<
" ms for " << detector.getNbObjects() <<
" tags";
406 std::cout <<
"Benchmark loop processing time" << std::endl;
407 std::cout <<
"Mean / Median / Std: " <<
vpMath::getMean(time_vec) <<
" ms"
411 std::cout <<
"Benchmark detection processing time" << std::endl;
412 std::cout <<
"Mean / Median / Std: " <<
vpMath::getMean(time_vec_detection) <<
" ms"
417 std::cerr <<
"Catch an exception: " <<
e.getMessage() << std::endl;
420#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
421 if (!opt_display_off) {
438#ifndef VISP_HAVE_APRILTAG
439 std::cout <<
"Enable Apriltag support, configure and build ViSP to run this tutorial" << std::endl;
441 std::cout <<
"Install librealsense 3rd party, configure and build ViSP again to use this example" << std::endl;
Generic class defining intrinsic camera parameters.
@ perspectiveProjWithoutDistortion
Perspective projection without distortion model.
static vpColor getColor(const unsigned int &i)
static const vpColor none
static const vpColor orange
@ TAG_36h11
AprilTag 36h11 pattern (recommended).
Class that defines generic functionalities for display.
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void displayFrame(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, double size, const vpColor &color=vpColor::none, unsigned int thickness=1, const vpImagePoint &offset=vpImagePoint(0, 0), const std::string &frameName="", const vpColor &textColor=vpColor::black, const vpImagePoint &textOffset=vpImagePoint(15, 15))
static void flush(const vpImage< unsigned char > &I)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
error that can be emitted by ViSP classes.
Implementation of an homogeneous matrix and operations on such kind of matrices.
static void createDepthHistogram(const vpImage< uint16_t > &src_depth, vpImage< vpRGBa > &dest_rgba)
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
Definition of the vpImage class member functions.
void resize(unsigned int h, unsigned int w)
resize the image : Image initialization
static double getMedian(const std::vector< double > &v)
static double getStdev(const std::vector< double > &v, bool useBesselCorrection=false)
static double getMean(const std::vector< double > &v)
static bool computePlanarObjectPoseFromRGBD(const vpImage< float > &depthMap, const std::vector< vpImagePoint > &corners, const vpCameraParameters &colorIntrinsics, const std::vector< vpPoint > &point3d, vpHomogeneousMatrix &cMo, double *confidence_index=nullptr)
vpCameraParameters getCameraParameters(const rs2_stream &stream, vpCameraParameters::vpCameraParametersProjType type=vpCameraParameters::perspectiveProjWithDistortion, int index=-1) const
void acquire(vpImage< unsigned char > &grey, double *ts=nullptr)
bool open(const rs2::config &cfg=rs2::config())
std::shared_ptr< vpDisplay > createDisplay()
Return a smart pointer vpDisplay specialization if a GUI library is available or nullptr otherwise.
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.
VISP_EXPORT double measureTimeMs()