JeVoisBase  1.12
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 .dat 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 (if relative stem) or "
45  "from the specified absolute location (if absolute stem).",
46  "/jevois/share/camera/camera_para", ParamCateg);
47 
48  //! Parameter \relates ARtoolkit
49  JEVOIS_DECLARE_PARAMETER(contpose, bool, "Use continuous pose estimation in good quality video",
50  true, ParamCateg);
51 
52  //! Parameter \relates ARtoolkit
53  JEVOIS_DECLARE_PARAMETER(msg3d, bool, "Send 3D serial messages if true, otherwise 2D",
54  false, ParamCateg);
55 
56  //! Enum for parameter \relates ARtoolkit
57  /*! dictionary to use according to the ARtoolkit
58  AR_MATRIX_CODE_3x3 support 64 markers
59  AR_MATRIX_CODE_3x3_HAMMING63 support 7 markers
60  AR_MATRIX_CODE_3x3_PARITY65 support 32 markers
61  the smaller the # of marker => larger distance between markers => better performance */
62  JEVOIS_DEFINE_ENUM_CLASS(Dict, (AR_MATRIX_CODE_3x3) (AR_MATRIX_CODE_3x3_HAMMING63) (AR_MATRIX_CODE_3x3_PARITY65));
63 
64  //! ThreshMode to overcome different lighting situation \relates ARtoolkit
65  JEVOIS_DEFINE_ENUM_CLASS(DictThreshMode, (AR_LABELING_THRESH_MODE_MANUAL) (AR_LABELING_THRESH_MODE_AUTO_MEDIAN)
66  (AR_LABELING_THRESH_MODE_AUTO_OTSU) (AR_LABELING_THRESH_MODE_AUTO_ADAPTIVE)
67  (AR_LABELING_THRESH_MODE_AUTO_BRACKETING));
68 
69  //! Parameter \relates ARtoolkit
70  JEVOIS_DECLARE_PARAMETER(threshmode, DictThreshMode, "Dictionary threshold mode to use, affects "
71  "robustness to varying lighting conditions",
72  DictThreshMode::AR_LABELING_THRESH_MODE_AUTO_OTSU, DictThreshMode_Values, ParamCateg);
73 
74  //! Parameter \relates ARtoolkit
75  JEVOIS_DECLARE_PARAMETER(dictionary, Dict, "Symbol dictionary to use",
76  Dict::AR_MATRIX_CODE_3x3_PARITY65, Dict_Values, ParamCateg);
77 
78  //! Parameter \relates ARtoolkit
79  JEVOIS_DECLARE_PARAMETER(confthresh, double, "Minimum confidence threshold required for valid detections",
80  0.7, jevois::Range<double>(0.0, 1.0), ParamCateg)
81 }
82 
83 //! Augmented reality markers using ARtoolkit
84 /*! Detect and decode patterns known as ARtoolkit markers, which are small 2D barcodes often used in augmented
85  reality and robotics.
86 
87  \ingroup Components */
89  public jevois::Parameter<artoolkit::camparams, artoolkit::contpose, artoolkit::dictionary,
90  artoolkit::msg3d, artoolkit::confthresh, artoolkit::threshmode>
91 {
92  public:
93 
94  //! Constructor
96 
97  //! Core initialization
98  /*! Note that initialization is only partial and will complete once the frame size and format is known at the first
99  call to detectMarkers() */
100  void postInit() override;
101 
102  // Un-init in preparation for destruction
103  void postUninit() override;
104 
105  //! Destructor
106  ~ARtoolkit();
107 
108  //! Detect markers in an image
109  /*! Probably the fastest if you have not already converted your input image to cv::Mat */
110  void detectMarkers(jevois::RawImage const & image);
111 
112  //! Detect markers in an image
113  void detectMarkers(cv::Mat const & image);
114 
115  //! Draw any markers previously detected by detectMarkers()
116  /*! If txtx,txty are positive, also print a text string there */
117  void drawDetections(jevois::RawImage & outimg, int txtx = -1, int txty = -1);
118 
119  //! Send serial messages about detections
120  /*! The module given should be the owner of this component, we will use it to actually send each serial message
121  using some variant of jevois::Module::sendSetial(). */
122  void sendSerial(jevois::StdModule * mod);
123 
124  struct arresults
125  {
126  int id; //!< Marker ID
127  double width; //!< size in mm
128  double height; //!< size in mm
129  double q[4]; //!< quaternion
130  double pos[3]; //!< 3d position
131  int p2d[2]; //!< 2d position in image coords
132  std::vector<cv::Point> corners; //!< corners in standardized image coords
133  };
134 
135  std::vector<arresults> itsResults; //!< Results of the last detection round
136 
137  protected:
138  //! Manual initialization which should be run on first frame once its size is known
139  /*! pixformat should be AR_PIXEL_FORMAT_BGR, AR_PIXEL_FORMAT_RGB_565, etc */
140  void manualinit(int w, int h, AR_PIXEL_FORMAT pixformat);
141 
142  void detectInternal(unsigned char const * data);
143 
144  // some other not important parameters related to the initialization of the ARtoolkit
145 
146  // initilization required
147  ARHandle * arHandle = nullptr;
148  ARPattHandle *arPattHandle;
150  AR3DHandle * ar3DHandle;
151  ARParamLT * gCparamLT = nullptr;
152  ARMarkerSquare * markersSquare = nullptr;
153  int markersSquareCount = 0;
154  unsigned int itsW, itsH;
155 };
int id
Marker ID.
Definition: ARtoolkit.H:126
JEVOIS_DECLARE_PARAMETER(thresh1, double, "First threshold for hysteresis", 50.0, ParamCateg)
Augmented reality markers using ARtoolkit.
Definition: ARtoolkit.H:88
ARPattHandle * arPattHandle
Definition: ARtoolkit.H:148
unsigned int itsW
Definition: ARtoolkit.H:154
AR3DHandle * ar3DHandle
Definition: ARtoolkit.H:150
std::vector< arresults > itsResults
Results of the last detection round.
Definition: ARtoolkit.H:135
double width
size in mm
Definition: ARtoolkit.H:127
friend friend class Component
double height
size in mm
Definition: ARtoolkit.H:128
JEVOIS_DEFINE_ENUM_CLASS(CameraSensor,(any)(ov9650)(ov2640)(ov7725)(ar0135))
std::vector< cv::Point > corners
corners in standardized image coords
Definition: ARtoolkit.H:132
int gARPattDetectionMode
Definition: ARtoolkit.H:149