38 _reprojectionError(-1.0f),
40 _calibrationTime(
"-"),
44 _computerInfos(std::move(computerInfos))
50 const cv::Mat& distortion,
54 float reprojectionError,
56 const string& calibrationTime,
63 bool calcUndistortionMaps)
64 : _cameraMat(cameraMat.clone()),
65 _distortion(distortion.clone()),
66 _imageSize(std::move(imageSize)),
67 _boardSize(std::move(boardSize)),
68 _boardSquareMM(boardSquareMM),
69 _reprojectionError(reprojectionError),
70 _numCaptured(numCaptured),
71 _calibrationTime(calibrationTime),
72 _camSizeIndex(camSizeIndex),
73 _isMirroredH(mirroredH),
74 _isMirroredV(mirroredV),
76 _computerInfos(std::move(computerInfos)),
77 _calibFlags(calibFlags)
95 : _isMirroredH(mirroredH),
96 _isMirroredV(mirroredV),
98 _computerInfos(std::move(computerInfos))
109 const cv::Size& imageSize,
113 string computerInfos)
114 : _isMirroredH(mirroredH),
115 _isMirroredV(mirroredV),
117 _computerInfos(std::move(computerInfos))
120 float devFovH = 2.0f * atan(sensorWMM / (2.0f * focalLengthMM)) *
Utils::RAD2DEG;
121 if (devFovH > 60.0f && devFovH < 70.0f)
139 const string& calibFileName,
140 bool calcUndistortionMaps)
146 cv::FileStorage fs(fullPathAndFilename, cv::FileStorage::READ);
150 Utils::log(
"SLProject",
"Calib. created : No. Calib. will be estimated");
162 int calibFileVersion = 0;
163 fs[
"CALIBFILEVERSION"] >> calibFileVersion;
191 if (!fs[
"computerInfos"].empty())
195 vector<string> stringParts;
197 if (stringParts.size() >= 3)
210 if (calcUndistortionMaps)
214 Utils::log(
"SLProject",
"Calib. loaded : %s", fullPathAndFilename.c_str());
226 const string& calibFileName)
230 cv::FileStorage fs(fullPathAndFilename, cv::FileStorage::WRITE);
234 Utils::log(
"SLProject",
"Failed to write calib. %s", fullPathAndFilename.c_str());
241 "flags:%s%s%s%s%s%s%s",
242 _calibFlags & cv::CALIB_USE_INTRINSIC_GUESS ?
" +use_intrinsic_guess" :
"",
243 _calibFlags & cv::CALIB_FIX_ASPECT_RATIO ?
" +fix_aspectRatio" :
"",
244 _calibFlags & cv::CALIB_FIX_PRINCIPAL_POINT ?
" +fix_principal_point" :
"",
245 _calibFlags & cv::CALIB_ZERO_TANGENT_DIST ?
" +zero_tangent_dist" :
"",
246 _calibFlags & cv::CALIB_RATIONAL_MODEL ?
" +rational_model" :
"",
247 _calibFlags & cv::CALIB_THIN_PRISM_MODEL ?
" +thin_prism_model" :
"",
248 _calibFlags & cv::CALIB_TILTED_MODEL ?
" +tilted_model" :
"");
249 fs.writeComment(buf, 0);
262 fs <<
"calibFixAspectRatio" << (
_calibFlags & cv::CALIB_FIX_ASPECT_RATIO);
263 fs <<
"calibFixPrincipalPoint" << (
_calibFlags & cv::CALIB_FIX_PRINCIPAL_POINT);
264 fs <<
"calibZeroTangentDist" << (
_calibFlags & cv::CALIB_ZERO_TANGENT_DIST);
265 fs <<
"calibRationalModel" << (
_calibFlags & cv::CALIB_RATIONAL_MODEL);
266 fs <<
"calibTiltedModel" << (
_calibFlags & cv::CALIB_TILTED_MODEL);
267 fs <<
"calibThinPrismModel" << (
_calibFlags & cv::CALIB_THIN_PRISM_MODEL);
278 Utils::log(
"SLProject",
"Calib. saved : %s", fullPathAndFilename.c_str());
285 const cv::Mat& distCoeffs,
287 const cv::Mat& newCameraMatrix,
288 const cv::Size& imgSize,
289 cv::Rect_<float>& inner,
290 cv::Rect_<float>& outer)
294 cv::Mat pts(N * N, 2, CV_32F);
295 for (
int y = 0, k = 0; y < N; y++)
297 for (
int x = 0; x < N; x++)
299 pts.at<
float>(k, 0) = (
float)x * (float)imgSize.width / (N - 1);
300 pts.at<
float>(k, 1) = (
float)y * (float)imgSize.height / (N - 1);
305 pts = pts.reshape(2);
306 cv::undistortPoints(pts,
312 pts = pts.reshape(1);
314 float iX0 = -FLT_MAX, iX1 = FLT_MAX, iY0 = -FLT_MAX, iY1 = FLT_MAX;
315 float oX0 = FLT_MAX, oX1 = -FLT_MAX, oY0 = FLT_MAX, oY1 = -FLT_MAX;
318 for (
int y = 0, k = 0; y < N; y++)
319 for (
int x = 0; x < N; x++)
321 cv::Point2f p = {pts.at<
float>(k, 0),
322 pts.at<
float>(k, 1)};
338 inner = cv::Rect_<float>(iX0, iY0, iX1 - iX0, iY1 - iY0);
339 outer = cv::Rect_<float>(oX0, oY0, oX1 - oX0, oY1 - oY0);
348 "CVCalibration::buildUndistortionMaps: No _cameraMatUndistorted available",
367 "initUndistortRectifyMap: %fms",
372 "CVCalibration::buildUndistortionMaps failed.",
379 CVMat& outUndistorted)
381 assert(!inDistorted.empty() &&
382 "Input image is empty!");
386 "Undistortion Maps are empty!");
388 cv::remap(inDistorted,
412 float f = (0.5f * (float)imageWidthPX) / tanf(fovH * 0.5f *
Utils::DEG2RAD);
413 float fovV = 2.0f * (float)atan2(0.5f * (
float)imageHeightPX, f) *
Utils::RAD2DEG;
422 float cx = (float)imageWidthPX * 0.5f;
423 float cy = (float)imageHeightPX * 0.5f;
430 _distortion = (cv::Mat_<double>(5, 1) << 0, 0, 0, 0, 0);
453 if (((
float)newSize.width / (float)newSize.height) >
456 float scaleFactor = (float)newSize.width / (
float)
_imageSizeOrig.width;
458 fx = fxOrig * scaleFactor;
459 fy = fyOrig * scaleFactor;
460 float oldHeightScaled = (float)
_imageSizeOrig.height * scaleFactor;
461 float heightDiff = (oldHeightScaled - (
float)newSize.height) * 0.5f;
463 cx = cxOrig * scaleFactor;
464 cy = cyOrig * scaleFactor - heightDiff;
468 float scaleFactor = (float)newSize.height / (
float)
_imageSizeOrig.height;
469 fx = fxOrig * scaleFactor;
470 fy = fyOrig * scaleFactor;
472 float widthDiff = (oldWidthScaled - (
float)newSize.width) * 0.5f;
474 cx = cxOrig * scaleFactor - widthDiff;
475 cy = cyOrig * scaleFactor;
491 if (calcUndistortionMaps)
499 Utils::exitMsg(
"SLProject",
"CVCalibration::calculateUndistortedCameraMat: No intrinsic parameter available", __LINE__, __FILE__);
506 bool centerPrinciplePoint =
true;
507 if (centerPrinciplePoint)
519 cv::Rect_<float> inner, outer;
528 double s0 = std::max(std::max(std::max((
double)
cx / (cx0 - inner.x),
529 (
double)
cy / (cy0 - inner.y)),
530 (
double)
cx / (inner.x + inner.width - cx0)),
531 (
double)
cy / (inner.y + inner.height - cy0));
533 double s1 = std::min(std::min(std::min((
double)
cx / (cx0 - outer.x),
534 (
double)
cy / (cy0 - outer.y)),
535 (
double)
cx / (outer.x + outer.width - cx0)),
536 (
double)
cy / (outer.y + outer.height - cy0));
538 double s = s0 * (1 - alpha) +
s1 * alpha;
554 centerPrinciplePoint);
565 Utils::exitMsg(
"SLProject",
"CVCalibration::calcCameraFovFromSceneCameraMat: No _cameraMatUndistorted available", __LINE__, __FILE__);
void getInnerAndOuterRectangles(const cv::Mat &cameraMatrix, const cv::Mat &distCoeffs, const cv::Mat &R, const cv::Mat &newCameraMatrix, const cv::Size &imgSize, cv::Rect_< float > &inner, cv::Rect_< float > &outer)
get inscribed and circumscribed rectangle
@ CS_calibrated
The camera is calibrated.
@ CS_uncalibrated
The camera is not calibrated (no calibration found)
@ CS_guessed
The camera intrinsics where estimated from FOV.
float _boardSquareMM
Size of chessboard square in mm.
float _cameraFovVDeg
Vertical field of view in degrees.
void calculateUndistortedCameraMat()
Calculate a camera matrix that we use for the scene graph and for the reprojection of the undistorted...
bool _isMirroredV
Flag if image must be vertically mirrored.
float _reprojectionError
Reprojection error after calibration.
CVMat _undistortMapX
Undistortion float map in x-direction.
static const int _CALIBFILEVERSION
Global const file format version.
void calcCameraFovFromUndistortedCameraMat()
Calculates the vertical field of view angle in degrees.
CVCalibState _state
calibration state enumeration
void remap(CVMat &inDistorted, CVMat &outUndistorted)
Undistorts the inDistorted image into the outUndistorted.
CVMat _cameraMatUndistorted
Camera matrix that defines scene camera and may also be used for reprojection of undistorted image.
void createFromGuessedFOV(int imageWidthPX, int imageHeightPX, float fovH)
Calculates camera intrinsics from a guessed FOV angle.
CVMat _undistortMapY
Undistortion float map in y-direction.
CVSize _imageSize
Input image size in pixels (after cropping)
CVCalibration(CVCameraType camType, string computerInfos)
void adaptForNewResolution(const CVSize &newSize, bool calcUndistortionMaps)
Adapts an already calibrated camera to a new resolution (cropping and scaling)
CVMat _distortion
4x1 Matrix for intrinsic distortion
float _cameraFovHDeg
Horizontal field of view in degrees.
string _calibrationTime
Time stamp string of calibration.
bool load(const string &calibDir, const string &calibFileName, bool calcUndistortionMaps)
Loads the calibration information from the config file.
CVMat _cameraMat
3x3 Matrix for intrinsic camera matrix
int _camSizeIndex
The requested camera size index.
CVSize _imageSizeOrig
original image size (original from loading or calibration estimation)
string calibFileName() const
int _numCaptured
NO. of images captured.
bool _isMirroredH
Flag if image must be horizontally mirrored.
CVMat _cameraMatOrig
3x3 Matrix for intrinsic camera matrix (original from loading or calibration estimation)
bool save(const string &calibDir, const string &calibFileName)
Saves the camera calibration parameters to the config file.
CVSize _boardSize
NO. of inner chessboard corners.
void buildUndistortionMaps()
Builds undistortion maps after calibration or loading.
int _calibFlags
OpenCV calibration flags.
High Resolution Timer class using C++11.
float elapsedTimeInMilliSec()
The SLScene class represents the top level instance holding the scene structure.
string getDateTime2String()
Returns local time as string like "20190213-154611".
static const float DEG2RAD
string unifySlashes(const string &inputDir, bool withTrailingSlash)
Returns the inputDir string with unified forward slashes, e.g.: "dirA/dirB/".
string getFileNameWOExt(const string &pathFilename)
Returns the filename without extension.
void splitString(const string &s, char delimiter, vector< string > &splits)
Splits an input string at a delimiter character into a string vector.
void exitMsg(const char *tag, const char *msg, const int line, const char *file)
Terminates the application with a message. No leak checking.
static const float RAD2DEG
void log(const char *tag, const char *format,...)
logs a formatted string platform independently