SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
CVCalibrationEstimator.h
Go to the documentation of this file.
1 /**
2  * \file CVCalibrationEstimator.h
3  * \date Winter 2019
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 Michael Goettlicher, Marcus Hudritsch
7  * \copyright http://opensource.org/licenses/GPL-3.0
8 */
9 
10 #ifndef CVCALIBRATIONESTIMATOR_H
11 #define CVCALIBRATIONESTIMATOR_H
12 
13 #include <string>
14 #include <future>
15 #include <CVCalibration.h>
16 #include <CVTypedefs.h>
17 #include <CVTypes.h>
18 
19 using std::string;
20 using std::vector;
21 
22 //-----------------------------------------------------------------------------
23 //! special exception that informs about errors during calibration process
24 class CVCalibrationEstimatorException : public std::runtime_error
25 {
26 public:
28  const int line,
29  const string& file)
30  : std::runtime_error(toMessage(msg, line, file).c_str())
31  {
32  }
33 
34 private:
35  string toMessage(const string& msg, const int line, const string& file)
36  {
37  std::stringstream ss;
38  ss << msg << ": Exception thrown at line " << line << " in " << file << std::endl;
39  return ss.str();
40  }
41 };
42 //-----------------------------------------------------------------------------
44 {
45 public:
46  enum class State
47  {
48  Streaming = 0, //!< Estimator waits for new frames
49  Calculating, //!< Estimator is currently calculating the calibration
50  BusyExtracting, //!< Estimator is busy extracting the corners of a frame
51  Done, //!< Estimator finished
52  DoneCaptureAndSave, //!< All images are captured in
53  Error
54  };
55 
57  int camSizeIndex,
58  bool mirroredH,
59  bool mirroredV,
60  CVCameraType camType,
61  string computerInfos,
62  string calibDataPath,
63  string imageOutputPath,
64  string exePath);
66 
67  bool calculate();
68  bool updateAndDecorate(CVMat imageColor,
69  const CVMat& imageGray,
70  bool grabFrame,
71  bool drawCorners = true);
72 
74  {
75  return _state;
76  }
78  int numCapturedImgs() { return _numCaptured; }
79 
81  //! Get resulting calibration
84  bool isCalculating() { return _state == State::Calculating; }
85  bool isStreaming() { return _state == State::Streaming; }
86  bool isDone() { return _state == State::Done; }
88 
89  static bool calcCalibration(CVSize& imageSize,
90  CVMat& cameraMatrix,
91  CVMat& distCoeffs,
92  const CVVVPoint2f& imagePoints,
93  CVVMat& rvecs,
94  CVVMat& tvecs,
95  vector<float>& reprojErrs,
96  float& totalAvgErr,
97  CVSize& boardSize,
98  float squareSize,
99  int flag,
100  bool useReleaseObjectMethod);
101 
102 private:
103  bool calibrateAsync();
104  bool extractAsync();
105  bool loadCalibParams();
106  void updateExtractAndCalc(bool found, bool grabFrame, cv::Mat imageGray);
107  void updateOnlyCapture(bool found, bool grabFrame, cv::Mat imageGray);
108  void saveImage(cv::Mat imageGray);
109 
110  static double calcReprojectionErrors(const CVVVPoint3f& objectPoints,
111  const CVVVPoint2f& imagePoints,
112  const CVVMat& rvecs,
113  const CVVMat& tvecs,
114  const CVMat& cameraMatrix,
115  const CVMat& distCoeffs,
116  vector<float>& perViewErrors);
117  static void calcBoardCorners3D(const CVSize& boardSize,
118  float squareSize,
119  CVVPoint3f& objectPoints3D);
120 
123 
124  std::future<bool> _calibrationTask; //!< future object for calculation of calibration in async task
125 
127  CVVVPoint2f _imagePoints; //!< 2D vector of corner points in chessboard
128  CVSize _boardSize; //!< NO. of inner chessboard corners.
129  float _boardSquareMM = 10.f; //!< Size of chessboard square in mm
130  int _numOfImgsToCapture = 20; //!< NO. of images to capture
131  int _numCaptured = 0; //!< NO. of images captured
132  CVSize _imageSize; //!< Input image size in pixels (after cropping)
133  float _reprojectionError = -1.f; //!< Reprojection error after calibration
134 
135  // constructor transfer parameter
137  int _camSizeIndex = -1;
138  bool _mirroredH = false;
139  bool _mirroredV = false;
141  CVCalibration _calibration; //!< estimated calibration
142  string _calibParamsFileName; //!< name of calibration paramters file
146  string _exePath;
147 
148  // exception handling from async thread
149  bool _hasAsyncError = false;
151 };
152 
153 #endif // CVCALIBRATIONESTIMATOR_H
vector< cv::Point3f > CVVPoint3f
Definition: CVTypedefs.h:79
cv::Size CVSize
Definition: CVTypedefs.h:55
cv::Mat CVMat
Definition: CVTypedefs.h:38
vector< vector< cv::Point3f > > CVVVPoint3f
Definition: CVTypedefs.h:99
vector< cv::Mat > CVVMat
Definition: CVTypedefs.h:73
vector< vector< cv::Point2f > > CVVVPoint2f
Definition: CVTypedefs.h:96
CVCameraType
Definition: CVTypes.h:62
special exception that informs about errors during calibration process
CVCalibrationEstimatorException(const string &msg, const int line, const string &file)
string toMessage(const string &msg, const int line, const string &file)
CVCalibrationEstimatorException _exception
void saveImage(cv::Mat imageGray)
void updateOnlyCapture(bool found, bool grabFrame, cv::Mat imageGray)
bool calculate()
Initiates the final calculation.
CVVVPoint2f _imagePoints
2D vector of corner points in chessboard
string _calibParamsFileName
name of calibration paramters file
CVCalibration getCalibration()
Get resulting calibration.
static bool calcCalibration(CVSize &imageSize, CVMat &cameraMatrix, CVMat &distCoeffs, const CVVVPoint2f &imagePoints, CVVMat &rvecs, CVVMat &tvecs, vector< float > &reprojErrs, float &totalAvgErr, CVSize &boardSize, float squareSize, int flag, bool useReleaseObjectMethod)
Calculates the calibration with the given set of image points.
int _numCaptured
NO. of images captured.
void updateExtractAndCalc(bool found, bool grabFrame, cv::Mat imageGray)
float _reprojectionError
Reprojection error after calibration.
static void calcBoardCorners3D(const CVSize &boardSize, float squareSize, CVVPoint3f &objectPoints3D)
Calculates the 3D positions of the chessboard corners.
bool loadCalibParams()
Loads the chessboard calibration pattern parameters.
CVCalibrationEstimatorParams _params
bool updateAndDecorate(CVMat imageColor, const CVMat &imageGray, bool grabFrame, bool drawCorners=true)
< Finds the inner chessboard corners in the given image
@ BusyExtracting
Estimator is busy extracting the corners of a frame.
@ DoneCaptureAndSave
All images are captured in.
@ Streaming
Estimator waits for new frames.
@ Done
Estimator finished.
@ Calculating
Estimator is currently calculating the calibration.
CVCalibrationEstimator(CVCalibrationEstimatorParams params, int camSizeIndex, bool mirroredH, bool mirroredV, CVCameraType camType, string computerInfos, string calibDataPath, string imageOutputPath, string exePath)
CVSize _boardSize
NO. of inner chessboard corners.
std::future< bool > _calibrationTask
future object for calculation of calibration in async task
int _numOfImgsToCapture
NO. of images to capture.
CVSize _imageSize
Input image size in pixels (after cropping)
float _boardSquareMM
Size of chessboard square in mm.
CVCalibration _calibration
estimated calibration
static double calcReprojectionErrors(const CVVVPoint3f &objectPoints, const CVVVPoint2f &imagePoints, const CVVMat &rvecs, const CVVMat &tvecs, const CVMat &cameraMatrix, const CVMat &distCoeffs, vector< float > &perViewErrors)
Calculates the reprojection error of the calibration.
Live video camera calibration class with OpenCV an OpenCV calibration.
Definition: CVCalibration.h:71