Visual Servoing Platform  version 3.5.1 under development (2023-09-22)
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:
230  TAG_STANDARD52h13
231  };
232 
235  HOMOGRAPHY_VIRTUAL_VS,
237  DEMENTHON_VIRTUAL_VS,
239  LAGRANGE_VIRTUAL_VS,
241  BEST_RESIDUAL_VIRTUAL_VS,
244  HOMOGRAPHY_ORTHOGONAL_ITERATION
245  };
246 
247  vpDetectorAprilTag(const vpAprilTagFamily &tagFamily = TAG_36h11,
248  const vpPoseEstimationMethod &poseEstimationMethod = HOMOGRAPHY_VIRTUAL_VS);
251  virtual ~vpDetectorAprilTag();
252 
253  bool detect(const vpImage<unsigned char> &I);
254  bool detect(const vpImage<unsigned char> &I, double tagSize, const vpCameraParameters &cam,
255  std::vector<vpHomogeneousMatrix> &cMo_vec, std::vector<vpHomogeneousMatrix> *cMo_vec2 = NULL,
256  std::vector<double> *projErrors = NULL, std::vector<double> *projErrors2 = NULL);
257 
258  void displayFrames(const vpImage<unsigned char> &I, const std::vector<vpHomogeneousMatrix> &cMo_vec,
259  const vpCameraParameters &cam, double size, const vpColor &color, unsigned int thickness = 1) const;
260  void displayFrames(const vpImage<vpRGBa> &I, const std::vector<vpHomogeneousMatrix> &cMo_vec,
261  const vpCameraParameters &cam, double size, const vpColor &color, unsigned int thickness = 1) const;
262 
263  void displayTags(const vpImage<unsigned char> &I, const std::vector<std::vector<vpImagePoint> > &tagsCorners,
264  const vpColor &color = vpColor::none, unsigned int thickness = 1) const;
265  void displayTags(const vpImage<vpRGBa> &I, const std::vector<std::vector<vpImagePoint> > &tagsCorners,
266  const vpColor &color = vpColor::none, unsigned int thickness = 1) const;
267 
268  bool getPose(size_t tagIndex, double tagSize, const vpCameraParameters &cam, vpHomogeneousMatrix &cMo,
269  vpHomogeneousMatrix *cMo2 = NULL, double *projError = NULL, double *projError2 = NULL);
270 
274  inline vpPoseEstimationMethod getPoseEstimationMethod() const { return m_poseEstimationMethod; }
275 
276  std::vector<std::vector<vpImagePoint> > getTagsCorners() const;
277  std::vector<int> getTagsId() const;
278  std::vector<std::vector<vpPoint> > getTagsPoints3D(const std::vector<int> &tagsId,
279  const std::map<int, double> &tagsSize) const;
280 
281  void setAprilTagDecodeSharpening(double decodeSharpening);
282  void setAprilTagFamily(const vpAprilTagFamily &tagFamily);
283  void setAprilTagNbThreads(int nThreads);
284  void setAprilTagPoseEstimationMethod(const vpPoseEstimationMethod &poseEstimationMethod);
285  void setAprilTagQuadDecimate(float quadDecimate);
286  void setAprilTagQuadSigma(float quadSigma);
287  void setAprilTagRefineDecode(bool refineDecode);
288  void setAprilTagRefineEdges(bool refineEdges);
289  void setAprilTagRefinePose(bool refinePose);
290 
293  inline void setDisplayTag(bool display, const vpColor &color = vpColor::none, unsigned int thickness = 2)
294  {
295  m_displayTag = display;
296  m_displayTagColor = color;
297  m_displayTagThickness = thickness;
298  }
299 
300  friend void swap(vpDetectorAprilTag &o1, vpDetectorAprilTag &o2);
301 
302  void setZAlignedWithCameraAxis(bool zAlignedWithCameraFrame);
303 
304 protected:
307  unsigned int m_displayTagThickness;
310 
311 private:
312  vpCameraParameters m_defaultCam;
313 
314  // PIMPL idiom
315  class Impl;
316  Impl *m_impl;
317 };
318 
319 inline std::ostream &operator<<(std::ostream &os, const vpDetectorAprilTag::vpPoseEstimationMethod &method)
320 {
321  switch (method) {
323  os << "HOMOGRAPHY";
324  break;
325 
327  os << "HOMOGRAPHY_VIRTUAL_VS";
328  break;
329 
331  os << "DEMENTHON_VIRTUAL_VS";
332  break;
333 
335  os << "LAGRANGE_VIRTUAL_VS";
336  break;
337 
339  os << "BEST_RESIDUAL_VIRTUAL_VS";
340  break;
341 
343  os << "HOMOGRAPHY_ORTHOGONAL_ITERATION";
344  break;
345 
346  default:
347  os << "ERROR_UNKNOWN_POSE_METHOD!";
348  break;
349  }
350 
351  return os;
352 }
353 
354 inline std::ostream &operator<<(std::ostream &os, const vpDetectorAprilTag::vpAprilTagFamily &tagFamily)
355 {
356  switch (tagFamily) {
358  os << "36h11";
359  break;
360 
362  os << "36h10";
363  break;
364 
366  os << "36artoolkit";
367  break;
368 
370  os << "25h9";
371  break;
372 
374  os << "25h7";
375  break;
376 
378  os << "16h5";
379  break;
380 
382  os << "CIRCLE21h7";
383  break;
384 
386  os << "CIRCLE49h12";
387  break;
388 
390  os << "CUSTOM48h12";
391  break;
392 
394  os << "STANDARD52h13";
395  break;
396 
398  os << "STANDARD41h12";
399  break;
400 
401  default:
402  break;
403  }
404 
405  return os;
406 }
407 
408 #endif
409 #endif
friend std::ostream & operator<<(std::ostream &s, const vpArray2D< Type > &A)
Definition: vpArray2D.h:529
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)
vpPoseEstimationMethod getPoseEstimationMethod() const
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.
virtual bool detect(const vpImage< unsigned char > &I)=0
Implementation of an homogeneous matrix and operations on such kind of matrices.