Visual Servoing Platform  version 3.6.1 under development (2024-12-17)
vpDetectorAprilTag.h
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See https://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Base class for AprilTag detection.
32  */
33 #ifndef VP_DETECTOR_APRILTAG_H
34 #define VP_DETECTOR_APRILTAG_H
35 
36 #include <map>
37 
38 #include <visp3/core/vpConfig.h>
39 
40 #ifdef VISP_HAVE_APRILTAG
41 #include <visp3/core/vpCameraParameters.h>
42 #include <visp3/core/vpColor.h>
43 #include <visp3/core/vpHomogeneousMatrix.h>
44 #include <visp3/core/vpImage.h>
45 #include <visp3/detection/vpDetectorBase.h>
46 
47 BEGIN_VISP_NAMESPACE
234 class VISP_EXPORT vpDetectorAprilTag : public vpDetectorBase
235 {
236 public:
238  {
249  TAG_STANDARD52h13
250  };
251 
253  {
255  HOMOGRAPHY_VIRTUAL_VS,
257  DEMENTHON_VIRTUAL_VS,
259  LAGRANGE_VIRTUAL_VS,
261  BEST_RESIDUAL_VIRTUAL_VS,
264  HOMOGRAPHY_ORTHOGONAL_ITERATION
265  };
266 
267  vpDetectorAprilTag(const vpAprilTagFamily &tagFamily = TAG_36h11,
268  const vpPoseEstimationMethod &poseEstimationMethod = HOMOGRAPHY_VIRTUAL_VS);
271  virtual ~vpDetectorAprilTag() VP_OVERRIDE;
272  bool detect(const vpImage<unsigned char> &I) VP_OVERRIDE;
273 
274 
275  bool detect(const vpImage<unsigned char> &I, double tagSize, const vpCameraParameters &cam,
276  std::vector<vpHomogeneousMatrix> &cMo_vec, std::vector<vpHomogeneousMatrix> *cMo_vec2 = nullptr,
277  std::vector<double> *projErrors = nullptr, std::vector<double> *projErrors2 = nullptr);
278 
279  void displayFrames(const vpImage<unsigned char> &I, const std::vector<vpHomogeneousMatrix> &cMo_vec,
280  const vpCameraParameters &cam, double size, const vpColor &color, unsigned int thickness = 1) const;
281  void displayFrames(const vpImage<vpRGBa> &I, const std::vector<vpHomogeneousMatrix> &cMo_vec,
282  const vpCameraParameters &cam, double size, const vpColor &color, unsigned int thickness = 1) const;
283 
284  void displayTags(const vpImage<unsigned char> &I, const std::vector<std::vector<vpImagePoint> > &tagsCorners,
285  const vpColor &color = vpColor::none, unsigned int thickness = 1) const;
286  void displayTags(const vpImage<vpRGBa> &I, const std::vector<std::vector<vpImagePoint> > &tagsCorners,
287  const vpColor &color = vpColor::none, unsigned int thickness = 1) const;
288 
289  bool getPose(size_t tagIndex, double tagSize, const vpCameraParameters &cam, vpHomogeneousMatrix &cMo,
290  vpHomogeneousMatrix *cMo2 = nullptr, double *projError = nullptr, double *projError2 = nullptr);
291 
295  inline vpPoseEstimationMethod getPoseEstimationMethod() const { return m_poseEstimationMethod; }
296 
297  std::vector<std::vector<vpImagePoint> > getTagsCorners() const;
298  std::vector<int> getTagsId() const;
299  std::vector<std::vector<vpPoint> > getTagsPoints3D(const std::vector<int> &tagsId,
300  const std::map<int, double> &tagsSize) const;
301 
302  bool isZAlignedWithCameraAxis() const;
303 
304  void setAprilTagDecodeSharpening(double decodeSharpening);
305  void setAprilTagFamily(const vpAprilTagFamily &tagFamily);
306  void setAprilTagNbThreads(int nThreads);
307  void setAprilTagPoseEstimationMethod(const vpPoseEstimationMethod &poseEstimationMethod);
308  void setAprilTagQuadDecimate(float quadDecimate);
309  void setAprilTagQuadSigma(float quadSigma);
310  void setAprilTagRefineEdges(bool refineEdges);
311 
312 
313 
316  inline void setDisplayTag(bool display, const vpColor &color = vpColor::none, unsigned int thickness = 2)
317  {
318  m_displayTag = display;
319  m_displayTagColor = color;
320  m_displayTagThickness = thickness;
321  }
322 
323  inline friend void swap(vpDetectorAprilTag &o1, vpDetectorAprilTag &o2)
324  {
325  using std::swap;
326  swap(o1.m_impl, o2.m_impl);
327  }
328 
329  void setZAlignedWithCameraAxis(bool zAlignedWithCameraFrame);
330 
331 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
336  VP_DEPRECATED void setAprilTagRefinePose(bool refinePose);
337  VP_DEPRECATED void setAprilTagRefineDecode(bool refineDecode);
339 #endif
340 
341 protected:
344  unsigned int m_displayTagThickness;
347 
348 private:
349  vpCameraParameters m_defaultCam;
350 
351  // PIMPL idiom
352  class Impl;
353  Impl *m_impl;
354 };
355 
356 inline std::ostream &operator<<(std::ostream &os, const vpDetectorAprilTag::vpPoseEstimationMethod &method)
357 {
358  switch (method) {
360  os << "HOMOGRAPHY";
361  break;
362 
364  os << "HOMOGRAPHY_VIRTUAL_VS";
365  break;
366 
368  os << "DEMENTHON_VIRTUAL_VS";
369  break;
370 
372  os << "LAGRANGE_VIRTUAL_VS";
373  break;
374 
376  os << "BEST_RESIDUAL_VIRTUAL_VS";
377  break;
378 
380  os << "HOMOGRAPHY_ORTHOGONAL_ITERATION";
381  break;
382 
383  default:
384  os << "ERROR_UNKNOWN_POSE_METHOD!";
385  break;
386  }
387 
388  return os;
389 }
390 
391 inline std::ostream &operator<<(std::ostream &os, const vpDetectorAprilTag::vpAprilTagFamily &tagFamily)
392 {
393  switch (tagFamily) {
395  os << "36h11";
396  break;
397 
399  os << "36h10";
400  break;
401 
403  os << "36artoolkit";
404  break;
405 
407  os << "25h9";
408  break;
409 
411  os << "25h7";
412  break;
413 
415  os << "16h5";
416  break;
417 
419  os << "CIRCLE21h7";
420  break;
421 
423  os << "CIRCLE49h12";
424  break;
425 
427  os << "CUSTOM48h12";
428  break;
429 
431  os << "STANDARD52h13";
432  break;
433 
435  os << "STANDARD41h12";
436  break;
437 
438  default:
439  break;
440  }
441 
442  return os;
443 }
444 
445 END_VISP_NAMESPACE
446 
447 #endif
448 #endif
friend std::ostream & operator<<(std::ostream &s, const vpArray2D< Type > &A)
Definition: vpArray2D.h:614
Generic class defining intrinsic camera parameters.
Class to define RGB colors available for display functionalities.
Definition: vpColor.h:157
static const vpColor none
Definition: vpColor.h:229
void setDisplayTag(bool display, const vpColor &color=vpColor::none, unsigned int thickness=2)
friend void swap(vpDetectorAprilTag &o1, vpDetectorAprilTag &o2)
unsigned int m_displayTagThickness
vpAprilTagFamily m_tagFamily
vpPoseEstimationMethod m_poseEstimationMethod
@ 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.
Implementation of an homogeneous matrix and operations on such kind of matrices.
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
Definition of the vpImage class member functions.
Definition: vpImage.h:131
Definition: vpRGBa.h:65