Visual Servoing Platform  version 3.6.1 under development (2024-02-13)
vpDetectorAprilTag.h
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 #ifndef _vpDetectorAprilTag_h_
36 #define _vpDetectorAprilTag_h_
37 
38 #include <map>
39 
40 #include <visp3/core/vpConfig.h>
41 
42 #ifdef VISP_HAVE_APRILTAG
43 #include <visp3/core/vpCameraParameters.h>
44 #include <visp3/core/vpColor.h>
45 #include <visp3/core/vpHomogeneousMatrix.h>
46 #include <visp3/core/vpImage.h>
47 #include <visp3/detection/vpDetectorBase.h>
48 
216 class VISP_EXPORT vpDetectorAprilTag : public vpDetectorBase
217 {
218 public:
220  {
231  TAG_STANDARD52h13
232  };
233 
235  {
237  HOMOGRAPHY_VIRTUAL_VS,
239  DEMENTHON_VIRTUAL_VS,
241  LAGRANGE_VIRTUAL_VS,
243  BEST_RESIDUAL_VIRTUAL_VS,
246  HOMOGRAPHY_ORTHOGONAL_ITERATION
247  };
248 
249  vpDetectorAprilTag(const vpAprilTagFamily &tagFamily = TAG_36h11,
250  const vpPoseEstimationMethod &poseEstimationMethod = HOMOGRAPHY_VIRTUAL_VS);
253  virtual ~vpDetectorAprilTag() vp_override;
254  bool detect(const vpImage<unsigned char> &I) vp_override;
255 
256 
257  bool detect(const vpImage<unsigned char> &I, double tagSize, const vpCameraParameters &cam,
258  std::vector<vpHomogeneousMatrix> &cMo_vec, std::vector<vpHomogeneousMatrix> *cMo_vec2 = nullptr,
259  std::vector<double> *projErrors = nullptr, std::vector<double> *projErrors2 = nullptr);
260 
261  void displayFrames(const vpImage<unsigned char> &I, const std::vector<vpHomogeneousMatrix> &cMo_vec,
262  const vpCameraParameters &cam, double size, const vpColor &color, unsigned int thickness = 1) const;
263  void displayFrames(const vpImage<vpRGBa> &I, const std::vector<vpHomogeneousMatrix> &cMo_vec,
264  const vpCameraParameters &cam, double size, const vpColor &color, unsigned int thickness = 1) const;
265 
266  void displayTags(const vpImage<unsigned char> &I, const std::vector<std::vector<vpImagePoint> > &tagsCorners,
267  const vpColor &color = vpColor::none, unsigned int thickness = 1) const;
268  void displayTags(const vpImage<vpRGBa> &I, const std::vector<std::vector<vpImagePoint> > &tagsCorners,
269  const vpColor &color = vpColor::none, unsigned int thickness = 1) const;
270 
271  bool getPose(size_t tagIndex, double tagSize, const vpCameraParameters &cam, vpHomogeneousMatrix &cMo,
272  vpHomogeneousMatrix *cMo2 = nullptr, double *projError = nullptr, double *projError2 = nullptr);
273 
277  inline vpPoseEstimationMethod getPoseEstimationMethod() const { return m_poseEstimationMethod; }
278 
279  std::vector<std::vector<vpImagePoint> > getTagsCorners() const;
280  std::vector<int> getTagsId() const;
281  std::vector<std::vector<vpPoint> > getTagsPoints3D(const std::vector<int> &tagsId,
282  const std::map<int, double> &tagsSize) const;
283 
284  void setAprilTagDecodeSharpening(double decodeSharpening);
285  void setAprilTagFamily(const vpAprilTagFamily &tagFamily);
286  void setAprilTagNbThreads(int nThreads);
287  void setAprilTagPoseEstimationMethod(const vpPoseEstimationMethod &poseEstimationMethod);
288  void setAprilTagQuadDecimate(float quadDecimate);
289  void setAprilTagQuadSigma(float quadSigma);
290  void setAprilTagRefineEdges(bool refineEdges);
291 
292 
293 
296  inline void setDisplayTag(bool display, const vpColor &color = vpColor::none, unsigned int thickness = 2)
297  {
298  m_displayTag = display;
299  m_displayTagColor = color;
300  m_displayTagThickness = thickness;
301  }
302 
303  friend void swap(vpDetectorAprilTag &o1, vpDetectorAprilTag &o2);
304 
305  void setZAlignedWithCameraAxis(bool zAlignedWithCameraFrame);
306 
307 #if defined(VISP_BUILD_DEPRECATED_FUNCTIONS)
312  vp_deprecated void setAprilTagRefinePose(bool refinePose);
313  vp_deprecated void setAprilTagRefineDecode(bool refineDecode);
315 #endif
316 
317 protected:
320  unsigned int m_displayTagThickness;
323 
324 private:
325  vpCameraParameters m_defaultCam;
326 
327  // PIMPL idiom
328  class Impl;
329  Impl *m_impl;
330 };
331 
332 inline std::ostream &operator<<(std::ostream &os, const vpDetectorAprilTag::vpPoseEstimationMethod &method)
333 {
334  switch (method) {
336  os << "HOMOGRAPHY";
337  break;
338 
340  os << "HOMOGRAPHY_VIRTUAL_VS";
341  break;
342 
344  os << "DEMENTHON_VIRTUAL_VS";
345  break;
346 
348  os << "LAGRANGE_VIRTUAL_VS";
349  break;
350 
352  os << "BEST_RESIDUAL_VIRTUAL_VS";
353  break;
354 
356  os << "HOMOGRAPHY_ORTHOGONAL_ITERATION";
357  break;
358 
359  default:
360  os << "ERROR_UNKNOWN_POSE_METHOD!";
361  break;
362  }
363 
364  return os;
365 }
366 
367 inline std::ostream &operator<<(std::ostream &os, const vpDetectorAprilTag::vpAprilTagFamily &tagFamily)
368 {
369  switch (tagFamily) {
371  os << "36h11";
372  break;
373 
375  os << "36h10";
376  break;
377 
379  os << "36artoolkit";
380  break;
381 
383  os << "25h9";
384  break;
385 
387  os << "25h7";
388  break;
389 
391  os << "16h5";
392  break;
393 
395  os << "CIRCLE21h7";
396  break;
397 
399  os << "CIRCLE49h12";
400  break;
401 
403  os << "CUSTOM48h12";
404  break;
405 
407  os << "STANDARD52h13";
408  break;
409 
411  os << "STANDARD41h12";
412  break;
413 
414  default:
415  break;
416  }
417 
418  return os;
419 }
420 
421 #endif
422 #endif
friend std::ostream & operator<<(std::ostream &s, const vpArray2D< Type > &A)
Definition: vpArray2D.h:523
Generic class defining intrinsic camera parameters.
Class to define RGB colors available for display functionalities.
Definition: vpColor.h:152
static const vpColor none
Definition: vpColor.h:223
void setDisplayTag(bool display, const vpColor &color=vpColor::none, unsigned int thickness=2)
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:69
Definition: vpRGBa.h:61