SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
CVTrackedAruco.h
Go to the documentation of this file.
1 /**
2  * \file CVTrackedAruco.h
3  * \date Winter 2016
4  * \remarks Please use clangformat to format the code. See more code style on
5  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
6  * \authors Marcus Hudritsch, Michael Goettlicher
7  * \copyright http://opensource.org/licenses/GPL-3.0
8 */
9 
10 #ifndef CVTrackedAruco_H
11 #define CVTrackedAruco_H
12 
13 /*
14 The OpenCV library version 3.4 or above with extra module must be present.
15 If the application captures the live video stream with OpenCV you have
16 to define in addition the constant APP_USES_CVCAPTURE.
17 All classes that use OpenCV begin with CV.
18 See also the class docs for CVCapture, CVCalibration and CVTracked
19 for a good top down information.
20 */
21 
22 #include <CVTypedefs.h>
23 #include <CVTracked.h>
24 #include <SLFileStorage.h>
25 #include <opencv2/aruco.hpp>
26 
27 //-----------------------------------------------------------------------------
28 //! ArUco Parameters loaded from configuration file.
30 {
31 public:
34  filename("aruco_detector_params.yml")
35  {
36 #if CV_MAJOR_VERSION < 4 || CV_MINOR_VERSION < 7
37  arucoParams = cv::aruco::DetectorParameters::create();
38 #else
39  arucoParams = cv::aruco::DetectorParameters();
40 #endif
41  }
42 
43  bool loadFromFile(string calibIniPath)
44  {
45  string path = calibIniPath + filename;
46 
48  CVFileStorage fs(paramString, CVFileStorage::READ | CVFileStorage::MEMORY);
49  if (!fs.isOpened())
50  {
51  cout << "Could not find parameter file for ArUco tracking!" << endl;
52  cout << "Tried " << path << endl;
53  return false;
54  }
55 
56 #if CV_MAJOR_VERSION < 4 || CV_MINOR_VERSION < 7
57  fs["adaptiveThreshWinSizeMin"] >> arucoParams->adaptiveThreshWinSizeMin;
58  fs["adaptiveThreshWinSizeMax"] >> arucoParams->adaptiveThreshWinSizeMax;
59  fs["adaptiveThreshWinSizeStep"] >> arucoParams->adaptiveThreshWinSizeStep;
60  fs["adaptiveThreshConstant"] >> arucoParams->adaptiveThreshConstant;
61  fs["minMarkerPerimeterRate"] >> arucoParams->minMarkerPerimeterRate;
62  fs["maxMarkerPerimeterRate"] >> arucoParams->maxMarkerPerimeterRate;
63  fs["polygonalApproxAccuracyRate"] >> arucoParams->polygonalApproxAccuracyRate;
64  fs["minCornerDistanceRate"] >> arucoParams->minCornerDistanceRate;
65  fs["minDistanceToBorder"] >> arucoParams->minDistanceToBorder;
66  fs["cornerRefinementMethod"] >> arucoParams->cornerRefinementMethod; // cv::aruco::CornerRefineMethod
67  fs["cornerRefinementWinSize"] >> arucoParams->cornerRefinementWinSize;
68  fs["cornerRefinementMaxIterations"] >> arucoParams->cornerRefinementMaxIterations;
69  fs["cornerRefinementMinAccuracy"] >> arucoParams->cornerRefinementMinAccuracy;
70  fs["markerBorderBits"] >> arucoParams->markerBorderBits;
71  fs["perspectiveRemovePixelPerCell"] >> arucoParams->perspectiveRemovePixelPerCell;
72  fs["perspectiveRemoveIgnoredMarginPerCell"] >> arucoParams->perspectiveRemoveIgnoredMarginPerCell;
73  fs["maxErroneousBitsInBorderRate"] >> arucoParams->maxErroneousBitsInBorderRate;
74  fs["edgeLength"] >> edgeLength;
75  fs["arucoDictionaryId"] >> arucoDictionaryId;
76 #else
77  fs["adaptiveThreshWinSizeMin"] >> arucoParams.adaptiveThreshWinSizeMin;
78  fs["adaptiveThreshWinSizeMax"] >> arucoParams.adaptiveThreshWinSizeMax;
79  fs["adaptiveThreshWinSizeStep"] >> arucoParams.adaptiveThreshWinSizeStep;
80  fs["adaptiveThreshConstant"] >> arucoParams.adaptiveThreshConstant;
81  fs["minMarkerPerimeterRate"] >> arucoParams.minMarkerPerimeterRate;
82  fs["maxMarkerPerimeterRate"] >> arucoParams.maxMarkerPerimeterRate;
83  fs["polygonalApproxAccuracyRate"] >> arucoParams.polygonalApproxAccuracyRate;
84  fs["minCornerDistanceRate"] >> arucoParams.minCornerDistanceRate;
85  fs["minDistanceToBorder"] >> arucoParams.minDistanceToBorder;
86  fs["cornerRefinementMethod"] >> arucoParams.cornerRefinementMethod; // cv::aruco::CornerRefineMethod
87  fs["cornerRefinementWinSize"] >> arucoParams.cornerRefinementWinSize;
88  fs["cornerRefinementMaxIterations"] >> arucoParams.cornerRefinementMaxIterations;
89  fs["cornerRefinementMinAccuracy"] >> arucoParams.cornerRefinementMinAccuracy;
90  fs["markerBorderBits"] >> arucoParams.markerBorderBits;
91  fs["perspectiveRemovePixelPerCell"] >> arucoParams.perspectiveRemovePixelPerCell;
92  fs["perspectiveRemoveIgnoredMarginPerCell"] >> arucoParams.perspectiveRemoveIgnoredMarginPerCell;
93  fs["maxErroneousBitsInBorderRate"] >> arucoParams.maxErroneousBitsInBorderRate;
94  fs["edgeLength"] >> edgeLength;
95  fs["arucoDictionaryId"] >> arucoDictionaryId;
96 #endif
97 
98 #if CV_MAJOR_VERSION < 4 || CV_MINOR_VERSION < 7
99  dictionary = cv::aruco::getPredefinedDictionary(cv::aruco::PREDEFINED_DICTIONARY_NAME(arucoDictionaryId));
100 #else
101  dictionary = cv::aruco::getPredefinedDictionary(cv::aruco::PredefinedDictionaryType(arucoDictionaryId));
102 #endif
103 
104  return true;
105  }
106 
107 #if CV_MAJOR_VERSION < 4 || CV_MINOR_VERSION < 7
108  cv::Ptr<cv::aruco::Dictionary> dictionary; //!< predefined dictionary
109  cv::Ptr<cv::aruco::DetectorParameters> arucoParams; //!< detector parameter structure for aruco detection function
110 #else
111  cv::aruco::DetectorParameters arucoParams; //!< detector parameter structure for aruco detection function
112  cv::aruco::Dictionary dictionary; //!< predefined dictionary
113 #endif
114 
115  float edgeLength; //!< marker edge length
116  int arucoDictionaryId; //!< id of aruco dictionary
117  string arucoDetectorParams; //!< todo: put in one file
118  string filename; //!< parameter filename
119 };
120 
121 //-----------------------------------------------------------------------------
122 //! OpenCV ArUco marker tracker class derived from CVTracked
123 /*! Tracking class for ArUco markers tracking. See the official OpenCV docs on
124 ArUco markers: http://docs.opencv.org/3.1.0/d5/dae/tutorial_aruco_detection.html
125 The aruco marker used in the SLProject are printed in a PDF stored in the
126 data/Calibration folder. They use the dictionary 0 and where generated with the
127 functions CVTrackedAruco::drawArucoMarkerBoard and
128 CVTrackedAruco::drawArucoMarker.
129 */
130 class CVTrackedAruco : public CVTracked
131 {
132 public:
133  explicit CVTrackedAruco(int arucoID, string calibIniPath);
134 
135  // Getters
136  const CVArucoParams& params() const { return _params; }
137 
138  bool track(CVMat imageGray,
139  CVMat imageBgr,
140  CVCalibration* calib);
141 
142  //! Helper function to draw and save an aruco marker board image
143  static void drawArucoMarkerBoard(int dictionaryId,
144  int numMarkersX,
145  int numMarkersY,
146  float markerEdgeLengthM,
147  float markerSepaM,
148  const string& imgName,
149  float dpi = 254.0f,
150  bool showImage = false);
151 
152  //! Helper function to draw and save an aruco marker set
153  static void drawArucoMarker(int dictionaryId,
154  int minMarkerId,
155  int maxMarkerId,
156  int markerSizePX = 200);
157 
158 protected:
159  bool trackAll(CVMat imageGray,
160  CVMat imageBgr,
161  CVCalibration* calib,
162  CVRect roi = CVRect(0, 0, 0, 0));
163 
164  vector<int> arucoIDs; //!< detected Aruco marker IDs
165  CVVMatx44f objectViewMats; //!< object view matrices for all found markers
166 
167 private:
168  CVArucoParams _params; //!< Aruco parameters
169  int _arucoID; //!< Aruco Marker ID for this node
171 };
172 //-----------------------------------------------------------------------------
173 #endif // CVTrackedAruco_H
static SLint dpi
Dot per inch resolution of screen.
Definition: AppGLFW.cpp:41
cv::FileStorage CVFileStorage
Definition: CVTypedefs.h:61
vector< cv::Matx44f > CVVMatx44f
Definition: CVTypedefs.h:90
cv::Rect CVRect
Definition: CVTypedefs.h:39
cv::Mat CVMat
Definition: CVTypedefs.h:38
string SLstring
Definition: SL.h:158
@ IOK_config
Definition: SLFileStorage.h:44
ArUco Parameters loaded from configuration file.
int arucoDictionaryId
id of aruco dictionary
bool loadFromFile(string calibIniPath)
cv::Ptr< cv::aruco::DetectorParameters > arucoParams
detector parameter structure for aruco detection function
string arucoDetectorParams
todo: put in one file
float edgeLength
marker edge length
string filename
parameter filename
cv::Ptr< cv::aruco::Dictionary > dictionary
predefined dictionary
Live video camera calibration class with OpenCV an OpenCV calibration.
Definition: CVCalibration.h:71
OpenCV ArUco marker tracker class derived from CVTracked.
const CVArucoParams & params() const
bool track(CVMat imageGray, CVMat imageBgr, CVCalibration *calib)
Tracks the all Aruco markers in the given image for the first sceneview.
static void drawArucoMarkerBoard(int dictionaryId, int numMarkersX, int numMarkersY, float markerEdgeLengthM, float markerSepaM, const string &imgName, float dpi=254.0f, bool showImage=false)
Helper function to draw and save an aruco marker board image.
CVArucoParams _params
Aruco parameters.
int _arucoID
Aruco Marker ID for this node.
static void drawArucoMarker(int dictionaryId, int minMarkerId, int maxMarkerId, int markerSizePX=200)
Helper function to draw and save an aruco marker set.
CVTrackedAruco(int arucoID, string calibIniPath)
bool trackAll(CVMat imageGray, CVMat imageBgr, CVCalibration *calib, CVRect roi=CVRect(0, 0, 0, 0))
vector< int > arucoIDs
detected Aruco marker IDs
CVVMatx44f objectViewMats
object view matrices for all found markers
CVTracked is the pure virtual base class for tracking features in video.
Definition: CVTracked.h:50
std::string readIntoString(std::string path, SLIOStreamKind kind)
Reads an entire file into a string.