JeVoisBase  1.10
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
ARtoolkit.H
Go to the documentation of this file.
1 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 //
3 // JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2016 by Laurent Itti, the University of Southern
4 // California (USC), and iLab at USC. See http://iLab.usc.edu and http://jevois.org for information about this project.
5 //
6 // This file is part of the JeVois Smart Embedded Machine Vision Toolkit. This program is free software; you can
7 // redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
8 // Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
10 // License for more details. You should have received a copy of the GNU General Public License along with this program;
11 // if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12 //
13 // Contact information: Shixian Wen - 3641 Watt Way, HNB-10A - Los Angeles, BA 90089-2520 - USA.
14 // Tel: +1 213 740 3527 - shixianw@usc.edu - http://iLab.usc.edu - http://jevois.org
15 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16 /*! \file */
17 
18 #pragma once
19 
21 #include <jevois/Types/Enum.H>
22 #include "opencv2/objdetect.hpp"
23 #include "opencv2/highgui.hpp"
24 #include "opencv2/imgproc.hpp"
25 
26 #include <AR/ar.h>
27 #include <AR/video.h>
28 #include <AR/config.h>
29 #include <AR/param.h>
30 #include "ARMarkerSquare.h"
31 // I need to define diferent camera_para.dat according to different camera size because of the limitation of the
32 // original ARtoolkit library or change the original ARtoolkit library and I think its not a good way to go, makes the
33 // package hard to distribute and update
34 namespace jevois { class StdModule; }
35 
36 namespace artoolkit
37 {
38  static jevois::ParameterCategory const ParamCateg("ARtoolkit Options");
39 
40  //! Parameter \relates ARtoolkit
41  JEVOIS_DECLARE_PARAMETER(camparams, std::string, "File stem of camera parameters, or empty. Camera resolution "
42  "will be appended, as well as a .cfg extension. For example, specifying 'camera_para' "
43  "here and running the camera sensor at 320x240 will attempt to load "
44  "camera_para320x240.dat from within the module's directory.",
45  "camera_para", ParamCateg);
46 
47  //! Parameter \relates ARtoolkit
48  JEVOIS_DECLARE_PARAMETER(contpose, bool, "Use continuous pose estimation in good quality video",
49  true, ParamCateg);
50 
51  //! Parameter \relates ARtoolkit
52  JEVOIS_DECLARE_PARAMETER(msg3d, bool, "Send 3D serial messages if true, otherwise 2D",
53  false, ParamCateg);
54 
55  //! Enum for parameter \relates ARtoolkit
56  /*! dictionary to use according to the ARtoolkit
57  AR_MATRIX_CODE_3x3 support 64 markers
58  AR_MATRIX_CODE_3x3_HAMMING63 support 7 markers
59  AR_MATRIX_CODE_3x3_PARITY65 support 32 markers
60  the smaller the # of marker => larger distance between markers => better performance */
61  JEVOIS_DEFINE_ENUM_CLASS(Dict, (AR_MATRIX_CODE_3x3) (AR_MATRIX_CODE_3x3_HAMMING63) (AR_MATRIX_CODE_3x3_PARITY65));
62 
63  //! ThreshMode to overcome different lighting situation \relates ARtoolkit
64  JEVOIS_DEFINE_ENUM_CLASS(DictThreshMode, (AR_LABELING_THRESH_MODE_MANUAL) (AR_LABELING_THRESH_MODE_AUTO_MEDIAN)
65  (AR_LABELING_THRESH_MODE_AUTO_OTSU) (AR_LABELING_THRESH_MODE_AUTO_ADAPTIVE)
66  (AR_LABELING_THRESH_MODE_AUTO_BRACKETING));
67 
68  //! Parameter \relates ARtoolkit
69  JEVOIS_DECLARE_PARAMETER(threshmode, DictThreshMode, "Dictionary threshold mode to use, affects "
70  "robustness to varying lighting conditions",
71  DictThreshMode::AR_LABELING_THRESH_MODE_AUTO_OTSU, DictThreshMode_Values, ParamCateg);
72 
73  //! Parameter \relates ARtoolkit
74  JEVOIS_DECLARE_PARAMETER(dictionary, Dict, "Symbol dictionary to use",
75  Dict::AR_MATRIX_CODE_3x3_PARITY65, Dict_Values, ParamCateg);
76 
77  //! Parameter \relates ARtoolkit
78  JEVOIS_DECLARE_PARAMETER(confthresh, double, "Minimum confidence threshold required for valid detections",
79  0.7, jevois::Range<double>(0.0, 1.0), ParamCateg)
80 }
81 
82 //! Augmented reality markers using ARtoolkit
83 /*! Detect and decode patterns known as ARtoolkit markers, which are small 2D barcodes often used in augmented
84  reality and robotics.
85 
86  \ingroup Components */
88  public jevois::Parameter<artoolkit::camparams, artoolkit::contpose, artoolkit::dictionary,
89  artoolkit::msg3d, artoolkit::confthresh, artoolkit::threshmode>
90 {
91  public:
92 
93  //! Constructor
95 
96  //! Core initialization
97  /*! Note that initialization is only partial and will complete once the frame size and format is known at the first
98  call to detectMarkers() */
99  void postInit() override;
100 
101  // Un-init in preparation for destruction
102  void postUninit() override;
103 
104  //! Destructor
105  ~ARtoolkit();
106 
107  //! Detect markers in an image
108  /*! Probably the fastest if you have not already converted your input image to cv::Mat */
109  void detectMarkers(jevois::RawImage const & image);
110 
111  //! Detect markers in an image
112  void detectMarkers(cv::Mat const & image);
113 
114  //! Draw any markers previously detected by detectMarkers()
115  /*! If txtx,txty are positive, also print a text string there */
116  void drawDetections(jevois::RawImage & outimg, int txtx = -1, int txty = -1);
117 
118  //! Send serial messages about detections
119  /*! The module given should be the owner of this component, we will use it to actually send each serial message
120  using some variant of jevois::Module::sendSetial(). */
121  void sendSerial(jevois::StdModule * mod);
122 
123  struct arresults
124  {
125  int id; //!< Marker ID
126  double width; //!< size in mm
127  double height; //!< size in mm
128  double q[4]; //!< quaternion
129  double pos[3]; //!< 3d position
130  int p2d[2]; //!< 2d position in image coords
131  std::vector<cv::Point> corners; //!< corners in standardized image coords
132  };
133 
134  std::vector<arresults> itsResults; //!< Results of the last detection round
135 
136  protected:
137  //! Manual initialization which should be run on first frame once its size is known
138  /*! pixformat should be AR_PIXEL_FORMAT_BGR, AR_PIXEL_FORMAT_RGB_565, etc */
139  void manualinit(int w, int h, AR_PIXEL_FORMAT pixformat);
140 
141  void detectInternal(unsigned char const * data);
142 
143  // some other not important parameters related to the initialization of the ARtoolkit
144 
145  // initilization required
146  ARHandle * arHandle = nullptr;
147  ARPattHandle *arPattHandle;
149  AR3DHandle * ar3DHandle;
150  ARParamLT * gCparamLT = nullptr;
151  ARMarkerSquare * markersSquare = nullptr;
152  int markersSquareCount = 0;
153  unsigned int itsW, itsH;
154 };
int id
Marker ID.
Definition: ARtoolkit.H:125
JEVOIS_DECLARE_PARAMETER(thresh1, double, "First threshold for hysteresis", 50.0, ParamCateg)
Augmented reality markers using ARtoolkit.
Definition: ARtoolkit.H:87
ARPattHandle * arPattHandle
Definition: ARtoolkit.H:147
unsigned int itsW
Definition: ARtoolkit.H:153
AR3DHandle * ar3DHandle
Definition: ARtoolkit.H:149
std::vector< arresults > itsResults
Results of the last detection round.
Definition: ARtoolkit.H:134
double width
size in mm
Definition: ARtoolkit.H:126
friend friend class Component
double height
size in mm
Definition: ARtoolkit.H:127
JEVOIS_DEFINE_ENUM_CLASS(CameraSensor,(any)(ov9650)(ov2640)(ov7725)(ar0135))
std::vector< cv::Point > corners
corners in standardized image coords
Definition: ARtoolkit.H:131
int gARPattDetectionMode
Definition: ARtoolkit.H:148