SLProject  4.3.020
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SENSCalibrationEstimator.h
Go to the documentation of this file.
1 /**
2  * \file SENSCalibrationEstimator.h
3  * \authors Michael Goettlicher, Marcus Hudritsch
4  * \date Winter 2019
5  * \remarks Please use clangformat to format the code. See more code style on
6  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
7  * \copyright http://opensource.org/licenses/GPL-3.0
8 */
9 
10 #ifndef SENSCALIBRATIONESTIMATOR_H
11 #define SENSCALIBRATIONESTIMATOR_H
12 
13 #include <future>
14 #include <SENSCalibration.h>
15 
16 using namespace std;
17 
18 //-----------------------------------------------------------------------------
19 //!special exception that informs about errors during calibration process
20 class SENSCalibrationEstimatorException : public std::runtime_error
21 {
22 public:
23  SENSCalibrationEstimatorException(const std::string& msg, const int line, const std::string& file)
24  : std::runtime_error(toMessage(msg, line, file).c_str())
25  {
26  }
27 
28 private:
29  std::string toMessage(const std::string& msg, const int line, const std::string& file)
30  {
31  std::stringstream ss;
32  ss << msg << ": Exception thrown at line " << line << " in " << file << std::endl;
33  return ss.str();
34  }
35 };
36 //-----------------------------------------------------------------------------
38 {
39 public:
40  enum class State
41  {
42  Streaming = 0, //!< Estimator waits for new frames
43  Calculating, //!< Estimator is currently calculating the calibration
44  BusyExtracting, //!< Estimator is busy extracting the corners of a frame
45  Done, //!< Estimator finished
46  DoneCaptureAndSave, //!< All images are captured in
47  Error
48  };
49 
51  int camSizeIndex,
52  bool mirroredH,
53  bool mirroredV,
54  SENSCameraType camType,
55  std::string computerInfos,
56  std::string calibDataPath,
57  std::string imageOutputPath,
58  std::string exePath);
60 
61  bool calculate();
62  bool updateAndDecorate(cv::Mat imageColor,
63  const cv::Mat& imageGray,
64  bool grabFrame,
65  bool drawCorners = true);
66 
68  {
69  return _state;
70  }
71  int numImgsToCapture() { return _numOfImgsToCapture; }
72  int numCapturedImgs() { return _numCaptured; }
73 
74  bool calibrationSuccessful() { return _calibrationSuccessful; }
75  //!Get resulting calibration
76  SENSCalibration getCalibration() { return *_calibration; }
77  bool isBusyExtracting() { return _state == State::BusyExtracting; }
78  bool isCalculating() { return _state == State::Calculating; }
79  bool isStreaming() { return _state == State::Streaming; }
80  bool isDone() { return _state == State::Done; }
81  bool isDoneCaptureAndSave() { return _state == State::DoneCaptureAndSave; }
82 
83  static bool calcCalibration(cv::Size& imageSize,
84  cv::Mat& cameraMatrix,
85  cv::Mat& distCoeffs,
86  const vector<vector<cv::Point2f>>& imagePoints,
87  std::vector<cv::Mat>& rvecs,
88  std::vector<cv::Mat>& tvecs,
89  vector<float>& reprojErrs,
90  float& totalAvgErr,
91  cv::Size& boardSize,
92  float squareSize,
93  int flag,
94  bool useReleaseObjectMethod);
95 
96 private:
97  bool calibrateAsync();
98  bool extractAsync();
99  bool loadCalibParams();
100  void updateExtractAndCalc(bool found, bool grabFrame, cv::Mat imageGray);
101  void updateOnlyCapture(bool found, bool grabFrame, cv::Mat imageGray);
102  void saveImage(cv::Mat imageGray);
103 
104  static double calcReprojectionErrors(const vector<vector<cv::Point3f>>& objectPoints,
105  const vector<vector<cv::Point2f>>& imagePoints,
106  const std::vector<cv::Mat>& rvecs,
107  const std::vector<cv::Mat>& tvecs,
108  const cv::Mat& cameraMatrix,
109  const cv::Mat& distCoeffs,
110  vector<float>& perViewErrors);
111  static void calcBoardCorners3D(const cv::Size& boardSize,
112  float squareSize,
113  std::vector<cv::Point3f>& objectPoints3D);
114 
115  State _state = State::Streaming;
116  bool _calibrationSuccessful = false;
117 
118  std::future<bool> _calibrationTask; //!< future object for calculation of calibration in async task
119 
121  vector<vector<cv::Point2f>> _imagePoints; //!< 2D vector of corner points in chessboard
122  cv::Size _boardSize; //!< NO. of inner chessboard corners.
123  float _boardSquareMM = 10.f; //!< Size of chessboard square in mm
124  int _numOfImgsToCapture = 20; //!< NO. of images to capture
125  int _numCaptured = 0; //!< NO. of images captured
126  cv::Size _imageSize; //!< Input image size in pixels (after cropping)
127  float _reprojectionError = -1.f; //!< Reprojection error after calibration
128 
129  //constructor transfer parameter
131  int _camSizeIndex = -1;
132  bool _mirroredH = false;
133  bool _mirroredV = false;
135  std::unique_ptr<SENSCalibration> _calibration; //!< estimated calibration
136  std::string _calibParamsFileName; //!< name of calibration paramters file
137  std::string _computerInfos;
138  std::string _calibDataPath;
139  std::string _calibImgOutputDir;
140  std::string _exePath;
141 
142  //exception handling from async thread
143  bool _hasAsyncError = false;
145 };
146 
147 #endif // SENSCALIBRATIONESTIMATOR_H
special exception that informs about errors during calibration process
std::string toMessage(const std::string &msg, const int line, const std::string &file)
SENSCalibrationEstimatorException(const std::string &msg, const int line, const std::string &file)
std::future< bool > _calibrationTask
future object for calculation of calibration in async task
std::unique_ptr< SENSCalibration > _calibration
estimated calibration
SENSCalibrationEstimatorException _exception
std::string _calibParamsFileName
name of calibration paramters file
SENSCalibration getCalibration()
Get resulting calibration.
cv::Size _imageSize
Input image size in pixels (after cropping)
SENSCalibrationEstimatorParams _params
cv::Size _boardSize
NO. of inner chessboard corners.
vector< vector< cv::Point2f > > _imagePoints
2D vector of corner points in chessboard
Parameterset for the SENSCalibrationEstimator.