Visual Servoing Platform  version 3.6.1 under development (2024-07-27)
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 
227 class VISP_EXPORT vpDetectorAprilTag : public vpDetectorBase
228 {
229 public:
231  {
242  TAG_STANDARD52h13
243  };
244 
246  {
248  HOMOGRAPHY_VIRTUAL_VS,
250  DEMENTHON_VIRTUAL_VS,
252  LAGRANGE_VIRTUAL_VS,
254  BEST_RESIDUAL_VIRTUAL_VS,
257  HOMOGRAPHY_ORTHOGONAL_ITERATION
258  };
259 
260  vpDetectorAprilTag(const vpAprilTagFamily &tagFamily = TAG_36h11,
261  const vpPoseEstimationMethod &poseEstimationMethod = HOMOGRAPHY_VIRTUAL_VS);
264  virtual ~vpDetectorAprilTag() VP_OVERRIDE;
265  bool detect(const vpImage<unsigned char> &I) VP_OVERRIDE;
266 
267 
268  bool detect(const vpImage<unsigned char> &I, double tagSize, const vpCameraParameters &cam,
269  std::vector<vpHomogeneousMatrix> &cMo_vec, std::vector<vpHomogeneousMatrix> *cMo_vec2 = nullptr,
270  std::vector<double> *projErrors = nullptr, std::vector<double> *projErrors2 = nullptr);
271 
272  void displayFrames(const vpImage<unsigned char> &I, const std::vector<vpHomogeneousMatrix> &cMo_vec,
273  const vpCameraParameters &cam, double size, const vpColor &color, unsigned int thickness = 1) const;
274  void displayFrames(const vpImage<vpRGBa> &I, const std::vector<vpHomogeneousMatrix> &cMo_vec,
275  const vpCameraParameters &cam, double size, const vpColor &color, unsigned int thickness = 1) const;
276 
277  void displayTags(const vpImage<unsigned char> &I, const std::vector<std::vector<vpImagePoint> > &tagsCorners,
278  const vpColor &color = vpColor::none, unsigned int thickness = 1) const;
279  void displayTags(const vpImage<vpRGBa> &I, const std::vector<std::vector<vpImagePoint> > &tagsCorners,
280  const vpColor &color = vpColor::none, unsigned int thickness = 1) const;
281 
282  bool getPose(size_t tagIndex, double tagSize, const vpCameraParameters &cam, vpHomogeneousMatrix &cMo,
283  vpHomogeneousMatrix *cMo2 = nullptr, double *projError = nullptr, double *projError2 = nullptr);
284 
288  inline vpPoseEstimationMethod getPoseEstimationMethod() const { return m_poseEstimationMethod; }
289 
290  std::vector<std::vector<vpImagePoint> > getTagsCorners() const;
291  std::vector<int> getTagsId() const;
292  std::vector<std::vector<vpPoint> > getTagsPoints3D(const std::vector<int> &tagsId,
293  const std::map<int, double> &tagsSize) const;
294 
295  void setAprilTagDecodeSharpening(double decodeSharpening);
296  void setAprilTagFamily(const vpAprilTagFamily &tagFamily);
297  void setAprilTagNbThreads(int nThreads);
298  void setAprilTagPoseEstimationMethod(const vpPoseEstimationMethod &poseEstimationMethod);
299  void setAprilTagQuadDecimate(float quadDecimate);
300  void setAprilTagQuadSigma(float quadSigma);
301  void setAprilTagRefineEdges(bool refineEdges);
302 
303 
304 
307  inline void setDisplayTag(bool display, const vpColor &color = vpColor::none, unsigned int thickness = 2)
308  {
309  m_displayTag = display;
310  m_displayTagColor = color;
311  m_displayTagThickness = thickness;
312  }
313 
314  inline friend void swap(vpDetectorAprilTag& o1, vpDetectorAprilTag& o2) {
315  using std::swap;
316  swap(o1.m_impl, o2.m_impl);
317  }
318 
319  void setZAlignedWithCameraAxis(bool zAlignedWithCameraFrame);
320 
321 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
326  VP_DEPRECATED void setAprilTagRefinePose(bool refinePose);
327  VP_DEPRECATED void setAprilTagRefineDecode(bool refineDecode);
329 #endif
330 
331 protected:
334  unsigned int m_displayTagThickness;
337 
338 private:
339  vpCameraParameters m_defaultCam;
340 
341  // PIMPL idiom
342  class Impl;
343  Impl *m_impl;
344 };
345 
346 inline std::ostream &operator<<(std::ostream &os, const vpDetectorAprilTag::vpPoseEstimationMethod &method)
347 {
348  switch (method) {
350  os << "HOMOGRAPHY";
351  break;
352 
354  os << "HOMOGRAPHY_VIRTUAL_VS";
355  break;
356 
358  os << "DEMENTHON_VIRTUAL_VS";
359  break;
360 
362  os << "LAGRANGE_VIRTUAL_VS";
363  break;
364 
366  os << "BEST_RESIDUAL_VIRTUAL_VS";
367  break;
368 
370  os << "HOMOGRAPHY_ORTHOGONAL_ITERATION";
371  break;
372 
373  default:
374  os << "ERROR_UNKNOWN_POSE_METHOD!";
375  break;
376  }
377 
378  return os;
379 }
380 
381 inline std::ostream &operator<<(std::ostream &os, const vpDetectorAprilTag::vpAprilTagFamily &tagFamily)
382 {
383  switch (tagFamily) {
385  os << "36h11";
386  break;
387 
389  os << "36h10";
390  break;
391 
393  os << "36artoolkit";
394  break;
395 
397  os << "25h9";
398  break;
399 
401  os << "25h7";
402  break;
403 
405  os << "16h5";
406  break;
407 
409  os << "CIRCLE21h7";
410  break;
411 
413  os << "CIRCLE49h12";
414  break;
415 
417  os << "CUSTOM48h12";
418  break;
419 
421  os << "STANDARD52h13";
422  break;
423 
425  os << "STANDARD41h12";
426  break;
427 
428  default:
429  break;
430  }
431 
432  return os;
433 }
434 
435 END_VISP_NAMESPACE
436 
437 #endif
438 #endif
friend std::ostream & operator<<(std::ostream &s, const vpArray2D< Type > &A)
Definition: vpArray2D.h:611
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