22 string imageOutputPath,
25 _camSizeIndex(camSizeIndex),
26 _mirroredH(mirroredH),
27 _mirroredV(mirroredV),
29 _calibration(_camType,
""),
30 _calibParamsFileName(
"calib_in_params.yml"),
31 _exception(
"Undefined error", 0, __FILE__),
32 _computerInfos(computerInfos),
33 _calibDataPath(calibDataPath),
48 ss <<
"Image output directory does not exist: " << imageOutputPath;
85 else if (
_calibrationTask.wait_for(std::chrono::milliseconds(1)) == std::future_status::ready)
90 Utils::log(
"SLProject",
"Calibration succeeded.");
95 Utils::log(
"SLProject",
"Calibration failed.");
115 bool foundPrecisely =
false;
119 int flags = cv::CALIB_CB_ADAPTIVE_THRESH | cv::CALIB_CB_NORMALIZE_IMAGE;
131 cv::TermCriteria(cv::TermCriteria::EPS + cv::TermCriteria::COUNT,
140 catch (std::exception& e)
153 return foundPrecisely;
163 vector<float> reprojErrs;
180 if (!rvecs.empty() || !reprojErrs.empty())
181 _numCaptured = (int)std::max(rvecs.size(), reprojErrs.size());
205 catch (std::exception& e)
228 vector<float>& reprojErrs,
233 bool useReleaseObjectMethod)
236 cameraMatrix = CVMat::eye(3, 3, CV_64F);
239 if (flag & cv::CALIB_FIX_ASPECT_RATIO)
240 cameraMatrix.at<
double>(0, 0) = 1.0;
243 distCoeffs = CVMat::zeros(8, 1, CV_64F);
251 objectPoints.resize(imagePoints.size(), objectPoints[0]);
256 int iFixedPoint = -1;
257 if (useReleaseObjectMethod)
258 iFixedPoint = boardSize.width - 1;
259 double rms = cv::calibrateCameraRO(objectPoints,
270 double rms = cv::calibrateCamera(objectPoints,
283 Utils::log(
"SLProject",
"Re-projection error reported by calibrateCamera: %f", rms);
285 bool ok = cv::checkRange(cameraMatrix) && cv::checkRange(distCoeffs);
302 const CVMat& cameraMatrix,
303 const CVMat& distCoeffs,
304 vector<float>& perViewErrors)
307 size_t totalPoints = 0;
308 double totalErr = 0, err;
309 perViewErrors.resize(objectPoints.size());
311 for (
size_t i = 0; i < objectPoints.size(); ++i)
313 cv::projectPoints(objectPoints[i],
320 err = norm(imagePoints[i], imagePoints2, cv::NORM_L2);
322 size_t n = objectPoints[i].size();
323 perViewErrors[i] = (float)std::sqrt(err * err / n);
324 totalErr += err * err;
328 return std::sqrt(totalErr / totalPoints);
337 fs.open(fullCalibIniFile, cv::FileStorage::READ);
340 Utils::log(
"SLProject",
"Could not open the calibration parameter file: %s", fullCalibIniFile.c_str());
345 fs[
"numInnerCornersWidth"] >>
_boardSize.width;
346 fs[
"numInnerCornersHeight"] >>
_boardSize.height;
355 #ifndef __EMSCRIPTEN__
358 cv::imwrite(ss.str(), imageGray);
368 if (grabFrame && found)
384 if (
_calibrationTask.wait_for(std::chrono::milliseconds(1)) == std::future_status::ready)
408 if (
_calibrationTask.wait_for(std::chrono::milliseconds(1)) == std::future_status::ready)
415 Utils::log(
"SLProject",
"Calibration succeeded.");
420 Utils::log(
"SLProject",
"Calibration failed.");
442 if (grabFrame && found)
461 const CVMat& imageGray,
465 assert(!imageGray.empty() &&
466 "CVCalibration::findChessboard: imageGray is empty!");
467 assert(!imageColor.empty() &&
468 "CVCalibration::findChessboard: imageColor is empty!");
470 "CVCalibration::findChessboard: _boardSize is not set!");
472 cv::Size imageSize = imageColor.size();
474 cv::Mat imageGrayExtract = imageGray;
477 bool doScale =
false;
478 int targetExtractWidth = 640;
479 if (imageSize.width > targetExtractWidth)
482 scale = (double)imageSize.width / (
double)targetExtractWidth;
483 cv::resize(imageGray, imageGrayExtract, cv::Size(), 1 / scale, 1 / scale);
487 bool found = cv::findChessboardCorners(imageGrayExtract,
490 cv::CALIB_CB_FAST_CHECK);
497 cv::bitwise_not(imageColor, imageColor);
505 for (cv::Point2f& pt : corners2D)
511 cv::drawChessboardCorners(imageColor,
538 objectPoints3D.clear();
539 for (
int y = boardSize.height - 1; y >= 0; --y)
540 for (
int x = 0; x < boardSize.width; ++x)
541 objectPoints3D.push_back(
CVPoint3f((
float)x * squareSize,
542 (
float)y * squareSize,
vector< cv::Point3f > CVVPoint3f
vector< cv::Point2f > CVVPoint2f
vector< vector< cv::Point3f > > CVVVPoint3f
vector< vector< cv::Point2f > > CVVVPoint2f
special exception that informs about errors during calibration process
CVCalibrationEstimatorException _exception
cv::Mat _currentImgToExtract
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
bool _calibrationSuccessful
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.
~CVCalibrationEstimator()
string _calibImgOutputDir
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.
bool calibrationSuccessful()
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.
bool useReleaseObjectMethod
Live video camera calibration class with OpenCV an OpenCV calibration.
string findFile(const string &filename, const vector< string > &pathsToCheck)
Tries to find a filename on various paths to check.
string getDateTime2String()
Returns local time as string like "20190213-154611".
string unifySlashes(const string &inputDir, bool withTrailingSlash)
Returns the inputDir string with unified forward slashes, e.g.: "dirA/dirB/".
bool dirExists(const string &path)
Returns true if a directory exists.
bool makeDir(const string &path)
Creates a directory with given path.
void log(const char *tag, const char *format,...)
logs a formatted string platform independently