JeVoisBase  1.20
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 <jevois/GPU/GUIhelper.H>
23 
24 #include <AR/ar.h>
25 #include <AR/video.h>
26 #include <AR/config.h>
27 #include <AR/param.h>
29 
30 // I need to define diferent camera_para.dat according to different camera size because of the limitation of the
31 // original ARtoolkit library or change the original ARtoolkit library and I think its not a good way to go, makes the
32 // package hard to distribute and update
33 namespace jevois { class StdModule; }
34 
35 namespace artoolkit
36 {
37  static jevois::ParameterCategory const ParamCateg("ARtoolkit Options");
38 
39  //! Parameter \relates ARtoolkit
40  JEVOIS_DECLARE_PARAMETER(camparams, std::string, "File stem of camera parameters, or empty. Camera resolution "
41  "will be appended, as well as a .dat extension. For example, specifying 'camera_para' "
42  "here and running the camera sensor at 320x240 will attempt to load "
43  "camera_para320x240.dat from within the module's directory (if relative stem) or "
44  "from the specified absolute location (if absolute stem).",
45  JEVOIS_SHARE_PATH "/camera/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  //! Constructor
94 
95  //! Core initialization
96  /*! Note that initialization is only partial and will complete once the frame size and format is known at the first
97  call to detectMarkers() */
98  void postInit() override;
99 
100  // Un-init in preparation for destruction
101  void postUninit() override;
102 
103  //! Destructor
104  ~ARtoolkit();
105 
106  //! Detect markers in an image
107  /*! Probably the fastest if you have not already converted your input image to cv::Mat */
108  void detectMarkers(jevois::RawImage const & image);
109 
110  //! Detect markers in an image
111  void detectMarkers(cv::Mat const & image);
112 
113  //! Draw any markers previously detected by detectMarkers()
114  /*! If txtx,txty are positive, also print a text string there */
115  void drawDetections(jevois::RawImage & outimg, int txtx = -1, int txty = -1);
116 
117 #ifdef JEVOIS_PRO
118  //! Draw any markers previously detected by detectMarkers() using ImGui
119  /*! Detections are drawn to scale on the last image drawn by the helper. */
120  void drawDetections(jevois::GUIhelper & helper);
121 #endif
122 
123  //! Send serial messages about detections
124  /*! The module given should be the owner of this component, we will use it to actually send each serial message
125  using some variant of jevois::Module::sendSetial(). */
126  void sendSerial(jevois::StdModule * mod);
127 
128  struct arresults
129  {
130  int id; //!< Marker ID
131  double width; //!< size in mm
132  double height; //!< size in mm
133  double q[4]; //!< quaternion
134  double pos[3]; //!< 3d position
135  int p2d[2]; //!< 2d position in image coords
136  std::vector<cv::Point> corners; //!< corners in standardized image coords
137  };
138 
139  std::vector<arresults> itsResults; //!< Results of the last detection round
140 
141  protected:
142  //! Manual initialization which should be run on first frame once its size is known
143  /*! pixformat should be AR_PIXEL_FORMAT_BGR, AR_PIXEL_FORMAT_RGB_565, etc */
144  void manualinit(int w, int h, AR_PIXEL_FORMAT pixformat);
145 
146  void detectInternal(unsigned char const * data);
147 
148  // some other not important parameters related to the initialization of the ARtoolkit
149 
150  // initialization required
151  ARHandle * arHandle = nullptr;
152  ARPattHandle *arPattHandle;
154  AR3DHandle * ar3DHandle;
155  ARParamLT * gCparamLT = nullptr;
158  unsigned int itsW, itsH;
159 };
jevois::GUIhelper
jevois::ParameterRegistry::Component
friend friend class Component
ARtoolkit::manualinit
void manualinit(int w, int h, AR_PIXEL_FORMAT pixformat)
Manual initialization which should be run on first frame once its size is known.
Definition: ARtoolkit.C:58
jevois::Range
ARtoolkit::drawDetections
void drawDetections(jevois::RawImage &outimg, int txtx=-1, int txty=-1)
Draw any markers previously detected by detectMarkers()
Definition: ARtoolkit.C:237
JEVOIS_DECLARE_PARAMETER
JEVOIS_DECLARE_PARAMETER(thresh1, double, "First threshold for hysteresis", 50.0, ParamCateg)
ARtoolkit::arresults::p2d
int p2d[2]
2d position in image coords
Definition: ARtoolkit.H:135
artoolkit
Definition: ARtoolkit.H:35
ARtoolkit::arresults::corners
std::vector< cv::Point > corners
corners in standardized image coords
Definition: ARtoolkit.H:136
jevois::Component
ARtoolkit::postUninit
void postUninit() override
Definition: ARtoolkit.C:36
ARtoolkit::markersSquareCount
int markersSquareCount
Definition: ARtoolkit.H:157
jevois::RawImage
ARtoolkit::markersSquare
ARMarkerSquare * markersSquare
Definition: ARtoolkit.H:156
ARtoolkit::gCparamLT
ARParamLT * gCparamLT
Definition: ARtoolkit.H:155
ARtoolkit::gARPattDetectionMode
int gARPattDetectionMode
Definition: ARtoolkit.H:153
jevois::ParameterCategory
ARtoolkit::arresults::id
int id
Marker ID.
Definition: ARtoolkit.H:130
ARtoolkit::itsW
unsigned int itsW
Definition: ARtoolkit.H:158
demo.image
image
Definition: demo.py:84
ARtoolkit::itsH
unsigned int itsH
Definition: ARtoolkit.H:158
ARtoolkit
Augmented reality markers using ARtoolkit.
Definition: ARtoolkit.H:87
jevois
ARtoolkit::itsResults
std::vector< arresults > itsResults
Results of the last detection round.
Definition: ARtoolkit.H:139
Component.H
Enum.H
ARtoolkit::sendSerial
void sendSerial(jevois::StdModule *mod)
Send serial messages about detections.
Definition: ARtoolkit.C:281
ARtoolkit::arPattHandle
ARPattHandle * arPattHandle
Definition: ARtoolkit.H:152
ARtoolkit::arresults::height
double height
size in mm
Definition: ARtoolkit.H:132
ARtoolkit::arresults::q
double q[4]
quaternion
Definition: ARtoolkit.H:133
ARtoolkit::arresults::pos
double pos[3]
3d position
Definition: ARtoolkit.H:134
ARtoolkit::arresults
Definition: ARtoolkit.H:128
ARtoolkit::~ARtoolkit
~ARtoolkit()
Destructor.
Definition: ARtoolkit.C:25
ARtoolkit::detectInternal
void detectInternal(unsigned char const *data)
Definition: ARtoolkit.C:168
_ARMarkerSquare
Definition: ARMarkerSquare.h:80
ARtoolkit::arresults::width
double width
size in mm
Definition: ARtoolkit.H:131
ARtoolkit::detectMarkers
void detectMarkers(jevois::RawImage const &image)
Detect markers in an image.
Definition: ARtoolkit.C:130
ARtoolkit::postInit
void postInit() override
Core initialization.
Definition: ARtoolkit.C:29
ARtoolkit::ar3DHandle
AR3DHandle * ar3DHandle
Definition: ARtoolkit.H:154
ARMarkerSquare.h
JEVOIS_DEFINE_ENUM_CLASS
JEVOIS_DEFINE_ENUM_CLASS(CameraSensor,(any)(imx290)(os08a10)(ar0234))
ARtoolkit::arHandle
ARHandle * arHandle
Definition: ARtoolkit.H:151
jevois::StdModule
demo.w
w
Definition: demo.py:85
GUIhelper.H