SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
CVTrackedAruco Class Reference

OpenCV ArUco marker tracker class derived from CVTracked. More...

#include <CVTrackedAruco.h>

Inheritance diagram for CVTrackedAruco:
[legend]

Public Member Functions

 CVTrackedAruco (int arucoID, string calibIniPath)
 
const CVArucoParamsparams () const
 
bool track (CVMat imageGray, CVMat imageBgr, CVCalibration *calib)
 Tracks the all Aruco markers in the given image for the first sceneview. More...
 
- Public Member Functions inherited from CVTracked
 CVTracked ()
 
virtual ~CVTracked ()=default
 
void drawDetection (bool draw)
 
bool isVisible ()
 
bool drawDetection ()
 
CVMatx44f objectViewMat ()
 

Static Public Member Functions

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. More...
 
static void drawArucoMarker (int dictionaryId, int minMarkerId, int maxMarkerId, int markerSizePX=200)
 Helper function to draw and save an aruco marker set. More...
 
- Static Public Member Functions inherited from CVTracked
static cv::Matx44f createGLMatrix (const CVMat &tVec, const CVMat &rVec)
 Create an OpenGL 4x4 matrix from an OpenCV translation & rotation vector. More...
 
static void createRvecTvec (const CVMatx44f &glMat, CVMat &tVec, CVMat &rVec)
 Creates the OpenCV rvec & tvec vectors from an column major OpenGL 4x4 matrix. More...
 
static CVMatx44f calcObjectMatrix (const CVMatx44f &cameraObjectMat, const CVMatx44f &objectViewMat)
 
static CVVec3f averageVector (vector< CVVec3f > vectors, vector< float > weights)
 
static SLQuat4f averageQuaternion (vector< SLQuat4f > quaternions, vector< float > weights)
 
static void resetTimes ()
 Resets all static variables. More...
 

Protected Member Functions

bool trackAll (CVMat imageGray, CVMat imageBgr, CVCalibration *calib, CVRect roi=CVRect(0, 0, 0, 0))
 

Protected Attributes

vector< int > arucoIDs
 detected Aruco marker IDs More...
 
CVVMatx44f objectViewMats
 object view matrices for all found markers More...
 
- Protected Attributes inherited from CVTracked
bool _isVisible
 Flag if marker is visible. More...
 
bool _drawDetection
 Flag if detection should be drawn into image. More...
 
CVMatx44f _objectViewMat
 view transformation matrix More...
 
HighResTimer _timer
 High resolution timer. More...
 

Private Attributes

CVArucoParams _params
 Aruco parameters. More...
 
int _arucoID
 Aruco Marker ID for this node. More...
 
string _calibIniPath
 

Additional Inherited Members

- Static Public Attributes inherited from CVTracked
static AvgFloat trackingTimesMS
 Averaged time for video tracking in ms. More...
 
static AvgFloat detectTimesMS
 Averaged time for video feature detection & description in ms. More...
 
static AvgFloat detect1TimesMS
 Averaged time for video feature detection subpart 1 in ms. More...
 
static AvgFloat detect2TimesMS
 Averaged time for video feature detection subpart 2 in ms. More...
 
static AvgFloat matchTimesMS
 Averaged time for video feature matching in ms. More...
 
static AvgFloat optFlowTimesMS
 Averaged time for video feature optical flow tracking in ms. More...
 
static AvgFloat poseTimesMS
 Averaged time for video feature pose estimation in ms. More...
 

Detailed Description

OpenCV ArUco marker tracker class derived from CVTracked.

Tracking class for ArUco markers tracking. See the official OpenCV docs on ArUco markers: http://docs.opencv.org/3.1.0/d5/dae/tutorial_aruco_detection.html The aruco marker used in the SLProject are printed in a PDF stored in the data/Calibration folder. They use the dictionary 0 and where generated with the functions CVTrackedAruco::drawArucoMarkerBoard and CVTrackedAruco::drawArucoMarker.

Definition at line 130 of file CVTrackedAruco.h.

Constructor & Destructor Documentation

◆ CVTrackedAruco()

CVTrackedAruco::CVTrackedAruco ( int  arucoID,
string  calibIniPath 
)
explicit

Definition at line 23 of file CVTrackedAruco.cpp.

24  : _calibIniPath(calibIniPath),
25  _arucoID(arucoID)
26 {
27  SLbool paramsLoaded = _params.loadFromFile(_calibIniPath);
28 
29  if (!paramsLoaded)
30  Utils::exitMsg("SLProject",
31  "CVTrackedAruco::track: Failed to load Aruco parameters.",
32  __LINE__,
33  __FILE__);
34 }
bool SLbool
Definition: SL.h:175
bool loadFromFile(string calibIniPath)
CVArucoParams _params
Aruco parameters.
int _arucoID
Aruco Marker ID for this node.
void exitMsg(const char *tag, const char *msg, const int line, const char *file)
Terminates the application with a message. No leak checking.
Definition: Utils.cpp:1135

Member Function Documentation

◆ drawArucoMarker()

void CVTrackedAruco::drawArucoMarker ( int  dictionaryId,
int  minMarkerId,
int  maxMarkerId,
int  markerSizePX = 200 
)
static

Helper function to draw and save an aruco marker set.

Definition at line 249 of file CVTrackedAruco.cpp.

253 {
254  assert(dictionaryId > 0);
255  assert(minMarkerId > 0);
256  assert(minMarkerId < maxMarkerId);
257 
258 #if CV_MAJOR_VERSION < 4 || CV_MINOR_VERSION < 7
259  cv::Ptr<cv::aruco::Dictionary> dict = getPredefinedDictionary(cv::aruco::PREDEFINED_DICTIONARY_NAME(dictionaryId));
260  if (maxMarkerId > dict->bytesList.rows)
261  maxMarkerId = dict->bytesList.rows;
262 
263  CVMat markerImg;
264 
265  for (int i = minMarkerId; i < maxMarkerId; ++i)
266  {
267  cv::aruco::drawMarker(dict, i, markerSizePX, markerImg, 1);
268 # ifndef __EMSCRIPTEN__
269  imwrite(Utils::formatString("ArucoMarker_Dict%d_%dpx_Id%d.png",
270  dictionaryId,
271  markerSizePX,
272  i),
273  markerImg);
274 # endif
275  }
276 #else
277  cv::aruco::Dictionary dict = getPredefinedDictionary(cv::aruco::PredefinedDictionaryType(dictionaryId));
278  if (maxMarkerId > dict.bytesList.rows)
279  maxMarkerId = dict.bytesList.rows;
280 
281  CVMat markerImg;
282 
283  for (int i = minMarkerId; i < maxMarkerId; ++i)
284  {
285  cv::aruco::generateImageMarker(dict,
286  i,
287  markerSizePX,
288  markerImg,
289  1);
290 # ifndef __EMSCRIPTEN__
291  imwrite(Utils::formatString("ArucoMarker_Dict%d_%dpx_Id%d.png",
292  dictionaryId,
293  markerSizePX,
294  i),
295  markerImg);
296 # endif
297  }
298 #endif
299 }
cv::Mat CVMat
Definition: CVTypedefs.h:38
string formatString(string fmt_str,...)
Returns a formatted string as sprintf.
Definition: Utils.cpp:320

◆ drawArucoMarkerBoard()

void CVTrackedAruco::drawArucoMarkerBoard ( int  dictionaryId,
int  numMarkersX,
int  numMarkersY,
float  markerEdgeM,
float  markerSepaM,
const string &  imgName,
float  dpi = 254.0f,
bool  showImage = false 
)
static

Helper function to draw and save an aruco marker board image.

CVTrackedAruco::drawArucoMarkerBoard draws and saves an aruco board into an image.

Parameters
dictionaryIdinteger id of the dictionary
numMarkersXNO. of markers in x-direction
numMarkersYNO. of markers in y-direction
markerEdgeMLength of one marker in meters
markerSepaMSeparation between markers in meters
imgNameImage filename inklusive format extension
dpiDots per inch (default 256)
showImageShows image in window (default false)

Definition at line 182 of file CVTrackedAruco.cpp.

190 {
191 #if CV_MAJOR_VERSION < 4 || CV_MINOR_VERSION < 7
192  cv::Ptr<cv::aruco::Dictionary> dictionary = cv::aruco::getPredefinedDictionary(cv::aruco::PREDEFINED_DICTIONARY_NAME(dictionaryId));
193  cv::Ptr<cv::aruco::GridBoard> board = cv::aruco::GridBoard::create(numMarkersX,
194  numMarkersY,
195  markerEdgeM,
196  markerSepaM,
197  dictionary);
198  CVSize imageSize;
199  imageSize.width = (int)((markerEdgeM + markerSepaM) * 100.0f / 2.54f * dpi * (float)numMarkersX);
200  imageSize.height = (int)((markerEdgeM + markerSepaM) * 100.0f / 2.54f * dpi * (float)numMarkersY);
201 
202  imageSize.width -= (imageSize.width % 4);
203  imageSize.height -= (imageSize.height % 4);
204 
205  // show created board
206  CVMat boardImage;
207  board->draw(imageSize, boardImage, 0, 1);
208 
209  if (showImage)
210  {
211  imshow("board", boardImage);
212  cv::waitKey(0);
213  }
214 #else
215  cv::aruco::Dictionary dictionary = cv::aruco::getPredefinedDictionary(cv::aruco::PredefinedDictionaryType(dictionaryId));
216  cv::aruco::GridBoard board = cv::aruco::GridBoard(cv::Size(numMarkersX, numMarkersY),
217  markerEdgeM,
218  markerSepaM,
219  dictionary);
220 
221  CVSize imageSize;
222  imageSize.width = (int)((markerEdgeM + markerSepaM) * 100.0f / 2.54f * dpi * (float)numMarkersX);
223  imageSize.height = (int)((markerEdgeM + markerSepaM) * 100.0f / 2.54f * dpi * (float)numMarkersY);
224 
225  imageSize.width -= (imageSize.width % 4);
226  imageSize.height -= (imageSize.height % 4);
227 
228  // show created board
229  CVMat boardImage;
230  cv::aruco::drawPlanarBoard(&board,
231  imageSize,
232  boardImage,
233  0,
234  1);
235 # ifndef __EMSCRIPTEN__
236  if (showImage)
237  {
238  imshow("board", boardImage);
239  cv::waitKey(0);
240  }
241 # endif
242 #endif
243 
244 #ifndef __EMSCRIPTEN__
245  imwrite(imgName, boardImage);
246 #endif
247 }
static SLint dpi
Dot per inch resolution of screen.
Definition: AppGLFW.cpp:41
cv::Size CVSize
Definition: CVTypedefs.h:55

◆ params()

const CVArucoParams& CVTrackedAruco::params ( ) const
inline

Definition at line 136 of file CVTrackedAruco.h.

136 { return _params; }

◆ track()

bool CVTrackedAruco::track ( CVMat  imageGray,
CVMat  imageBgr,
CVCalibration calib 
)
virtual

Tracks the all Aruco markers in the given image for the first sceneview.

Implements CVTracked.

Reimplemented in CVTrackedArucoCube.

Definition at line 37 of file CVTrackedAruco.cpp.

40 {
41  if (!trackAll(imageGray, imageBgr, calib))
42  {
43  return false;
44  }
45 
46  if (!arucoIDs.empty())
47  {
48  // Find the marker with the matching id
49  for (size_t i = 0; i < arucoIDs.size(); ++i)
50  {
51  if (arucoIDs[i] == _arucoID)
52  {
54  return true;
55  }
56  }
57  }
58 
59  return false;
60 }
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
CVMatx44f _objectViewMat
view transformation matrix
Definition: CVTracked.h:93

◆ trackAll()

bool CVTrackedAruco::trackAll ( CVMat  imageGray,
CVMat  imageBgr,
CVCalibration calib,
CVRect  roi = CVRect(0, 0, 0, 0) 
)
protected

Definition at line 62 of file CVTrackedAruco.cpp.

66 {
68 
69  assert(!imageGray.empty() && "ImageGray is empty");
70  assert(!imageBgr.empty() && "ImageBGR is empty");
71  assert(!calib->cameraMat().empty() && "Calibration is empty");
72 
73 #if CV_MAJOR_VERSION < 4 || CV_MINOR_VERSION < 7
74  if (_params.arucoParams.empty() || _params.dictionary.empty())
75  {
76  Utils::warnMsg("SLProject",
77  "CVTrackedAruco::track: Aruco paramters are empty.",
78  __LINE__,
79  __FILE__);
80  return false;
81  }
82 #endif
83 
84  ////////////
85  // Detect //
86  ////////////
87 
88  CVMat croppedImageGray = roi.empty() ? imageGray : imageGray(roi);
89 
90  float startMS = _timer.elapsedTimeInMilliSec();
91 
92  arucoIDs.clear();
93  objectViewMats.clear();
94  CVVVPoint2f corners, rejected;
95 
96 #if CV_MAJOR_VERSION < 4 || CV_MINOR_VERSION < 7
97  cv::aruco::detectMarkers(croppedImageGray,
99  corners,
100  arucoIDs,
102  rejected);
103 #else
104  cv::aruco::ArucoDetector detector(_params.dictionary, _params.arucoParams);
105  detector.detectMarkers(croppedImageGray,
106  corners,
107  arucoIDs,
108  rejected);
109 #endif
110 
111  for (auto& corner : corners)
112  {
113  for (auto& j : corner)
114  {
115  j.x += (float)roi.x;
116  j.y += (float)roi.y;
117  }
118  }
119 
121 
122  if (!arucoIDs.empty())
123  {
124  if (_drawDetection)
125  cv::aruco::drawDetectedMarkers(imageBgr,
126  corners,
127  arucoIDs,
128  cv::Scalar(0, 0, 255));
129 
130  /////////////////////
131  // Pose Estimation //
132  /////////////////////
133 
134  startMS = _timer.elapsedTimeInMilliSec();
135 
136  // find the camera extrinsic parameters (rVec & tVec)
137  CVVPoint3d rVecs, tVecs;
138  cv::aruco::estimatePoseSingleMarkers(corners,
140  calib->cameraMat(),
141  calib->distortion(),
142  rVecs,
143  tVecs);
144 
146 
147  // Get the object view matrix for all aruco markers
148  for (size_t i = 0; i < arucoIDs.size(); ++i)
149  {
150  CVMatx44f ovm = createGLMatrix(cv::Mat(tVecs[i]), cv::Mat(rVecs[i]));
151  objectViewMats.push_back(ovm);
152 
153  if (_drawDetection)
154  {
155 #if CV_MAJOR_VERSION < 4 || CV_MINOR_VERSION < 6
156 #else
157  cv::drawFrameAxes(imageBgr,
158  calib->cameraMat(),
159  calib->distortion(),
160  cv::Mat(rVecs[i]),
161  cv::Mat(tVecs[i]),
162  0.01f);
163 #endif
164  }
165  }
166  }
167 
168  return true;
169 }
cv::Matx44f CVMatx44f
Definition: CVTypedefs.h:59
vector< cv::Point3d > CVVPoint3d
Definition: CVTypedefs.h:80
vector< vector< cv::Point2f > > CVVVPoint2f
Definition: CVTypedefs.h:96
#define PROFILE_FUNCTION()
Definition: Instrumentor.h:41
cv::Ptr< cv::aruco::DetectorParameters > arucoParams
detector parameter structure for aruco detection function
float edgeLength
marker edge length
cv::Ptr< cv::aruco::Dictionary > dictionary
predefined dictionary
const CVMat & cameraMat() const
const CVMat & distortion() const
static AvgFloat detectTimesMS
Averaged time for video feature detection & description in ms.
Definition: CVTracked.h:83
bool _drawDetection
Flag if detection should be drawn into image.
Definition: CVTracked.h:92
static cv::Matx44f createGLMatrix(const CVMat &tVec, const CVMat &rVec)
Create an OpenGL 4x4 matrix from an OpenCV translation & rotation vector.
Definition: CVTracked.cpp:46
HighResTimer _timer
High resolution timer.
Definition: CVTracked.h:94
static AvgFloat poseTimesMS
Averaged time for video feature pose estimation in ms.
Definition: CVTracked.h:88
float elapsedTimeInMilliSec()
Definition: HighResTimer.h:38
void set(T value)
Sets the current value in the value array and builds the average.
Definition: Averaged.h:53
void warnMsg(const char *tag, const char *msg, const int line, const char *file)
Platform independent warn message output.
Definition: Utils.cpp:1145

Member Data Documentation

◆ _arucoID

int CVTrackedAruco::_arucoID
private

Aruco Marker ID for this node.

Definition at line 169 of file CVTrackedAruco.h.

◆ _calibIniPath

string CVTrackedAruco::_calibIniPath
private

Definition at line 170 of file CVTrackedAruco.h.

◆ _params

CVArucoParams CVTrackedAruco::_params
private

Aruco parameters.

Definition at line 168 of file CVTrackedAruco.h.

◆ arucoIDs

vector<int> CVTrackedAruco::arucoIDs
protected

detected Aruco marker IDs

Definition at line 164 of file CVTrackedAruco.h.

◆ objectViewMats

CVVMatx44f CVTrackedAruco::objectViewMats
protected

object view matrices for all found markers

Definition at line 165 of file CVTrackedAruco.h.


The documentation for this class was generated from the following files: