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

OpenCV chessboard tracker class derived from CVTracked. More...

#include <CVTrackedChessboard.h>

Inheritance diagram for CVTrackedChessboard:
[legend]

Public Member Functions

 CVTrackedChessboard (string calibIniPath)
 
bool track (CVMat imageGray, CVMat imageBgr, CVCalibration *calib) final
 Tracks the chessboard image 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 ()
 

Private Member Functions

void calcBoardCorners3D (const CVSize &boardSize, float squareSize, CVVPoint3f &objectPoints3D)
 
bool loadCalibParams ()
 

Private Attributes

string _calibParamsFileName
 
float _edgeLengthM
 
CVVPoint3f _boardPoints3D
 
CVSize _boardSize
 
bool _solved
 
CVMat _rVec
 
CVMat _tVec
 
string _calibIniPath
 

Additional Inherited Members

- 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...
 
- 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...
 
- 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...
 

Detailed Description

OpenCV chessboard tracker class derived from CVTracked.

The chessboard tracker uses the same chessboard pattern as the calibration class defined in CVCalibration. See CVTrackedChessboard::track for the core tracking implementation.

Definition at line 31 of file CVTrackedChessboard.h.

Constructor & Destructor Documentation

◆ CVTrackedChessboard()

CVTrackedChessboard::CVTrackedChessboard ( string  calibIniPath)
explicit

Definition at line 24 of file CVTrackedChessboard.cpp.

25  : _calibParamsFileName("calib_in_params.yml"),
26  _calibIniPath(calibIniPath)
27 {
28  if (!loadCalibParams())
29  {
30  Utils::exitMsg("SLProject",
31  "CVTrackedChessboard: could not load calibration parameter",
32  __LINE__,
33  __FILE__);
34  }
35 
39  _solved = false;
40 }
void calcBoardCorners3D(const CVSize &boardSize, float squareSize, CVVPoint3f &objectPoints3D)
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

◆ calcBoardCorners3D()

void CVTrackedChessboard::calcBoardCorners3D ( const CVSize boardSize,
float  squareSize,
CVVPoint3f objectPoints3D 
)
private

Definition at line 65 of file CVTrackedChessboard.cpp.

68 {
69  // Because OpenCV image coords are top-left we define the according
70  // 3D coords also top-left.
71  objectPoints3D.clear();
72  for (int y = boardSize.height - 1; y >= 0; --y)
73  for (int x = 0; x < boardSize.width; ++x)
74  objectPoints3D.push_back(CVPoint3f((float)x * squareSize,
75  (float)y * squareSize,
76  0));
77 }
cv::Point3f CVPoint3f
Definition: CVTypedefs.h:45

◆ loadCalibParams()

bool CVTrackedChessboard::loadCalibParams ( )
private

Definition at line 42 of file CVTrackedChessboard.cpp.

43 {
44  string fullCalibIniFile = _calibIniPath + _calibParamsFileName;
45  SLstring configString = SLFileStorage::readIntoString(fullCalibIniFile, IOK_config);
46  CVFileStorage fs(configString, CVFileStorage::READ | CVFileStorage::MEMORY);
47 
48  if (!fs.isOpened())
49  {
50  Utils::log("SLProject", "Could not open the calibration parameter file: %s", fullCalibIniFile.c_str());
51  return false;
52  }
53 
54  // assign paramters
55  fs["numInnerCornersWidth"] >> _boardSize.width;
56  fs["numInnerCornersHeight"] >> _boardSize.height;
57  // load edge length in MM
58  fs["squareSizeMM"] >> _edgeLengthM;
59  // convert to M
60  _edgeLengthM *= 0.001f;
61 
62  return true;
63 }
cv::FileStorage CVFileStorage
Definition: CVTypedefs.h:61
string SLstring
Definition: SL.h:158
@ IOK_config
Definition: SLFileStorage.h:44
std::string readIntoString(std::string path, SLIOStreamKind kind)
Reads an entire file into a string.
void log(const char *tag, const char *format,...)
logs a formatted string platform independently
Definition: Utils.cpp:1103

◆ track()

bool CVTrackedChessboard::track ( CVMat  imageGray,
CVMat  imageBgr,
CVCalibration calib 
)
finalvirtual

Tracks the chessboard image in the given image for the first sceneview.

Implements CVTracked.

Definition at line 81 of file CVTrackedChessboard.cpp.

84 {
85  assert(!imageGray.empty() && "ImageGray is empty");
86  assert(!imageBgr.empty() && "ImageBGR is empty");
87  assert(!calib->cameraMat().empty() && "Calibration is empty");
88 
89  ////////////
90  // Detect //
91  ////////////
92 
93  float startMS = _timer.elapsedTimeInMilliSec();
94 
95  // detect chessboard corners
96  int flags = cv::CALIB_CB_FAST_CHECK;
97 
98  CVVPoint2f corners2D;
99 
100  _isVisible = cv::findChessboardCorners(imageGray,
101  _boardSize,
102  corners2D,
103  flags);
104 
106 
107  if (_isVisible)
108  {
109 
110  if (_drawDetection)
111  {
112  cv::drawChessboardCorners(imageBgr, _boardSize, corners2D, true);
113  }
114 
115  /////////////////////
116  // Pose Estimation //
117  /////////////////////
118 
119  startMS = _timer.elapsedTimeInMilliSec();
120 
121  // find the camera extrinsic parameters (rVec & tVec)
122  _solved = solvePnP(CVMat(_boardPoints3D),
123  CVMat(corners2D),
124  calib->cameraMat(),
125  calib->distortion(),
126  _rVec,
127  _tVec,
128  _solved,
129  cv::SOLVEPNP_ITERATIVE);
130 
132 
133  if (_solved)
134  {
136  return true;
137  }
138  }
139 
140  return false;
141 }
cv::Mat CVMat
Definition: CVTypedefs.h:38
vector< cv::Point2f > CVVPoint2f
Definition: CVTypedefs.h:77
const CVMat & cameraMat() const
const CVMat & distortion() const
bool _isVisible
Flag if marker is visible.
Definition: CVTracked.h:91
CVMatx44f _objectViewMat
view transformation matrix
Definition: CVTracked.h:93
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

Member Data Documentation

◆ _boardPoints3D

CVVPoint3f CVTrackedChessboard::_boardPoints3D
private

Definition at line 48 of file CVTrackedChessboard.h.

◆ _boardSize

CVSize CVTrackedChessboard::_boardSize
private

Definition at line 49 of file CVTrackedChessboard.h.

◆ _calibIniPath

string CVTrackedChessboard::_calibIniPath
private

Definition at line 53 of file CVTrackedChessboard.h.

◆ _calibParamsFileName

string CVTrackedChessboard::_calibParamsFileName
private

Definition at line 46 of file CVTrackedChessboard.h.

◆ _edgeLengthM

float CVTrackedChessboard::_edgeLengthM
private

Definition at line 47 of file CVTrackedChessboard.h.

◆ _rVec

CVMat CVTrackedChessboard::_rVec
private

Definition at line 51 of file CVTrackedChessboard.h.

◆ _solved

bool CVTrackedChessboard::_solved
private

Definition at line 50 of file CVTrackedChessboard.h.

◆ _tVec

CVMat CVTrackedChessboard::_tVec
private

Definition at line 52 of file CVTrackedChessboard.h.


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