JeVoisBase  1.3
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
ArUco.C
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: Laurent Itti - 3641 Watt Way, HNB-07A - Los Angeles, CA 90089-2520 - USA.
14 // Tel: +1 213 740 3527 - itti@pollux.usc.edu - http://iLab.usc.edu - http://jevois.org
15 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16 /*! \file */
17 
19 #include <string>
20 
21 // ##############################################################################################################
23 { }
24 
25 // ##############################################################################################################
27 {
28  // Defer reading camera parameters to first processed frame, so we know the resolution:
30 
31  // Initialize default detector parameters:
32  itsDetectorParams = cv::aruco::DetectorParameters::create();
33  itsDetectorParams->doCornerRefinement = true; // do corner refinement in markers by default
34 
35  // Read detector parameters if any:
37  std::string const dpf = aruco::detparams::get();
38  if (dpf.empty() == false)
39  {
40  cv::FileStorage fs(dpf, cv::FileStorage::READ);
41  if (fs.isOpened())
42  {
43  fs["adaptiveThreshWinSizeMin"] >> itsDetectorParams->adaptiveThreshWinSizeMin;
44  fs["adaptiveThreshWinSizeMax"] >> itsDetectorParams->adaptiveThreshWinSizeMax;
45  fs["adaptiveThreshWinSizeStep"] >> itsDetectorParams->adaptiveThreshWinSizeStep;
46  fs["adaptiveThreshConstant"] >> itsDetectorParams->adaptiveThreshConstant;
47  fs["minMarkerPerimeterRate"] >> itsDetectorParams->minMarkerPerimeterRate;
48  fs["maxMarkerPerimeterRate"] >> itsDetectorParams->maxMarkerPerimeterRate;
49  fs["polygonalApproxAccuracyRate"] >> itsDetectorParams->polygonalApproxAccuracyRate;
50  fs["minCornerDistanceRate"] >> itsDetectorParams->minCornerDistanceRate;
51  fs["minDistanceToBorder"] >> itsDetectorParams->minDistanceToBorder;
52  fs["minMarkerDistanceRate"] >> itsDetectorParams->minMarkerDistanceRate;
53  fs["doCornerRefinement"] >> itsDetectorParams->doCornerRefinement;
54  fs["cornerRefinementWinSize"] >> itsDetectorParams->cornerRefinementWinSize;
55  fs["cornerRefinementMaxIterations"] >> itsDetectorParams->cornerRefinementMaxIterations;
56  fs["cornerRefinementMinAccuracy"] >> itsDetectorParams->cornerRefinementMinAccuracy;
57  fs["markerBorderBits"] >> itsDetectorParams->markerBorderBits;
58  fs["perspectiveRemovePixelPerCell"] >> itsDetectorParams->perspectiveRemovePixelPerCell;
59  fs["perspectiveRemoveIgnoredMarginPerCell"] >> itsDetectorParams->perspectiveRemoveIgnoredMarginPerCell;
60  fs["maxErroneousBitsInBorderRate"] >> itsDetectorParams->maxErroneousBitsInBorderRate;
61  fs["minOtsuStdDev"] >> itsDetectorParams->minOtsuStdDev;
62  fs["errorCorrectionRate"] >> itsDetectorParams->errorCorrectionRate;
63  }
64  else LERROR("Failed to read detector parameters from file [" << dpf << "] -- IGNORED");
65  }
66 
67  // Instantiate the disctionary:
68  switch (aruco::dictionary::get())
69  {
70  case aruco::Dict::Original: itsDictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_ARUCO_ORIGINAL);break;
71  case aruco::Dict::D4X4_50: itsDictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_4X4_50); break;
72  case aruco::Dict::D4X4_100: itsDictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_4X4_100); break;
73  case aruco::Dict::D4X4_250: itsDictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_4X4_250); break;
74  case aruco::Dict::D4X4_1000: itsDictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_4X4_1000); break;
75  case aruco::Dict::D5X5_50: itsDictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_5X5_50); break;
76  case aruco::Dict::D5X5_100: itsDictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_5X5_100); break;
77  case aruco::Dict::D5X5_250: itsDictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_5X5_250); break;
78  case aruco::Dict::D5X5_1000: itsDictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_5X5_1000); break;
79  case aruco::Dict::D6X6_50: itsDictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_6X6_50); break;
80  case aruco::Dict::D6X6_100: itsDictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_6X6_100); break;
81  case aruco::Dict::D6X6_250: itsDictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_6X6_250); break;
82  case aruco::Dict::D6X6_1000: itsDictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_6X6_1000); break;
83  case aruco::Dict::D7X7_50: itsDictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_7X7_50); break;
84  case aruco::Dict::D7X7_100: itsDictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_7X7_100); break;
85  case aruco::Dict::D7X7_250: itsDictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_7X7_250); break;
86  case aruco::Dict::D7X7_1000: itsDictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_7X7_1000); break;
87  }
88 }
89 
90 // ##############################################################################################################
92 {
95  itsDictionary.release();
96  itsDetectorParams.release();
97  itsCamMatrix = cv::Mat();
98  itsDistCoeffs = cv::Mat();
99 }
100 
101 // ##############################################################################################################
102 void ArUco::detectMarkers(cv::InputArray image, cv::OutputArray ids, cv::OutputArrayOfArrays corners)
103 {
104  if (itsCamMatrix.empty())
105  {
106  std::string const cpf = absolutePath(aruco::camparams::get() + std::to_string(image.cols()) + 'x' +
107  std::to_string(image.rows()) + ".yaml");
108 
109  cv::FileStorage fs(cpf, cv::FileStorage::READ);
110  if (fs.isOpened())
111  {
112  fs["camera_matrix"] >> itsCamMatrix;
113  fs["distortion_coefficients"] >> itsDistCoeffs;
114  LINFO("Loaded camera calibration from " << cpf);
115  }
116  else LERROR("Failed to read camera parameters from file [" << cpf << "] -- IGNORED");
117  }
118 
119  cv::aruco::detectMarkers(image, itsDictionary, corners, ids, itsDetectorParams);
120 }
121 
122 // ##############################################################################################################
123 void ArUco::estimatePoseSingleMarkers(cv::InputArrayOfArrays corners, float markerLength,
124  cv::OutputArray rvecs, cv::OutputArray tvecs)
125 {
126  cv::aruco::estimatePoseSingleMarkers(corners, markerLength, itsCamMatrix, itsDistCoeffs, rvecs, tvecs);
127 }
128 
129 
void unFreeze()
void detectMarkers(cv::InputArray image, cv::OutputArray ids, cv::OutputArrayOfArrays corners)
Detect markers.
Definition: ArUco.C:102
void estimatePoseSingleMarkers(cv::InputArrayOfArrays corners, float markerLength, cv::OutputArray rvecs, cv::OutputArray tvecs)
Estimate pose of individual markers.
Definition: ArUco.C:123
void postUninit() override
Un-initialize, nuke allocated resources.
Definition: ArUco.C:91
#define LERROR(msg)
void freeze()
virtual ~ArUco()
Destructor.
Definition: ArUco.C:22
cv::Mat itsDistCoeffs
Our current distortion coefficients.
Definition: ArUco.H:144
cv::Mat itsCamMatrix
Our current camera matrix.
Definition: ArUco.H:141
std::string to_string(T const &val)
cv::Ptr< cv::aruco::Dictionary > itsDictionary
Definition: ArUco.H:148
#define LINFO(msg)
void postInit() override
Initialize, create the detector and read the config files.
Definition: ArUco.C:26
cv::Ptr< cv::aruco::DetectorParameters > itsDetectorParams
Definition: ArUco.H:147
std::string absolutePath(std::string const &path="")