JeVoisBase  1.16
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 "opencv2/objdetect.hpp"
25 #include "opencv2/highgui.hpp"
26 #include "opencv2/imgproc.hpp"
27 
28 #include <AR/ar.h>
29 #include <AR/video.h>
30 #include <AR/config.h>
31 #include <AR/param.h>
32 #include "ARMarkerSquare.h"
33 // I need to define diferent camera_para.dat according to different camera size because of the limitation of the
34 // original ARtoolkit library or change the original ARtoolkit library and I think its not a good way to go, makes the
35 // package hard to distribute and update
36 namespace jevois { class StdModule; }
37 
38 namespace artoolkit
39 {
40  static jevois::ParameterCategory const ParamCateg("ARtoolkit Options");
41 
42  //! Parameter \relates ARtoolkit
43  JEVOIS_DECLARE_PARAMETER(camparams, std::string, "File stem of camera parameters, or empty. Camera resolution "
44  "will be appended, as well as a .dat extension. For example, specifying 'camera_para' "
45  "here and running the camera sensor at 320x240 will attempt to load "
46  "camera_para320x240.dat from within the module's directory (if relative stem) or "
47  "from the specified absolute location (if absolute stem).",
48  JEVOIS_SHARE_PATH "/camera/camera_para", ParamCateg);
49 
50  //! Parameter \relates ARtoolkit
51  JEVOIS_DECLARE_PARAMETER(contpose, bool, "Use continuous pose estimation in good quality video",
52  true, ParamCateg);
53 
54  //! Parameter \relates ARtoolkit
55  JEVOIS_DECLARE_PARAMETER(msg3d, bool, "Send 3D serial messages if true, otherwise 2D",
56  false, ParamCateg);
57 
58  //! Enum for parameter \relates ARtoolkit
59  /*! dictionary to use according to the ARtoolkit
60  AR_MATRIX_CODE_3x3 support 64 markers
61  AR_MATRIX_CODE_3x3_HAMMING63 support 7 markers
62  AR_MATRIX_CODE_3x3_PARITY65 support 32 markers
63  the smaller the # of marker => larger distance between markers => better performance */
64  JEVOIS_DEFINE_ENUM_CLASS(Dict, (AR_MATRIX_CODE_3x3) (AR_MATRIX_CODE_3x3_HAMMING63) (AR_MATRIX_CODE_3x3_PARITY65));
65 
66  //! ThreshMode to overcome different lighting situation \relates ARtoolkit
67  JEVOIS_DEFINE_ENUM_CLASS(DictThreshMode, (AR_LABELING_THRESH_MODE_MANUAL) (AR_LABELING_THRESH_MODE_AUTO_MEDIAN)
68  (AR_LABELING_THRESH_MODE_AUTO_OTSU) (AR_LABELING_THRESH_MODE_AUTO_ADAPTIVE)
69  (AR_LABELING_THRESH_MODE_AUTO_BRACKETING));
70 
71  //! Parameter \relates ARtoolkit
72  JEVOIS_DECLARE_PARAMETER(threshmode, DictThreshMode, "Dictionary threshold mode to use, affects "
73  "robustness to varying lighting conditions",
74  DictThreshMode::AR_LABELING_THRESH_MODE_AUTO_OTSU, DictThreshMode_Values, ParamCateg);
75 
76  //! Parameter \relates ARtoolkit
77  JEVOIS_DECLARE_PARAMETER(dictionary, Dict, "Symbol dictionary to use",
78  Dict::AR_MATRIX_CODE_3x3_PARITY65, Dict_Values, ParamCateg);
79 
80  //! Parameter \relates ARtoolkit
81  JEVOIS_DECLARE_PARAMETER(confthresh, double, "Minimum confidence threshold required for valid detections",
82  0.7, jevois::Range<double>(0.0, 1.0), ParamCateg)
83 }
84 
85 //! Augmented reality markers using ARtoolkit
86 /*! Detect and decode patterns known as ARtoolkit markers, which are small 2D barcodes often used in augmented
87  reality and robotics.
88 
89  \ingroup Components */
91  public jevois::Parameter<artoolkit::camparams, artoolkit::contpose, artoolkit::dictionary,
92  artoolkit::msg3d, artoolkit::confthresh, artoolkit::threshmode>
93 {
94  public:
95  //! Constructor
97 
98  //! Core initialization
99  /*! Note that initialization is only partial and will complete once the frame size and format is known at the first
100  call to detectMarkers() */
101  void postInit() override;
102 
103  // Un-init in preparation for destruction
104  void postUninit() override;
105 
106  //! Destructor
107  ~ARtoolkit();
108 
109  //! Detect markers in an image
110  /*! Probably the fastest if you have not already converted your input image to cv::Mat */
111  void detectMarkers(jevois::RawImage const & image);
112 
113  //! Detect markers in an image
114  void detectMarkers(cv::Mat const & image);
115 
116  //! Draw any markers previously detected by detectMarkers()
117  /*! If txtx,txty are positive, also print a text string there */
118  void drawDetections(jevois::RawImage & outimg, int txtx = -1, int txty = -1);
119 
120 #ifdef JEVOIS_PRO
121  //! Draw any markers previously detected by detectMarkers() using ImGui
122  /*! Detections are drawn to scale on the last image drawn by the helper. */
123  void drawDetections(jevois::GUIhelper & helper);
124 #endif
125 
126  //! Send serial messages about detections
127  /*! The module given should be the owner of this component, we will use it to actually send each serial message
128  using some variant of jevois::Module::sendSetial(). */
129  void sendSerial(jevois::StdModule * mod);
130 
131  struct arresults
132  {
133  int id; //!< Marker ID
134  double width; //!< size in mm
135  double height; //!< size in mm
136  double q[4]; //!< quaternion
137  double pos[3]; //!< 3d position
138  int p2d[2]; //!< 2d position in image coords
139  std::vector<cv::Point> corners; //!< corners in standardized image coords
140  };
141 
142  std::vector<arresults> itsResults; //!< Results of the last detection round
143 
144  protected:
145  //! Manual initialization which should be run on first frame once its size is known
146  /*! pixformat should be AR_PIXEL_FORMAT_BGR, AR_PIXEL_FORMAT_RGB_565, etc */
147  void manualinit(int w, int h, AR_PIXEL_FORMAT pixformat);
148 
149  void detectInternal(unsigned char const * data);
150 
151  // some other not important parameters related to the initialization of the ARtoolkit
152 
153  // initialization required
154  ARHandle * arHandle = nullptr;
155  ARPattHandle *arPattHandle;
157  AR3DHandle * ar3DHandle;
158  ARParamLT * gCparamLT = nullptr;
161  unsigned int itsW, itsH;
162 };
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:56
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:235
jevois::GUIhelper
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:138
artoolkit
Definition: ARtoolkit.H:38
JEVOIS_DEFINE_ENUM_CLASS
JEVOIS_DEFINE_ENUM_CLASS(CameraSensor,(any)(ov9650)(ov2640)(ov7725)(ar0135)(imx290))
ARtoolkit::arresults::corners
std::vector< cv::Point > corners
corners in standardized image coords
Definition: ARtoolkit.H:139
jevois::Component
ARtoolkit::postUninit
void postUninit() override
Definition: ARtoolkit.C:34
ARtoolkit::markersSquareCount
int markersSquareCount
Definition: ARtoolkit.H:160
jevois::RawImage
ARtoolkit::markersSquare
ARMarkerSquare * markersSquare
Definition: ARtoolkit.H:159
ARtoolkit::gCparamLT
ARParamLT * gCparamLT
Definition: ARtoolkit.H:158
ARtoolkit::gARPattDetectionMode
int gARPattDetectionMode
Definition: ARtoolkit.H:156
jevois::ParameterCategory
ARtoolkit::arresults::id
int id
Marker ID.
Definition: ARtoolkit.H:133
ARtoolkit::itsW
unsigned int itsW
Definition: ARtoolkit.H:161
ARtoolkit::itsH
unsigned int itsH
Definition: ARtoolkit.H:161
ARtoolkit
Augmented reality markers using ARtoolkit.
Definition: ARtoolkit.H:90
jevois
ARtoolkit::itsResults
std::vector< arresults > itsResults
Results of the last detection round.
Definition: ARtoolkit.H:142
Component.H
Enum.H
ARtoolkit::sendSerial
void sendSerial(jevois::StdModule *mod)
Send serial messages about detections.
Definition: ARtoolkit.C:279
ARtoolkit::arPattHandle
ARPattHandle * arPattHandle
Definition: ARtoolkit.H:155
ARtoolkit::arresults::height
double height
size in mm
Definition: ARtoolkit.H:135
ARtoolkit::arresults::q
double q[4]
quaternion
Definition: ARtoolkit.H:136
ARtoolkit::arresults::pos
double pos[3]
3d position
Definition: ARtoolkit.H:137
ARtoolkit::arresults
Definition: ARtoolkit.H:131
ARtoolkit::~ARtoolkit
~ARtoolkit()
Destructor.
Definition: ARtoolkit.C:23
ARtoolkit::detectInternal
void detectInternal(unsigned char const *data)
Definition: ARtoolkit.C:166
_ARMarkerSquare
Definition: ARMarkerSquare.h:80
ARtoolkit::arresults::width
double width
size in mm
Definition: ARtoolkit.H:134
ARtoolkit::detectMarkers
void detectMarkers(jevois::RawImage const &image)
Detect markers in an image.
Definition: ARtoolkit.C:128
ARtoolkit::postInit
void postInit() override
Core initialization.
Definition: ARtoolkit.C:27
ARtoolkit::ar3DHandle
AR3DHandle * ar3DHandle
Definition: ARtoolkit.H:157
ARMarkerSquare.h
ARtoolkit::arHandle
ARHandle * arHandle
Definition: ARtoolkit.H:154
jevois::StdModule
GUIhelper.H