Visual Servoing Platform  version 3.6.1 under development (2024-03-29)
vpDetectorAprilTag.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See https://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Base class for AprilTag detection.
33  *
34 *****************************************************************************/
35 #include <visp3/core/vpConfig.h>
36 
37 #ifdef VISP_HAVE_APRILTAG
38 #include <map>
39 
40 #include <apriltag.h>
41 #include <apriltag_pose.h>
42 #include <common/homography.h>
43 #include <tag16h5.h>
44 #include <tag25h7.h>
45 #include <tag25h9.h>
46 #include <tag36h10.h>
47 #include <tag36h11.h>
48 #include <tagCircle21h7.h>
49 #include <tagStandard41h12.h>
50 #include <visp3/detection/vpDetectorAprilTag.h>
51 #if defined(VISP_HAVE_APRILTAG_BIG_FAMILY)
52 #include <tagCircle49h12.h>
53 #include <tagCustom48h12.h>
54 #include <tagStandard41h12.h>
55 #include <tagStandard52h13.h>
56 #endif
57 
58 #include <visp3/core/vpDisplay.h>
59 #include <visp3/core/vpPixelMeterConversion.h>
60 #include <visp3/core/vpPoint.h>
61 #include <visp3/vision/vpPose.h>
62 
63 #ifndef DOXYGEN_SHOULD_SKIP_THIS
64 class vpDetectorAprilTag::Impl
65 {
66 public:
67  Impl(const vpAprilTagFamily &tagFamily, const vpPoseEstimationMethod &method)
68  : m_poseEstimationMethod(method), m_tagsId(), m_tagFamily(tagFamily), m_td(nullptr), m_tf(nullptr), m_detections(nullptr),
69  m_zAlignedWithCameraFrame(false)
70  {
71  switch (m_tagFamily) {
72  case TAG_36h11:
73  m_tf = tag36h11_create();
74  break;
75 
76  case TAG_36h10:
77  m_tf = tag36h10_create();
78  break;
79 
80  case TAG_36ARTOOLKIT:
81  break;
82 
83  case TAG_25h9:
84  m_tf = tag25h9_create();
85  break;
86 
87  case TAG_25h7:
88  m_tf = tag25h7_create();
89  break;
90 
91  case TAG_16h5:
92  m_tf = tag16h5_create();
93  break;
94 
95  case TAG_CIRCLE21h7:
96  m_tf = tagCircle21h7_create();
97  break;
98 
99  case TAG_CIRCLE49h12:
100 #if defined(VISP_HAVE_APRILTAG_BIG_FAMILY)
101  m_tf = tagCircle49h12_create();
102 #endif
103  break;
104 
105  case TAG_CUSTOM48h12:
106 #if defined(VISP_HAVE_APRILTAG_BIG_FAMILY)
107  m_tf = tagCustom48h12_create();
108 #endif
109  break;
110 
111  case TAG_STANDARD52h13:
112 #if defined(VISP_HAVE_APRILTAG_BIG_FAMILY)
113  m_tf = tagStandard52h13_create();
114 #endif
115  break;
116 
117  case TAG_STANDARD41h12:
118 #if defined(VISP_HAVE_APRILTAG_BIG_FAMILY)
119  m_tf = tagStandard41h12_create();
120 #endif
121  break;
122 
123  default:
124  throw vpException(vpException::fatalError, "Unknown Tag family!");
125  }
126 
127  if (m_tagFamily != TAG_36ARTOOLKIT && m_tf) {
128  m_td = apriltag_detector_create();
129  apriltag_detector_add_family(m_td, m_tf);
130  }
131 
132  m_mapOfCorrespondingPoseMethods[DEMENTHON_VIRTUAL_VS] = vpPose::DEMENTHON;
133  m_mapOfCorrespondingPoseMethods[LAGRANGE_VIRTUAL_VS] = vpPose::LAGRANGE;
134  }
135 
136  Impl(const Impl &o)
137  : m_poseEstimationMethod(o.m_poseEstimationMethod), m_tagsId(o.m_tagsId), m_tagFamily(o.m_tagFamily), m_td(nullptr),
138  m_tf(nullptr), m_detections(nullptr), m_zAlignedWithCameraFrame(o.m_zAlignedWithCameraFrame)
139  {
140  switch (m_tagFamily) {
141  case TAG_36h11:
142  m_tf = tag36h11_create();
143  break;
144 
145  case TAG_36h10:
146  m_tf = tag36h10_create();
147  break;
148 
149  case TAG_36ARTOOLKIT:
150  break;
151 
152  case TAG_25h9:
153  m_tf = tag25h9_create();
154  break;
155 
156  case TAG_25h7:
157  m_tf = tag25h7_create();
158  break;
159 
160  case TAG_16h5:
161  m_tf = tag16h5_create();
162  break;
163 
164  case TAG_CIRCLE21h7:
165  m_tf = tagCircle21h7_create();
166  break;
167 
168  case TAG_CIRCLE49h12:
169 #if defined(VISP_HAVE_APRILTAG_BIG_FAMILY)
170  m_tf = tagCircle49h12_create();
171 #endif
172  break;
173 
174  case TAG_CUSTOM48h12:
175 #if defined(VISP_HAVE_APRILTAG_BIG_FAMILY)
176  m_tf = tagCustom48h12_create();
177 #endif
178  break;
179 
180  case TAG_STANDARD52h13:
181 #if defined(VISP_HAVE_APRILTAG_BIG_FAMILY)
182  m_tf = tagStandard52h13_create();
183 #endif
184  break;
185 
186  case TAG_STANDARD41h12:
187 #if defined(VISP_HAVE_APRILTAG_BIG_FAMILY)
188  m_tf = tagStandard41h12_create();
189 #endif
190  break;
191 
192  default:
193  throw vpException(vpException::fatalError, "Unknown Tag family!");
194  }
195 
196  if (m_tagFamily != TAG_36ARTOOLKIT && m_tf) {
197  m_td = apriltag_detector_create();
198  apriltag_detector_add_family(m_td, m_tf);
199  }
200 
201  m_mapOfCorrespondingPoseMethods[DEMENTHON_VIRTUAL_VS] = vpPose::DEMENTHON;
202  m_mapOfCorrespondingPoseMethods[LAGRANGE_VIRTUAL_VS] = vpPose::LAGRANGE;
203 
204  if (o.m_detections != nullptr) {
205  m_detections = apriltag_detections_copy(o.m_detections);
206  }
207  }
208 
209  ~Impl()
210  {
211  if (m_td) {
212  apriltag_detector_destroy(m_td);
213  }
214 
215  if (m_tf) {
216  switch (m_tagFamily) {
217  case TAG_36h11:
218  tag36h11_destroy(m_tf);
219  break;
220 
221  case TAG_36h10:
222  tag36h10_destroy(m_tf);
223  break;
224 
225  case TAG_36ARTOOLKIT:
226  break;
227 
228  case TAG_25h9:
229  tag25h9_destroy(m_tf);
230  break;
231 
232  case TAG_25h7:
233  tag25h7_destroy(m_tf);
234  break;
235 
236  case TAG_16h5:
237  tag16h5_destroy(m_tf);
238  break;
239 
240  case TAG_CIRCLE21h7:
241  tagCircle21h7_destroy(m_tf);
242  break;
243 
244  case TAG_CIRCLE49h12:
245 #if defined(VISP_HAVE_APRILTAG_BIG_FAMILY)
246  tagCustom48h12_destroy(m_tf);
247 #endif
248  break;
249 
250  case TAG_CUSTOM48h12:
251 #if defined(VISP_HAVE_APRILTAG_BIG_FAMILY)
252  tagCustom48h12_destroy(m_tf);
253 #endif
254  break;
255 
256  case TAG_STANDARD52h13:
257 #if defined(VISP_HAVE_APRILTAG_BIG_FAMILY)
258  tagStandard52h13_destroy(m_tf);
259 #endif
260  break;
261 
262  case TAG_STANDARD41h12:
263 #if defined(VISP_HAVE_APRILTAG_BIG_FAMILY)
264  tagStandard41h12_destroy(m_tf);
265 #endif
266  break;
267 
268  default:
269  break;
270  }
271  }
272 
273  if (m_detections) {
274  apriltag_detections_destroy(m_detections);
275  m_detections = nullptr;
276  }
277  }
278 
279  void convertHomogeneousMatrix(const apriltag_pose_t &pose, vpHomogeneousMatrix &cMo)
280  {
281  for (unsigned int i = 0; i < 3; i++) {
282  for (unsigned int j = 0; j < 3; j++) {
283  cMo[i][j] = MATD_EL(pose.R, i, j);
284  }
285  cMo[i][3] = MATD_EL(pose.t, i, 0);
286  }
287  }
288 
289  bool detect(const vpImage<unsigned char> &I, double tagSize, const vpCameraParameters &cam,
290  std::vector<std::vector<vpImagePoint> > &polygons, std::vector<std::string> &messages, bool displayTag,
291  const vpColor color, unsigned int thickness, std::vector<vpHomogeneousMatrix> *cMo_vec,
292  std::vector<vpHomogeneousMatrix> *cMo_vec2, std::vector<double> *projErrors,
293  std::vector<double> *projErrors2)
294  {
295  if (m_tagFamily == TAG_36ARTOOLKIT) {
296  // TAG_36ARTOOLKIT is not available anymore
297  std::cerr << "TAG_36ARTOOLKIT detector is not available anymore." << std::endl;
298  return false;
299  }
300 #if !defined(VISP_HAVE_APRILTAG_BIG_FAMILY)
303  std::cerr << "TAG_CIRCLE49h12, TAG_CUSTOM48h12, TAG_STANDARD41h12 and TAG_STANDARD52h13 are disabled."
304  << std::endl;
305  return false;
306  }
307 #endif
308 
309  const bool computePose = (cMo_vec != nullptr);
310 
311  image_u8_t im = {/*.width =*/(int32_t)I.getWidth(),
312  /*.height =*/(int32_t)I.getHeight(),
313  /*.stride =*/(int32_t)I.getWidth(),
314  /*.buf =*/I.bitmap };
315 
316  if (m_detections) {
317  apriltag_detections_destroy(m_detections);
318  m_detections = nullptr;
319  }
320 
321  m_detections = apriltag_detector_detect(m_td, &im);
322  int nb_detections = zarray_size(m_detections);
323  bool detected = nb_detections > 0;
324 
325  polygons.resize(static_cast<size_t>(nb_detections));
326  messages.resize(static_cast<size_t>(nb_detections));
327  m_tagsId.resize(static_cast<size_t>(nb_detections));
328 
329  for (int i = 0; i < zarray_size(m_detections); i++) {
330  apriltag_detection_t *det;
331  zarray_get(m_detections, i, &det);
332 
333  std::vector<vpImagePoint> polygon;
334  for (int j = 0; j < 4; j++) {
335  polygon.push_back(vpImagePoint(det->p[j][1], det->p[j][0]));
336  }
337  polygons[static_cast<size_t>(i)] = polygon;
338  std::stringstream ss;
339  ss << m_tagFamily << " id: " << det->id;
340  messages[static_cast<size_t>(i)] = ss.str();
341  m_tagsId[static_cast<size_t>(i)] = det->id;
342 
343  if (displayTag) {
344  vpColor Ox = (color == vpColor::none) ? vpColor::red : color;
345  vpColor Oy = (color == vpColor::none) ? vpColor::green : color;
346  vpColor Ox2 = (color == vpColor::none) ? vpColor::yellow : color;
347  vpColor Oy2 = (color == vpColor::none) ? vpColor::blue : color;
348 
349  vpDisplay::displayLine(I, (int)det->p[0][1], (int)det->p[0][0], (int)det->p[1][1], (int)det->p[1][0], Ox,
350  thickness);
351  vpDisplay::displayLine(I, (int)det->p[0][1], (int)det->p[0][0], (int)det->p[3][1], (int)det->p[3][0], Oy,
352  thickness);
353  vpDisplay::displayLine(I, (int)det->p[1][1], (int)det->p[1][0], (int)det->p[2][1], (int)det->p[2][0], Ox2,
354  thickness);
355  vpDisplay::displayLine(I, (int)det->p[2][1], (int)det->p[2][0], (int)det->p[3][1], (int)det->p[3][0], Oy2,
356  thickness);
357  }
358 
359  if (computePose) {
360  vpHomogeneousMatrix cMo, cMo2;
361  double err1, err2;
362  if (getPose(static_cast<size_t>(i), tagSize, cam, cMo, cMo_vec2 ? &cMo2 : nullptr, projErrors ? &err1 : nullptr,
363  projErrors2 ? &err2 : nullptr)) {
364  cMo_vec->push_back(cMo);
365  if (cMo_vec2) {
366  cMo_vec2->push_back(cMo2);
367  }
368  if (projErrors) {
369  projErrors->push_back(err1);
370  }
371  if (projErrors2) {
372  projErrors2->push_back(err2);
373  }
374  }
375  // else case should never happen
376  }
377  }
378 
379  return detected;
380  }
381 
382  void displayFrames(const vpImage<unsigned char> &I, const std::vector<vpHomogeneousMatrix> &cMo_vec,
383  const vpCameraParameters &cam, double size, const vpColor &color, unsigned int thickness) const
384  {
385  for (size_t i = 0; i < cMo_vec.size(); i++) {
386  const vpHomogeneousMatrix &cMo = cMo_vec[i];
387  vpDisplay::displayFrame(I, cMo, cam, size, color, thickness);
388  }
389  }
390 
391  void displayFrames(const vpImage<vpRGBa> &I, const std::vector<vpHomogeneousMatrix> &cMo_vec,
392  const vpCameraParameters &cam, double size, const vpColor &color, unsigned int thickness) const
393  {
394  for (size_t i = 0; i < cMo_vec.size(); i++) {
395  const vpHomogeneousMatrix &cMo = cMo_vec[i];
396  vpDisplay::displayFrame(I, cMo, cam, size, color, thickness);
397  }
398  }
399 
400  void displayTags(const vpImage<unsigned char> &I, const std::vector<std::vector<vpImagePoint> > &tagsCorners,
401  const vpColor &color, unsigned int thickness) const
402  {
403  for (size_t i = 0; i < tagsCorners.size(); i++) {
404  const vpColor Ox = (color == vpColor::none) ? vpColor::red : color;
405  const vpColor Oy = (color == vpColor::none) ? vpColor::green : color;
406  const vpColor Ox2 = (color == vpColor::none) ? vpColor::yellow : color;
407  const vpColor Oy2 = (color == vpColor::none) ? vpColor::blue : color;
408 
409  const std::vector<vpImagePoint> &corners = tagsCorners[i];
410  assert(corners.size() == 4);
411 
412  vpDisplay::displayLine(I, (int)corners[0].get_i(), (int)corners[0].get_j(), (int)corners[1].get_i(), (int)corners[1].get_j(),
413  Ox, thickness);
414  vpDisplay::displayLine(I, (int)corners[0].get_i(), (int)corners[0].get_j(), (int)corners[3].get_i(), (int)corners[3].get_j(),
415  Oy, thickness);
416  vpDisplay::displayLine(I, (int)corners[1].get_i(), (int)corners[1].get_j(), (int)corners[2].get_i(), (int)corners[2].get_j(),
417  Ox2, thickness);
418  vpDisplay::displayLine(I, (int)corners[2].get_i(), (int)corners[2].get_j(), (int)corners[3].get_i(), (int)corners[3].get_j(),
419  Oy2, thickness);
420  }
421  }
422 
423  void displayTags(const vpImage<vpRGBa> &I, const std::vector<std::vector<vpImagePoint> > &tagsCorners,
424  const vpColor &color, unsigned int thickness) const
425  {
426  for (size_t i = 0; i < tagsCorners.size(); i++) {
427  const vpColor Ox = (color == vpColor::none) ? vpColor::red : color;
428  const vpColor Oy = (color == vpColor::none) ? vpColor::green : color;
429  const vpColor Ox2 = (color == vpColor::none) ? vpColor::yellow : color;
430  const vpColor Oy2 = (color == vpColor::none) ? vpColor::blue : color;
431 
432  const std::vector<vpImagePoint> &corners = tagsCorners[i];
433  assert(corners.size() == 4);
434 
435  vpDisplay::displayLine(I, (int)corners[0].get_i(), (int)corners[0].get_j(), (int)corners[1].get_i(), (int)corners[1].get_j(),
436  Ox, thickness);
437  vpDisplay::displayLine(I, (int)corners[0].get_i(), (int)corners[0].get_j(), (int)corners[3].get_i(), (int)corners[3].get_j(),
438  Oy, thickness);
439  vpDisplay::displayLine(I, (int)corners[1].get_i(), (int)corners[1].get_j(), (int)corners[2].get_i(), (int)corners[2].get_j(),
440  Ox2, thickness);
441  vpDisplay::displayLine(I, (int)corners[2].get_i(), (int)corners[2].get_j(), (int)corners[3].get_i(), (int)corners[3].get_j(),
442  Oy2, thickness);
443  }
444  }
445 
446  bool getPose(size_t tagIndex, double tagSize, const vpCameraParameters &cam, vpHomogeneousMatrix &cMo,
447  vpHomogeneousMatrix *cMo2, double *projErrors, double *projErrors2)
448  {
449  if (m_detections == nullptr) {
450  throw(vpException(vpException::fatalError, "Cannot get tag index=%d pose: detection empty", tagIndex));
451  }
452  if (m_tagFamily == TAG_36ARTOOLKIT) {
453  // TAG_36ARTOOLKIT is not available anymore
454  std::cerr << "TAG_36ARTOOLKIT detector is not available anymore." << std::endl;
455  return false;
456  }
457 #if !defined(VISP_HAVE_APRILTAG_BIG_FAMILY)
460  std::cerr << "TAG_CIRCLE49h12, TAG_CUSTOM48h12, TAG_STANDARD41h12 and TAG_STANDARD52h13 are disabled."
461  << std::endl;
462  return false;
463  }
464 #endif
465 
466  apriltag_detection_t *det;
467  zarray_get(m_detections, static_cast<int>(tagIndex), &det);
468 
469  int nb_detections = zarray_size(m_detections);
470  if (tagIndex >= (size_t)nb_detections) {
471  return false;
472  }
473 
474  // In AprilTag3, estimate_pose_for_tag_homography() and estimate_tag_pose() have been added.
475  // They use a tag frame aligned with the camera frame
476  // Before the release of AprilTag3, convention used was to define the z-axis of the tag going upward.
477  // To keep compatibility, we maintain the same convention than before and there is setZAlignedWithCameraAxis().
478  // Under the hood, we use aligned frames everywhere and transform the pose according to the option.
479 
480  vpHomogeneousMatrix cMo_homography_ortho_iter;
483  double fx = cam.get_px(), fy = cam.get_py();
484  double cx = cam.get_u0(), cy = cam.get_v0();
485 
486  apriltag_detection_info_t info;
487  info.det = det;
488  info.tagsize = tagSize;
489  info.fx = fx;
490  info.fy = fy;
491  info.cx = cx;
492  info.cy = cy;
493 
494  // projErrors and projErrors2 will be override later
495  getPoseWithOrthogonalMethod(info, cMo, cMo2, projErrors, projErrors2);
496  cMo_homography_ortho_iter = cMo;
497  }
498 
499  vpHomogeneousMatrix cMo_homography;
502  double fx = cam.get_px(), fy = cam.get_py();
503  double cx = cam.get_u0(), cy = cam.get_v0();
504 
505  apriltag_detection_info_t info;
506  info.det = det;
507  info.tagsize = tagSize;
508  info.fx = fx;
509  info.fy = fy;
510  info.cx = cx;
511  info.cy = cy;
512 
513  apriltag_pose_t pose;
514  estimate_pose_for_tag_homography(&info, &pose);
515  convertHomogeneousMatrix(pose, cMo);
516 
517  matd_destroy(pose.R);
518  matd_destroy(pose.t);
519 
520  cMo_homography = cMo;
521  }
522 
523  // Add marker object points
524  vpPose pose;
525  vpPoint pt;
526 
527  vpImagePoint imPt;
528  double x = 0.0, y = 0.0;
529  std::vector<vpPoint> pts(4);
530  pt.setWorldCoordinates(-tagSize / 2.0, tagSize / 2.0, 0.0);
531  imPt.set_uv(det->p[0][0], det->p[0][1]);
532  vpPixelMeterConversion::convertPoint(cam, imPt, x, y);
533  pt.set_x(x);
534  pt.set_y(y);
535  pts[0] = pt;
536 
537  pt.setWorldCoordinates(tagSize / 2.0, tagSize / 2.0, 0.0);
538  imPt.set_uv(det->p[1][0], det->p[1][1]);
539  vpPixelMeterConversion::convertPoint(cam, imPt, x, y);
540  pt.set_x(x);
541  pt.set_y(y);
542  pts[1] = pt;
543 
544  pt.setWorldCoordinates(tagSize / 2.0, -tagSize / 2.0, 0.0);
545  imPt.set_uv(det->p[2][0], det->p[2][1]);
546  vpPixelMeterConversion::convertPoint(cam, imPt, x, y);
547  pt.set_x(x);
548  pt.set_y(y);
549  pts[2] = pt;
550 
551  pt.setWorldCoordinates(-tagSize / 2.0, -tagSize / 2.0, 0.0);
552  imPt.set_uv(det->p[3][0], det->p[3][1]);
553  vpPixelMeterConversion::convertPoint(cam, imPt, x, y);
554  pt.set_x(x);
555  pt.set_y(y);
556  pts[3] = pt;
557 
558  pose.addPoints(pts);
559 
563  vpHomogeneousMatrix cMo_dementhon, cMo_lagrange;
564 
565  double residual_dementhon = std::numeric_limits<double>::max(),
566  residual_lagrange = std::numeric_limits<double>::max();
567  double residual_homography = pose.computeResidual(cMo_homography);
568  double residual_homography_ortho_iter = pose.computeResidual(cMo_homography_ortho_iter);
569 
570  if (pose.computePose(vpPose::DEMENTHON, cMo_dementhon)) {
571  residual_dementhon = pose.computeResidual(cMo_dementhon);
572  }
573 
574  if (pose.computePose(vpPose::LAGRANGE, cMo_lagrange)) {
575  residual_lagrange = pose.computeResidual(cMo_lagrange);
576  }
577 
578  std::vector<double> residuals;
579  residuals.push_back(residual_dementhon);
580  residuals.push_back(residual_lagrange);
581  residuals.push_back(residual_homography);
582  residuals.push_back(residual_homography_ortho_iter);
583  std::vector<vpHomogeneousMatrix> poses;
584  poses.push_back(cMo_dementhon);
585  poses.push_back(cMo_lagrange);
586  poses.push_back(cMo_homography);
587  poses.push_back(cMo_homography_ortho_iter);
588 
589  std::ptrdiff_t minIndex = std::min_element(residuals.begin(), residuals.end()) - residuals.begin();
590  cMo = *(poses.begin() + minIndex);
591  }
592  else {
593  pose.computePose(m_mapOfCorrespondingPoseMethods[m_poseEstimationMethod], cMo);
594  }
595  }
596 
598  // Compute final pose using VVS
599  pose.computePose(vpPose::VIRTUAL_VS, cMo);
600  }
601 
602  // Only with HOMOGRAPHY_ORTHOGONAL_ITERATION we can directly get two solutions
604  if (cMo2) {
605  double scale = tagSize / 2.0;
606  double data_p0[] = { -scale, scale, 0 };
607  double data_p1[] = { scale, scale, 0 };
608  double data_p2[] = { scale, -scale, 0 };
609  double data_p3[] = { -scale, -scale, 0 };
610  matd_t *p[4] = { matd_create_data(3, 1, data_p0), matd_create_data(3, 1, data_p1),
611  matd_create_data(3, 1, data_p2), matd_create_data(3, 1, data_p3) };
612  matd_t *v[4];
613  for (int i = 0; i < 4; i++) {
614  double data_v[] = { (det->p[i][0] - cam.get_u0()) / cam.get_px(), (det->p[i][1] - cam.get_v0()) / cam.get_py(),
615  1 };
616  v[i] = matd_create_data(3, 1, data_v);
617  }
618 
619  apriltag_pose_t solution1, solution2;
620  const int nIters = 50;
621  solution1.R = matd_create_data(3, 3, cMo.getRotationMatrix().data);
622  solution1.t = matd_create_data(3, 1, cMo.getTranslationVector().data);
623 
624  double err2;
625  get_second_solution(v, p, &solution1, &solution2, nIters, &err2);
626 
627  for (int i = 0; i < 4; i++) {
628  matd_destroy(p[i]);
629  matd_destroy(v[i]);
630  }
631 
632  if (solution2.R) {
633  convertHomogeneousMatrix(solution2, *cMo2);
634 
635  matd_destroy(solution2.R);
636  matd_destroy(solution2.t);
637  }
638 
639  matd_destroy(solution1.R);
640  matd_destroy(solution1.t);
641  }
642  }
643 
644  // Compute projection error with vpPose::computeResidual() for consistency
645  if (projErrors) {
646  *projErrors = pose.computeResidual(cMo);
647  }
648  if (projErrors2 && cMo2) {
649  *projErrors2 = pose.computeResidual(*cMo2);
650  }
651 
652  if (!m_zAlignedWithCameraFrame) {
654  // Apply a rotation of 180deg around x axis
655  oMo[0][0] = 1;
656  oMo[0][1] = 0;
657  oMo[0][2] = 0;
658  oMo[1][0] = 0;
659  oMo[1][1] = -1;
660  oMo[1][2] = 0;
661  oMo[2][0] = 0;
662  oMo[2][1] = 0;
663  oMo[2][2] = -1;
664  cMo = cMo * oMo;
665  if (cMo2) {
666  *cMo2 = *cMo2 * oMo;
667  }
668  }
669 
670  return true;
671  }
672 
673  void getPoseWithOrthogonalMethod(apriltag_detection_info_t &info, vpHomogeneousMatrix &cMo1,
674  vpHomogeneousMatrix *cMo2, double *err1, double *err2)
675  {
676  apriltag_pose_t pose1, pose2;
677  double err_1, err_2;
678  estimate_tag_pose_orthogonal_iteration(&info, &err_1, &pose1, &err_2, &pose2, 50);
679  if (err_1 <= err_2) {
680  convertHomogeneousMatrix(pose1, cMo1);
681  if (cMo2) {
682  if (pose2.R) {
683  convertHomogeneousMatrix(pose2, *cMo2);
684  }
685  else {
686  *cMo2 = cMo1;
687  }
688  }
689  }
690  else {
691  convertHomogeneousMatrix(pose2, cMo1);
692  if (cMo2) {
693  convertHomogeneousMatrix(pose1, *cMo2);
694  }
695  }
696 
697  matd_destroy(pose1.R);
698  matd_destroy(pose1.t);
699  if (pose2.R) {
700  matd_destroy(pose2.t);
701  }
702  matd_destroy(pose2.R);
703 
704  if (err1)
705  *err1 = err_1;
706  if (err2)
707  *err2 = err_2;
708  }
709 
710  bool getZAlignedWithCameraAxis() { return m_zAlignedWithCameraFrame; }
711 
712  bool getAprilTagDecodeSharpening(double &decodeSharpening) const
713  {
714  if (m_td) {
715  decodeSharpening = m_td->decode_sharpening;
716  return true;
717  }
718  return false;
719  }
720 
721  bool getNbThreads(int &nThreads) const
722  {
723  if (m_td) {
724  nThreads = m_td->nthreads;
725  return true;
726  }
727  return false;
728  }
729 
730  bool getQuadDecimate(float &quadDecimate) const
731  {
732  if (m_td) {
733  quadDecimate = m_td->quad_decimate;
734  return true;
735  }
736  return false;
737  }
738 
739  bool getQuadSigma(float &quadSigma) const
740  {
741  if (m_td) {
742  quadSigma = m_td->quad_sigma;
743  return true;
744  }
745  return false;
746  }
747 
748  bool getRefineEdges(bool &refineEdges) const
749  {
750  if (m_td) {
751  refineEdges = (m_td->refine_edges ? true : false);
752  return true;
753  }
754  return false;
755  }
756 
757  bool getZAlignedWithCameraAxis() const { return m_zAlignedWithCameraFrame; }
758 
759  std::vector<int> getTagsId() const { return m_tagsId; }
760 
761  void setAprilTagDecodeSharpening(double decodeSharpening)
762  {
763  if (m_td) {
764  m_td->decode_sharpening = decodeSharpening;
765  }
766  }
767 
768  void setNbThreads(int nThreads)
769  {
770  if (m_td) {
771  m_td->nthreads = nThreads;
772  }
773  }
774 
775  void setQuadDecimate(float quadDecimate)
776  {
777  if (m_td) {
778  m_td->quad_decimate = quadDecimate;
779  }
780  }
781 
782  void setQuadSigma(float quadSigma)
783  {
784  if (m_td) {
785  m_td->quad_sigma = quadSigma;
786  }
787  }
788 
789  void setRefineDecode(bool) { }
790 
791  void setRefineEdges(bool refineEdges)
792  {
793  if (m_td) {
794  m_td->refine_edges = refineEdges ? 1 : 0;
795  }
796  }
797 
798  void setRefinePose(bool) { }
799 
800  void setPoseEstimationMethod(const vpPoseEstimationMethod &method) { m_poseEstimationMethod = method; }
801 
802  void setZAlignedWithCameraAxis(bool zAlignedWithCameraFrame) { m_zAlignedWithCameraFrame = zAlignedWithCameraFrame; }
803 
804 protected:
805  std::map<vpPoseEstimationMethod, vpPose::vpPoseMethodType> m_mapOfCorrespondingPoseMethods;
807  std::vector<int> m_tagsId;
809  apriltag_detector_t *m_td;
810  apriltag_family_t *m_tf;
811  zarray_t *m_detections;
812  bool m_zAlignedWithCameraFrame;
813 };
814 #endif // DOXYGEN_SHOULD_SKIP_THIS
815 
817  const vpPoseEstimationMethod &poseEstimationMethod)
818  : m_displayTag(false), m_displayTagColor(vpColor::none), m_displayTagThickness(2),
819  m_poseEstimationMethod(poseEstimationMethod), m_tagFamily(tagFamily), m_defaultCam(),
820  m_impl(new Impl(tagFamily, poseEstimationMethod))
821 { }
822 
824  : vpDetectorBase(o), m_displayTag(false), m_displayTagColor(vpColor::none), m_displayTagThickness(2),
825  m_poseEstimationMethod(o.m_poseEstimationMethod), m_tagFamily(o.m_tagFamily), m_defaultCam(),
826  m_impl(new Impl(*o.m_impl))
827 { }
828 
830 {
831  swap(*this, o);
832  return *this;
833 }
834 
836 
845 {
846  m_message.clear();
847  m_polygon.clear();
848  m_nb_objects = 0;
849 
850  std::vector<vpHomogeneousMatrix> cMo_vec;
851  const double tagSize = 1.0;
852  bool detected = m_impl->detect(I, tagSize, m_defaultCam, m_polygon, m_message, m_displayTag, m_displayTagColor,
853  m_displayTagThickness, nullptr, nullptr, nullptr, nullptr);
854  m_nb_objects = m_message.size();
855 
856  return detected;
857 }
858 
876 bool vpDetectorAprilTag::detect(const vpImage<unsigned char> &I, double tagSize, const vpCameraParameters &cam,
877  std::vector<vpHomogeneousMatrix> &cMo_vec, std::vector<vpHomogeneousMatrix> *cMo_vec2,
878  std::vector<double> *projErrors, std::vector<double> *projErrors2)
879 {
880  m_message.clear();
881  m_polygon.clear();
882  m_nb_objects = 0;
883 
884  cMo_vec.clear();
885  if (cMo_vec2) {
886  cMo_vec2->clear();
887  }
888  bool detected = m_impl->detect(I, tagSize, cam, m_polygon, m_message, m_displayTag, m_displayTagColor,
889  m_displayTagThickness, &cMo_vec, cMo_vec2, projErrors, projErrors2);
890  m_nb_objects = m_message.size();
891 
892  return detected;
893 }
894 
905 void vpDetectorAprilTag::displayFrames(const vpImage<unsigned char> &I, const std::vector<vpHomogeneousMatrix> &cMo_vec,
906  const vpCameraParameters &cam, double size, const vpColor &color, unsigned int thickness) const
907 {
908  m_impl->displayFrames(I, cMo_vec, cam, size, color, thickness);
909 }
910 
921 void vpDetectorAprilTag::displayFrames(const vpImage<vpRGBa> &I, const std::vector<vpHomogeneousMatrix> &cMo_vec,
922  const vpCameraParameters &cam, double size, const vpColor &color, unsigned int thickness) const
923 {
924  m_impl->displayFrames(I, cMo_vec, cam, size, color, thickness);
925 }
926 
935 void vpDetectorAprilTag::displayTags(const vpImage<unsigned char> &I, const std::vector<std::vector<vpImagePoint> > &tagsCorners,
936  const vpColor &color, unsigned int thickness) const
937 {
938  m_impl->displayTags(I, tagsCorners, color, thickness);
939 }
940 
949 void vpDetectorAprilTag::displayTags(const vpImage<vpRGBa> &I, const std::vector<std::vector<vpImagePoint> > &tagsCorners,
950  const vpColor &color, unsigned int thickness) const
951 {
952  m_impl->displayTags(I, tagsCorners, color, thickness);
953 }
954 
986 bool vpDetectorAprilTag::getPose(size_t tagIndex, double tagSize, const vpCameraParameters &cam,
987  vpHomogeneousMatrix &cMo, vpHomogeneousMatrix *cMo2, double *projError,
988  double *projError2)
989 {
990  return m_impl->getPose(tagIndex, tagSize, cam, cMo, cMo2, projError, projError2);
991 }
992 
1010 std::vector<std::vector<vpPoint> > vpDetectorAprilTag::getTagsPoints3D(const std::vector<int> &tagsId,
1011  const std::map<int, double> &tagsSize) const
1012 {
1013  std::vector<std::vector<vpPoint> > tagsPoints3D;
1014 
1015  double default_size = -1;
1016  {
1017  std::map<int, double>::const_iterator it = tagsSize.find(-1);
1018  if (it != tagsSize.end()) {
1019  default_size = it->second; // Default size
1020  }
1021  }
1022  for (size_t i = 0; i < tagsId.size(); i++) {
1023  std::map<int, double>::const_iterator it = tagsSize.find(tagsId[i]);
1024  double tagSize = default_size; // Default size
1025  if (it == tagsSize.end()) {
1026  if (default_size < 0) { // no default size found
1028  "Tag with id %d has no 3D size or there is no default 3D size defined", tagsId[i]));
1029  }
1030  }
1031  else {
1032  tagSize = it->second;
1033  }
1034  std::vector<vpPoint> points3D(4);
1035  if (m_impl->getZAlignedWithCameraAxis()) {
1036  points3D[0] = vpPoint(-tagSize / 2, tagSize / 2, 0);
1037  points3D[1] = vpPoint(tagSize / 2, tagSize / 2, 0);
1038  points3D[2] = vpPoint(tagSize / 2, -tagSize / 2, 0);
1039  points3D[3] = vpPoint(-tagSize / 2, -tagSize / 2, 0);
1040  }
1041  else {
1042  points3D[0] = vpPoint(-tagSize / 2, -tagSize / 2, 0);
1043  points3D[1] = vpPoint(tagSize / 2, -tagSize / 2, 0);
1044  points3D[2] = vpPoint(tagSize / 2, tagSize / 2, 0);
1045  points3D[3] = vpPoint(-tagSize / 2, tagSize / 2, 0);
1046  }
1047  tagsPoints3D.push_back(points3D);
1048  }
1049 
1050  return tagsPoints3D;
1051 }
1052 
1058 std::vector<std::vector<vpImagePoint> > vpDetectorAprilTag::getTagsCorners() const { return m_polygon; }
1059 
1065 std::vector<int> vpDetectorAprilTag::getTagsId() const { return m_impl->getTagsId(); }
1066 
1068 {
1069  return m_impl->setAprilTagDecodeSharpening(decodeSharpening);
1070 }
1071 
1073 {
1074  // back-up settings
1075  double decodeSharpening = 0.25;
1076  m_impl->getAprilTagDecodeSharpening(decodeSharpening);
1077  int nThreads = 1;
1078  m_impl->getNbThreads(nThreads);
1079  float quadDecimate = 1;
1080  m_impl->getQuadDecimate(quadDecimate);
1081  float quadSigma = 0;
1082  m_impl->getQuadSigma(quadSigma);
1083  bool refineEdges = true;
1084  m_impl->getRefineEdges(refineEdges);
1085  bool zAxis = m_impl->getZAlignedWithCameraAxis();
1086 
1087  delete m_impl;
1088  m_impl = new Impl(tagFamily, m_poseEstimationMethod);
1089  m_impl->setAprilTagDecodeSharpening(decodeSharpening);
1090  m_impl->setNbThreads(nThreads);
1091  m_impl->setQuadDecimate(quadDecimate);
1092  m_impl->setQuadSigma(quadSigma);
1093  m_impl->setRefineEdges(refineEdges);
1094  m_impl->setZAlignedWithCameraAxis(zAxis);
1095 }
1096 
1103 {
1104  if (nThreads > 0) {
1105  m_impl->setNbThreads(nThreads);
1106  }
1107 }
1108 
1115 {
1116  m_poseEstimationMethod = poseEstimationMethod;
1117  m_impl->setPoseEstimationMethod(poseEstimationMethod);
1118 }
1119 
1132 void vpDetectorAprilTag::setAprilTagQuadDecimate(float quadDecimate) { m_impl->setQuadDecimate(quadDecimate); }
1133 
1146 void vpDetectorAprilTag::setAprilTagQuadSigma(float quadSigma) { m_impl->setQuadSigma(quadSigma); }
1147 
1148 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
1153 {
1154  m_impl->setRefineDecode(refineDecode);
1155 }
1156 #endif
1157 
1172 void vpDetectorAprilTag::setAprilTagRefineEdges(bool refineEdges) { m_impl->setRefineEdges(refineEdges); }
1173 
1174 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
1178 void vpDetectorAprilTag::setAprilTagRefinePose(bool refinePose) { m_impl->setRefinePose(refinePose); }
1179 #endif
1180 
1182 {
1183  using std::swap;
1184 
1185  swap(o1.m_impl, o2.m_impl);
1186 }
1187 
1193 void vpDetectorAprilTag::setZAlignedWithCameraAxis(bool zAlignedWithCameraFrame)
1194 {
1195  m_impl->setZAlignedWithCameraAxis(zAlignedWithCameraFrame);
1196 }
1197 
1198 #elif !defined(VISP_BUILD_SHARED_LIBS)
1199 // Work around to avoid warning: libvisp_core.a(vpDetectorAprilTag.cpp.o) has
1200 // no symbols
1201 void dummy_vpDetectorAprilTag() { }
1202 #endif
Type * data
Address of the first element of the data array.
Definition: vpArray2D.h:138
Generic class defining intrinsic camera parameters.
Class to define RGB colors available for display functionalities.
Definition: vpColor.h:152
static const vpColor red
Definition: vpColor.h:211
static const vpColor none
Definition: vpColor.h:223
static const vpColor blue
Definition: vpColor.h:217
static const vpColor yellow
Definition: vpColor.h:219
static const vpColor green
Definition: vpColor.h:214
void setZAlignedWithCameraAxis(bool zAlignedWithCameraFrame)
std::vector< std::vector< vpImagePoint > > getTagsCorners() const
void setAprilTagQuadDecimate(float quadDecimate)
friend void swap(vpDetectorAprilTag &o1, vpDetectorAprilTag &o2)
void displayFrames(const vpImage< unsigned char > &I, const std::vector< vpHomogeneousMatrix > &cMo_vec, const vpCameraParameters &cam, double size, const vpColor &color, unsigned int thickness=1) const
vpDetectorAprilTag & operator=(vpDetectorAprilTag o)
std::vector< std::vector< vpPoint > > getTagsPoints3D(const std::vector< int > &tagsId, const std::map< int, double > &tagsSize) const
unsigned int m_displayTagThickness
bool detect(const vpImage< unsigned char > &I) vp_override
vpAprilTagFamily m_tagFamily
void setAprilTagRefineEdges(bool refineEdges)
vpPoseEstimationMethod m_poseEstimationMethod
vp_deprecated void setAprilTagRefinePose(bool refinePose)
@ TAG_CIRCLE21h7
AprilTag Circle21h7 pattern.
@ TAG_25h7
DEPRECATED AND POOR DETECTION PERFORMANCE.
@ TAG_36ARTOOLKIT
DEPRECATED AND WILL NOT DETECT ARTOOLKIT TAGS.
@ TAG_25h9
AprilTag 25h9 pattern.
@ TAG_CUSTOM48h12
AprilTag Custom48h12 pattern.
@ TAG_36h11
AprilTag 36h11 pattern (recommended)
@ TAG_STANDARD52h13
AprilTag Standard52h13 pattern.
@ TAG_16h5
AprilTag 16h5 pattern.
@ TAG_STANDARD41h12
AprilTag Standard41h12 pattern.
@ TAG_CIRCLE49h12
AprilTag Circle49h12 pattern.
void setAprilTagQuadSigma(float quadSigma)
void setAprilTagNbThreads(int nThreads)
vp_deprecated void setAprilTagRefineDecode(bool refineDecode)
vpDetectorAprilTag(const vpAprilTagFamily &tagFamily=TAG_36h11, const vpPoseEstimationMethod &poseEstimationMethod=HOMOGRAPHY_VIRTUAL_VS)
void setAprilTagPoseEstimationMethod(const vpPoseEstimationMethod &poseEstimationMethod)
std::vector< int > getTagsId() const
bool getPose(size_t tagIndex, double tagSize, const vpCameraParameters &cam, vpHomogeneousMatrix &cMo, vpHomogeneousMatrix *cMo2=nullptr, double *projError=nullptr, double *projError2=nullptr)
virtual ~vpDetectorAprilTag() vp_override
void displayTags(const vpImage< unsigned char > &I, const std::vector< std::vector< vpImagePoint > > &tagsCorners, const vpColor &color=vpColor::none, unsigned int thickness=1) const
void setAprilTagFamily(const vpAprilTagFamily &tagFamily)
void setAprilTagDecodeSharpening(double decodeSharpening)
std::vector< std::string > m_message
Message attached to each object.
std::vector< std::vector< vpImagePoint > > m_polygon
For each object, defines the polygon that contains the object.
size_t m_nb_objects
Number of detected objects.
static void displayLine(const vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, const vpColor &color, unsigned int thickness=1, bool segment=true)
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))
error that can be emitted by ViSP classes.
Definition: vpException.h:59
@ fatalError
Fatal error.
Definition: vpException.h:84
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpRotationMatrix getRotationMatrix() const
vpTranslationVector getTranslationVector() const
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
void set_uv(double u, double v)
Definition: vpImagePoint.h:352
unsigned int getWidth() const
Definition: vpImage.h:245
Type * bitmap
points toward the bitmap
Definition: vpImage.h:139
unsigned int getHeight() const
Definition: vpImage.h:184
static void convertPoint(const vpCameraParameters &cam, const double &u, const double &v, double &x, double &y)
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:77
void set_x(double x)
Set the point x coordinate in the image plane.
Definition: vpPoint.cpp:500
void setWorldCoordinates(double oX, double oY, double oZ)
Definition: vpPoint.cpp:110
void set_y(double y)
Set the point y coordinate in the image plane.
Definition: vpPoint.cpp:502
Class used for pose computation from N points (pose from point only). Some of the algorithms implemen...
Definition: vpPose.h:78
@ DEMENTHON
Definition: vpPose.h:84
@ VIRTUAL_VS
Definition: vpPose.h:93
@ LAGRANGE
Definition: vpPose.h:83
void addPoints(const std::vector< vpPoint > &lP)
Definition: vpPose.cpp:100
double computeResidual(const vpHomogeneousMatrix &cMo) const
Compute and return the sum of squared residuals expressed in meter^2 for the pose matrix cMo.
Definition: vpPose.cpp:285
bool computePose(vpPoseMethodType method, vpHomogeneousMatrix &cMo, bool(*func)(const vpHomogeneousMatrix &)=nullptr)
Definition: vpPose.cpp:333