JeVoisBase  1.0
JeVois Smart Embedded Machine Vision Toolkit Base Modules
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 
20 // ##############################################################################################################
22 { }
23 
24 // ##############################################################################################################
26 {
27  // Read camera parameters if any:
29  std::string const cpf = aruco::camparams::get();
30  if (cpf.empty() == false)
31  {
32  cv::FileStorage fs(cpf, cv::FileStorage::READ);
33  if (fs.isOpened())
34  {
35  fs["camera_matrix"] >> itsCamMatrix;
36  fs["distortion_coefficients"] >> itsDistCoeffs;
37  }
38  else LERROR("Failed to read camera parameters from file [" << cpf << "] -- IGNORED");
39  }
40 
41  // Read detector parameters if any:
43  std::string const dpf = aruco::detparams::get();
44  itsDetectorParams.reset(new cv::aruco::DetectorParameters());
45  itsDetectorParams->doCornerRefinement = true; // do corner refinement in markers by default
46  if (dpf.empty() == false)
47  {
48  cv::FileStorage fs(dpf, cv::FileStorage::READ);
49  if (fs.isOpened())
50  {
51  fs["adaptiveThreshWinSizeMin"] >> itsDetectorParams->adaptiveThreshWinSizeMin;
52  fs["adaptiveThreshWinSizeMax"] >> itsDetectorParams->adaptiveThreshWinSizeMax;
53  fs["adaptiveThreshWinSizeStep"] >> itsDetectorParams->adaptiveThreshWinSizeStep;
54  fs["adaptiveThreshConstant"] >> itsDetectorParams->adaptiveThreshConstant;
55  fs["minMarkerPerimeterRate"] >> itsDetectorParams->minMarkerPerimeterRate;
56  fs["maxMarkerPerimeterRate"] >> itsDetectorParams->maxMarkerPerimeterRate;
57  fs["polygonalApproxAccuracyRate"] >> itsDetectorParams->polygonalApproxAccuracyRate;
58  fs["minCornerDistanceRate"] >> itsDetectorParams->minCornerDistanceRate;
59  fs["minDistanceToBorder"] >> itsDetectorParams->minDistanceToBorder;
60  fs["minMarkerDistanceRate"] >> itsDetectorParams->minMarkerDistanceRate;
61  fs["doCornerRefinement"] >> itsDetectorParams->doCornerRefinement;
62  fs["cornerRefinementWinSize"] >> itsDetectorParams->cornerRefinementWinSize;
63  fs["cornerRefinementMaxIterations"] >> itsDetectorParams->cornerRefinementMaxIterations;
64  fs["cornerRefinementMinAccuracy"] >> itsDetectorParams->cornerRefinementMinAccuracy;
65  fs["markerBorderBits"] >> itsDetectorParams->markerBorderBits;
66  fs["perspectiveRemovePixelPerCell"] >> itsDetectorParams->perspectiveRemovePixelPerCell;
67  fs["perspectiveRemoveIgnoredMarginPerCell"] >> itsDetectorParams->perspectiveRemoveIgnoredMarginPerCell;
68  fs["maxErroneousBitsInBorderRate"] >> itsDetectorParams->maxErroneousBitsInBorderRate;
69  fs["minOtsuStdDev"] >> itsDetectorParams->minOtsuStdDev;
70  fs["errorCorrectionRate"] >> itsDetectorParams->errorCorrectionRate;
71  }
72  else LERROR("Failed to read detector parameters from file [" << dpf << "] -- IGNORED");
73  }
74 
75  // Instantiate the disctionary:
76  switch (aruco::dictionary::get())
77  {
78  case aruco::Dict::Original: itsDictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_ARUCO_ORIGINAL);break;
79  case aruco::Dict::D4X4_50: itsDictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_4X4_50); break;
80  case aruco::Dict::D4X4_100: itsDictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_4X4_100); break;
81  case aruco::Dict::D4X4_250: itsDictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_4X4_250); break;
82  case aruco::Dict::D4X4_1000: itsDictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_4X4_1000); break;
83  case aruco::Dict::D5X5_50: itsDictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_5X5_50); break;
84  case aruco::Dict::D5X5_100: itsDictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_5X5_100); break;
85  case aruco::Dict::D5X5_250: itsDictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_5X5_250); break;
86  case aruco::Dict::D5X5_1000: itsDictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_5X5_1000); break;
87  case aruco::Dict::D6X6_50: itsDictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_6X6_50); break;
88  case aruco::Dict::D6X6_100: itsDictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_6X6_100); break;
89  case aruco::Dict::D6X6_250: itsDictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_6X6_250); break;
90  case aruco::Dict::D6X6_1000: itsDictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_6X6_1000); break;
91  case aruco::Dict::D7X7_50: itsDictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_7X7_50); break;
92  case aruco::Dict::D7X7_100: itsDictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_7X7_100); break;
93  case aruco::Dict::D7X7_250: itsDictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_7X7_250); break;
94  case aruco::Dict::D7X7_1000: itsDictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_7X7_1000); break;
95  }
96 }
97 
98 // ##############################################################################################################
100 {
103  itsDictionary.release();
104  itsDetectorParams.release();
105  itsCamMatrix = cv::Mat();
106  itsDistCoeffs = cv::Mat();
107 }
108 
109 // ##############################################################################################################
110 void ArUco::detectMarkers(cv::InputArray image, cv::OutputArray ids, cv::OutputArrayOfArrays corners)
111 {
112  cv::aruco::detectMarkers(image, itsDictionary, corners, ids, itsDetectorParams);
113 }
114 
115 // ##############################################################################################################
116 void ArUco::estimatePoseSingleMarkers(cv::InputArrayOfArrays corners, float markerLength,
117  cv::OutputArray rvecs, cv::OutputArray tvecs)
118 {
119  cv::aruco::estimatePoseSingleMarkers(corners, markerLength, itsCamMatrix, itsDistCoeffs, rvecs, tvecs);
120 }
121 
122 
void unFreeze()
void detectMarkers(cv::InputArray image, cv::OutputArray ids, cv::OutputArrayOfArrays corners)
Detect markers.
Definition: ArUco.C:110
void estimatePoseSingleMarkers(cv::InputArrayOfArrays corners, float markerLength, cv::OutputArray rvecs, cv::OutputArray tvecs)
Estimate pose of individual markers.
Definition: ArUco.C:116
void postUninit() override
Un-initialize, nuke allocated resources.
Definition: ArUco.C:99
#define LERROR(msg)
void freeze()
virtual ~ArUco()
Destructor.
Definition: ArUco.C:21
cv::Mat itsDistCoeffs
Our current distortion coefficients.
Definition: ArUco.H:128
cv::Mat itsCamMatrix
Our current camera matrix.
Definition: ArUco.H:125
cv::Ptr< cv::aruco::Dictionary > itsDictionary
Definition: ArUco.H:132
void postInit() override
Initialize, create the detector and read the config files.
Definition: ArUco.C:25
cv::Ptr< cv::aruco::DetectorParameters > itsDetectorParams
Definition: ArUco.H:131