SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
CVTrackedChessboard.cpp
Go to the documentation of this file.
1 /**
2  * \file CVTrackedChessboard.cpp
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 /*
11 The OpenCV library version 3.4 or above with extra module must be present.
12 If the application captures the live video stream with OpenCV you have
13 to define in addition the constant APP_USES_CVCAPTURE.
14 All classes that use OpenCV begin with CV.
15 See also the class docs for CVCapture, CVCalibration and CVTracked
16 for a good top down information.
17 */
18 
19 #include <CVTrackedChessboard.h>
20 #include <SLFileStorage.h>
21 #include <Utils.h>
22 
23 //-----------------------------------------------------------------------------
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 }
41 //-----------------------------------------------------------------------------
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 }
64 //-----------------------------------------------------------------------------
66  float squareSize,
67  CVVPoint3f& objectPoints3D)
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 }
78 
79 //-----------------------------------------------------------------------------
80 //! Tracks the chessboard image in the given image for the first sceneview
82  CVMat imageBgr,
83  CVCalibration* calib)
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 }
142 //------------------------------------------------------------------------------
cv::Point3f CVPoint3f
Definition: CVTypedefs.h:45
vector< cv::Point3f > CVVPoint3f
Definition: CVTypedefs.h:79
cv::FileStorage CVFileStorage
Definition: CVTypedefs.h:61
cv::Size CVSize
Definition: CVTypedefs.h:55
cv::Mat CVMat
Definition: CVTypedefs.h:38
vector< cv::Point2f > CVVPoint2f
Definition: CVTypedefs.h:77
string SLstring
Definition: SL.h:158
@ IOK_config
Definition: SLFileStorage.h:44
Live video camera calibration class with OpenCV an OpenCV calibration.
Definition: CVCalibration.h:71
const CVMat & cameraMat() const
const CVMat & distortion() const
bool track(CVMat imageGray, CVMat imageBgr, CVCalibration *calib) final
Tracks the chessboard image in the given image for the first sceneview.
void calcBoardCorners3D(const CVSize &boardSize, float squareSize, CVVPoint3f &objectPoints3D)
CVTrackedChessboard(string calibIniPath)
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
std::string readIntoString(std::string path, SLIOStreamKind kind)
Reads an entire file into a string.
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
void log(const char *tag, const char *format,...)
logs a formatted string platform independently
Definition: Utils.cpp:1103