SLProject  4.3.020
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SENSCalibration Class Reference

#include <SENSCalibration.h>

Public Types

enum class  State { uncalibrated , calibrated , guessed }
 

Public Member Functions

 SENSCalibration (const cv::Mat &cameraMat, const cv::Mat &distortion, cv::Size imageSize, cv::Size boardSize, float boardSquareMM, float reprojectionError, int numCaptured, const string &calibrationTime, int camSizeIndex, bool mirroredH, bool mirroredV, SENSCameraType camType, string computerInfos, int calibFlags, bool calcUndistortionMaps)
 creates a fully defined calibration More...
 
 SENSCalibration (const cv::Size &imageSize, float fovH, bool mirroredH, bool mirroredV, SENSCameraType type, string computerInfos)
 creates a guessed calibration using image size and fovV angle More...
 
 SENSCalibration (float sensorWMM, float sensorHMM, float focalLengthMM, const cv::Size &imageSize, bool mirroredH, bool mirroredV, SENSCameraType camType, string computerInfos)
 create a guessed calibration using sensor size, camera focal length and captured image size More...
 
 SENSCalibration (const cv::Mat &intrinsics, const cv::Size &imageSize, bool mirroredH, bool mirroredV, SENSCameraType camType, const string &computerInfos)
 create a calibration using a known camera matrix (intrinsics) without distortion parameters More...
 
 SENSCalibration (const string &calibDir, const string &calibFileName, bool calcUndistortionMaps)
 ??? More...
 
 ~SENSCalibration ()
 destructor More...
 
 SENSCalibration (const SENSCalibration &other)
 copy constructor More...
 
SENSCalibrationoperator= (const SENSCalibration &other)
 copy assignment operator implicitly defined More...
 
bool save (const string &calibDir, const string &calibFileName)
 Saves the camera calibration parameters to the config file. More...
 
void remap (cv::Mat &inDistorted, cv::Mat &outUndistorted) const
 Undistorts the inDistorted image into the outUndistorted. More...
 
void adaptForNewResolution (const cv::Size &newSize, bool calcUndistortionMaps)
 Adapts an already calibrated camera to a new resolution (cropping and scaling) More...
 
void buildUndistortionMaps ()
 Builds undistortion maps after calibration or loading. More...
 
cv::Size imageSize () const
 
cv::Size imageSizeOriginal () const
 
float imageAspectRatio () const
 
const cv::Mat & cameraMat () const
 
const cv::Mat & cameraMatUndistorted () const
 
const cv::Mat & distortion () const
 
float cameraFovVDeg () const
 
float cameraFovHDeg () const
 
int calibrationFlags () const
 
bool calibFixPrincipalPoint ()
 
bool calibFixAspectRatio ()
 
bool calibZeroTangentDist ()
 
bool calibRationalModel ()
 
bool calibTiltedModel ()
 
bool calibThinPrismModel ()
 
bool isMirroredH () const
 
bool isMirroredV () const
 
bool undistortMapsValid () const
 
float fx () const
 
float fy () const
 
float cx () const
 
float cy () const
 
float k1 () const
 
float k2 () const
 
float p1 () const
 
float p2 () const
 
float k3 () const
 
float k4 () const
 
float k5 () const
 
float k6 () const
 
float s1 () const
 
float s2 () const
 
float s3 () const
 
float s4 () const
 
float tauX () const
 
float tauY () const
 
SENSCameraType camType () const
 
State state () const
 
int numCapturedImgs () const
 
float reprojectionError () const
 
cv::Size boardSize () const
 
float boardSquareMM () const
 
float boardSquareM () const
 
string calibrationTime () const
 
string calibFileName () const
 
string computerInfos () const
 
string stateStr () const
 
void calcCameraFovFromUndistortedCameraMat ()
 Calculates the vertical field of view angle in degrees. More...
 
void calculateUndistortedCameraMat ()
 Calculate a camera matrix that we use for the scene graph and for the reprojection of the undistored image. More...
 

Static Public Member Functions

static 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 More...
 

Private Member Functions

bool load (const string &calibDir, const string &calibFileName, bool calcUndistortionMaps)
 Loads the calibration information from the config file. More...
 
void createFromGuessedFOV (int imageWidthPX, int imageHeightPX, float fovH)
 Calculates camera intrinsics from a guessed FOV angle. More...
 

Private Attributes

cv::Mat _cameraMat
 3x3 Matrix for intrinsic camera matrix More...
 
cv::Mat _distortion
 4x1 Matrix for intrinsic distortion More...
 
cv::Mat _cameraMatOrig
 3x3 Matrix for intrinsic camera matrix (original from loading or calibration estimation) More...
 
cv::Size _imageSizeOrig
 original image size (original from loading or calibration estimation) More...
 
State _state = State::uncalibrated
 calibration state enumeration More...
 
float _cameraFovVDeg = 0.0f
 Vertical field of view in degrees. More...
 
float _cameraFovHDeg = 0.0f
 Horizontal field of view in degrees. More...
 
string _calibFileName
 name for calibration file More...
 
int _calibFlags = 0
 OpenCV calibration flags. More...
 
bool _isMirroredH = false
 Flag if image must be horizontally mirrored. More...
 
bool _isMirroredV = false
 Flag if image must be vertically mirrored. More...
 
int _numCaptured = 0
 NO. of images captured. More...
 
cv::Size _boardSize
 NO. of inner chessboard corners. More...
 
float _boardSquareMM = 20.f
 Size of chessboard square in mm. More...
 
float _reprojectionError = -1.0f
 Reprojection error after calibration. More...
 
cv::Size _imageSize
 Input image size in pixels (after cropping) More...
 
int _camSizeIndex = -1
 The requested camera size index. More...
 
cv::Mat _undistortMapX
 Undistortion float map in x-direction. More...
 
cv::Mat _undistortMapY
 Undistortion float map in y-direction. More...
 
cv::Mat _cameraMatUndistorted
 
string _calibrationTime = "-"
 Time stamp string of calibration. More...
 
string _computerInfos
 
SENSCameraType _camType = SENSCameraType::FRONTFACING
 

Static Private Attributes

static const int _CALIBFILEVERSION = 6
 Global const file format version. More...
 

Detailed Description

Definition at line 18 of file SENSCalibration.h.

Member Enumeration Documentation

◆ State

Enumerator
uncalibrated 

The camera is not calibrated (no calibration found)

calibrated 

The camera is calibrated (mainly this means it has distortion coeffs)

guessed 

The camera intrinsics where estimated from FOV.

Definition at line 21 of file SENSCalibration.h.

22  {
23  uncalibrated, //!< The camera is not calibrated (no calibration found)
24  calibrated, //!< The camera is calibrated (mainly this means it has distortion coeffs)
25  guessed, //!< The camera intrinsics where estimated from FOV
26  };

Constructor & Destructor Documentation

◆ SENSCalibration() [1/6]

SENSCalibration::SENSCalibration ( const cv::Mat &  cameraMat,
const cv::Mat &  distortion,
cv::Size  imageSize,
cv::Size  boardSize,
float  boardSquareMM,
float  reprojectionError,
int  numCaptured,
const string &  calibrationTime,
int  camSizeIndex,
bool  mirroredH,
bool  mirroredV,
SENSCameraType  camType,
string  computerInfos,
int  calibFlags,
bool  calcUndistortionMaps 
)

creates a fully defined calibration

◆ SENSCalibration() [2/6]

SENSCalibration::SENSCalibration ( const cv::Size &  imageSize,
float  fovH,
bool  mirroredH,
bool  mirroredV,
SENSCameraType  type,
string  computerInfos 
)

creates a guessed calibration using image size and fovV angle

◆ SENSCalibration() [3/6]

SENSCalibration::SENSCalibration ( float  sensorWMM,
float  sensorHMM,
float  focalLengthMM,
const cv::Size &  imageSize,
bool  mirroredH,
bool  mirroredV,
SENSCameraType  camType,
string  computerInfos 
)

create a guessed calibration using sensor size, camera focal length and captured image size

◆ SENSCalibration() [4/6]

SENSCalibration::SENSCalibration ( const cv::Mat &  intrinsics,
const cv::Size &  imageSize,
bool  mirroredH,
bool  mirroredV,
SENSCameraType  camType,
const string &  computerInfos 
)

create a calibration using a known camera matrix (intrinsics) without distortion parameters

◆ SENSCalibration() [5/6]

SENSCalibration::SENSCalibration ( const string &  calibDir,
const string &  calibFileName,
bool  calcUndistortionMaps 
)

???

◆ ~SENSCalibration()

SENSCalibration::~SENSCalibration ( )
inline

destructor

Definition at line 76 of file SENSCalibration.h.

77  {
78  }

◆ SENSCalibration() [6/6]

SENSCalibration::SENSCalibration ( const SENSCalibration other)
inline

copy constructor

Definition at line 81 of file SENSCalibration.h.

82  : _cameraMat(other._cameraMat.clone()),
83  _distortion(other._distortion.clone()),
84  _cameraMatOrig(other._cameraMatOrig.clone()),
86  _state(other._state),
90  _calibFlags(other._calibFlags),
94  _boardSize(other._boardSize),
97  _imageSize(other._imageSize),
99  _undistortMapX(other._undistortMapX.clone()),
100  _undistortMapY(other._undistortMapY.clone()),
104  _camType(other._camType)
105  {
106  }
State _state
calibration state enumeration
int _numCaptured
NO. of images captured.
cv::Mat _undistortMapX
Undistortion float map in x-direction.
cv::Mat _cameraMatOrig
3x3 Matrix for intrinsic camera matrix (original from loading or calibration estimation)
cv::Mat _cameraMatUndistorted
int _camSizeIndex
The requested camera size index.
SENSCameraType _camType
float _reprojectionError
Reprojection error after calibration.
float _cameraFovHDeg
Horizontal field of view in degrees.
cv::Size _imageSize
Input image size in pixels (after cropping)
string _calibrationTime
Time stamp string of calibration.
float _boardSquareMM
Size of chessboard square in mm.
cv::Mat _undistortMapY
Undistortion float map in y-direction.
cv::Size _boardSize
NO. of inner chessboard corners.
float _cameraFovVDeg
Vertical field of view in degrees.
bool _isMirroredH
Flag if image must be horizontally mirrored.
cv::Mat _cameraMat
3x3 Matrix for intrinsic camera matrix
cv::Mat _distortion
4x1 Matrix for intrinsic distortion
int _calibFlags
OpenCV calibration flags.
string _calibFileName
name for calibration file
cv::Size _imageSizeOrig
original image size (original from loading or calibration estimation)
bool _isMirroredV
Flag if image must be vertically mirrored.

Member Function Documentation

◆ adaptForNewResolution()

void SENSCalibration::adaptForNewResolution ( const cv::Size &  newSize,
bool  calcUndistortionMaps 
)

Adapts an already calibrated camera to a new resolution (cropping and scaling)

Definition at line 443 of file SENSCalibration.cpp.

444 {
446  return;
447 
448  // new center and focal length in pixels not mm
449  float fx, fy, cy, cx;
450 
451  // use original camera matrix for adaptions.
452  // Otherwise we get rounding errors after too many adaptions.
453  float fxOrig = (float)_cameraMatOrig.at<double>(0, 0);
454  float fyOrig = (float)_cameraMatOrig.at<double>(1, 1);
455  float cxOrig = (float)_cameraMatOrig.at<double>(0, 2);
456  float cyOrig = (float)_cameraMatOrig.at<double>(1, 2);
457 
458  if (((float)newSize.width / (float)newSize.height) >
459  ((float)_imageSizeOrig.width / (float)_imageSizeOrig.height))
460  {
461  float scaleFactor = (float)newSize.width / (float)_imageSizeOrig.width;
462 
463  fx = fxOrig * scaleFactor;
464  fy = fyOrig * scaleFactor;
465  float oldHeightScaled = _imageSizeOrig.height * scaleFactor;
466  float heightDiff = (oldHeightScaled - newSize.height) * 0.5f;
467 
468  cx = cxOrig * scaleFactor;
469  cy = cyOrig * scaleFactor - heightDiff;
470  }
471  else
472  {
473  float scaleFactor = (float)newSize.height / (float)_imageSizeOrig.height;
474  fx = fxOrig * scaleFactor;
475  fy = fyOrig * scaleFactor;
476  float oldWidthScaled = _imageSizeOrig.width * scaleFactor;
477  float widthDiff = (oldWidthScaled - newSize.width) * 0.5f;
478 
479  cx = cxOrig * scaleFactor - widthDiff;
480  cy = cyOrig * scaleFactor;
481  }
482 
483  //std::cout << "adaptForNewResolution: _cameraMat before: " << _cameraMat << std::endl;
484  _cameraMat = (cv::Mat_<double>(3, 3) << fx, 0, cx, 0, fy, cy, 0, 0, 1);
485  //std::cout << "adaptForNewResolution: _cameraMat after: " << _cameraMat << std::endl;
486  //_distortion remains unchanged
488 
489  //std::cout << "adaptForNewResolution: _imageSize before: " << _imageSize << std::endl;
490  _imageSize.width = newSize.width;
491  _imageSize.height = newSize.height;
492  //std::cout << "adaptForNewResolution: _imageSize after: " << _imageSize << std::endl;
493 
496  if (calcUndistortionMaps)
498 }
float fy() const
void buildUndistortionMaps()
Builds undistortion maps after calibration or loading.
float fx() const
@ uncalibrated
The camera is not calibrated (no calibration found)
void calculateUndistortedCameraMat()
Calculate a camera matrix that we use for the scene graph and for the reprojection of the undistored ...
float cy() const
float cx() const
void calcCameraFovFromUndistortedCameraMat()
Calculates the vertical field of view angle in degrees.
string getDateTime2String()
Returns local time as string like "20190213-154611".
Definition: Utils.cpp:289

◆ boardSize()

cv::Size SENSCalibration::boardSize ( ) const
inline

Definition at line 200 of file SENSCalibration.h.

200 { return _boardSize; }

◆ boardSquareM()

float SENSCalibration::boardSquareM ( ) const
inline

Definition at line 202 of file SENSCalibration.h.

202 { return _boardSquareMM * 0.001f; }

◆ boardSquareMM()

float SENSCalibration::boardSquareMM ( ) const
inline

Definition at line 201 of file SENSCalibration.h.

201 { return _boardSquareMM; }

◆ buildUndistortionMaps()

void SENSCalibration::buildUndistortionMaps ( )

Builds undistortion maps after calibration or loading.

Definition at line 350 of file SENSCalibration.cpp.

351 {
352  if (_cameraMatUndistorted.rows != 3 || _cameraMatUndistorted.cols != 3)
353  Utils::exitMsg("SLProject",
354  "SENSCalibration::buildUndistortionMaps: No _cameraMatUndistorted available",
355  __LINE__,
356  __FILE__);
357 
358  // Create undistortion maps
359  _undistortMapX.release();
360  _undistortMapY.release();
361 
362  HighResTimer t;
363  cv::initUndistortRectifyMap(_cameraMat,
364  _distortion,
365  cv::Mat(), // Identity matrix R
367  _imageSize,
368  CV_16SC2, //before we had CV_32FC1 but in all tutorials they use CV_16SC2.. is there a reason?
371  Utils::log("SLProject",
372  "initUndistortRectifyMap: %fms",
374 
375  if (_undistortMapX.empty() || _undistortMapY.empty())
376  Utils::exitMsg("SLProject",
377  "SENSCalibration::buildUndistortionMaps failed.",
378  __LINE__,
379  __FILE__);
380 }
High Resolution Timer class using C++11.
Definition: HighResTimer.h:31
float elapsedTimeInMilliSec()
Definition: HighResTimer.h:38
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:1132
void log(const char *tag, const char *format,...)
logs a formatted string platform independently
Definition: Utils.cpp:1100

◆ calcCameraFovFromUndistortedCameraMat()

void SENSCalibration::calcCameraFovFromUndistortedCameraMat ( )

Calculates the vertical field of view angle in degrees.

Definition at line 567 of file SENSCalibration.cpp.

568 {
569  if (_cameraMatUndistorted.rows != 3 || _cameraMatUndistorted.cols != 3)
570  Utils::exitMsg("SLProject", "SENSCalibration::calcCameraFovFromSceneCameraMat: No _cameraMatUndistorted available", __LINE__, __FILE__);
571 
572  //calculate vertical field of view
573  float fx = (float)_cameraMatUndistorted.at<double>(0, 0);
574  float fy = (float)_cameraMatUndistorted.at<double>(1, 1);
575  float cx = (float)_cameraMatUndistorted.at<double>(0, 2);
576  float cy = (float)_cameraMatUndistorted.at<double>(1, 2);
577  _cameraFovHDeg = 2.0f * atan2(cx, fx) * Utils::RAD2DEG;
578  _cameraFovVDeg = 2.0f * atan2(cy, fy) * Utils::RAD2DEG;
579 }
static const float RAD2DEG
Definition: Utils.h:238

◆ calculateUndistortedCameraMat()

void SENSCalibration::calculateUndistortedCameraMat ( )

Calculate a camera matrix that we use for the scene graph and for the reprojection of the undistored image.

(The following is the algorithm from cv::getOptimalNewCameraMatrix and the code is here for understanding (it does the same))

Definition at line 501 of file SENSCalibration.cpp.

502 {
503  if (_cameraMat.rows != 3 || _cameraMat.cols != 3)
504  Utils::exitMsg("SLProject", "SENSCalibration::calculateUndistortedCameraMat: No intrinsic parameter available", __LINE__, __FILE__);
505 
506  // An alpha of 0 leads to no black borders
507  // An alpha of 1 leads to black borders
508  // (with alpha equaly zero the augmentation fits best)
509  double alpha = 1.0;
510 
511  bool centerPrinciplePoint = true;
512  if (centerPrinciplePoint)
513  {
514  //Attention: the principle point has to be centered because for the projection matrix we assume that image plane is "symmetrically arranged wrt the focal plane"
515  //(see http://kgeorge.github.io/2014/03/08/calculating-opengl-perspective-matrix-from-opencv-intrinsic-matrix)
516  //_cameraMatUndistorted = cv::getOptimalNewCameraMatrix(_cameraMat, _distortion, _imageSize, alpha, _imageSize, nullptr, centerPrinciplePoint);
517  //! (The following is the algorithm from cv::getOptimalNewCameraMatrix and the code is here for understanding (it does the same))
518 
519  double cx0 = _cameraMat.at<double>(0, 2);
520  double cy0 = _cameraMat.at<double>(1, 2);
521  double cx = (_imageSize.width) * 0.5;
522  double cy = (_imageSize.height) * 0.5;
523 
524  cv::Rect_<float> inner, outer;
526  _distortion,
527  cv::Mat(),
528  _cameraMat,
529  _imageSize,
530  inner,
531  outer);
532 
533  double s0 = std::max(std::max(std::max(cx / (cx0 - inner.x),
534  cy / (cy0 - inner.y)),
535  cx / (inner.x + inner.width - cx0)),
536  cy / (inner.y + inner.height - cy0));
537 
538  double s1 = std::min(std::min(std::min(cx / (cx0 - outer.x),
539  cy / (cy0 - outer.y)),
540  cx / (outer.x + outer.width - cx0)),
541  cy / (outer.y + outer.height - cy0));
542 
543  double s = s0 * (1 - alpha) + s1 * alpha;
544 
546  _cameraMatUndistorted.at<double>(0, 0) *= s;
547  _cameraMatUndistorted.at<double>(1, 1) *= s;
548  _cameraMatUndistorted.at<double>(0, 2) = cx;
549  _cameraMatUndistorted.at<double>(1, 2) = cy;
550  }
551  else
552  {
553  _cameraMatUndistorted = cv::getOptimalNewCameraMatrix(_cameraMat,
554  _distortion,
555  _imageSize,
556  alpha,
557  _imageSize,
558  nullptr,
559  centerPrinciplePoint);
560  }
561 
562  //std::cout << "_cameraMatUndistorted: " << _cameraMatUndistorted << std::endl;
563  //std::cout << "_cameraMat: " << _cameraMat << std::endl;
564 }
static 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
float s1() const
The SLScene class represents the top level instance holding the scene structure.
Definition: SLScene.h:47

◆ calibFileName()

string SENSCalibration::calibFileName ( ) const
inline

Definition at line 204 of file SENSCalibration.h.

204 { return _calibFileName; }

◆ calibFixAspectRatio()

bool SENSCalibration::calibFixAspectRatio ( )
inline

Definition at line 168 of file SENSCalibration.h.

168 { return _calibFlags & cv::CALIB_FIX_ASPECT_RATIO; }

◆ calibFixPrincipalPoint()

bool SENSCalibration::calibFixPrincipalPoint ( )
inline

Definition at line 167 of file SENSCalibration.h.

167 { return _calibFlags & cv::CALIB_FIX_PRINCIPAL_POINT; }

◆ calibRationalModel()

bool SENSCalibration::calibRationalModel ( )
inline

Definition at line 170 of file SENSCalibration.h.

170 { return _calibFlags & cv::CALIB_RATIONAL_MODEL; }

◆ calibrationFlags()

int SENSCalibration::calibrationFlags ( ) const
inline

Definition at line 166 of file SENSCalibration.h.

166 { return _calibFlags; }

◆ calibrationTime()

string SENSCalibration::calibrationTime ( ) const
inline

Definition at line 203 of file SENSCalibration.h.

203 { return _calibrationTime; }

◆ calibThinPrismModel()

bool SENSCalibration::calibThinPrismModel ( )
inline

Definition at line 172 of file SENSCalibration.h.

172 { return _calibFlags & cv::CALIB_THIN_PRISM_MODEL; }

◆ calibTiltedModel()

bool SENSCalibration::calibTiltedModel ( )
inline

Definition at line 171 of file SENSCalibration.h.

171 { return _calibFlags & cv::CALIB_TILTED_MODEL; }

◆ calibZeroTangentDist()

bool SENSCalibration::calibZeroTangentDist ( )
inline

Definition at line 169 of file SENSCalibration.h.

169 { return _calibFlags & cv::CALIB_ZERO_TANGENT_DIST; }

◆ cameraFovHDeg()

float SENSCalibration::cameraFovHDeg ( ) const
inline

Definition at line 164 of file SENSCalibration.h.

164 { return _cameraFovHDeg; }

◆ cameraFovVDeg()

float SENSCalibration::cameraFovVDeg ( ) const
inline

Definition at line 163 of file SENSCalibration.h.

163 { return _cameraFovVDeg; }

◆ cameraMat()

const cv::Mat& SENSCalibration::cameraMat ( ) const
inline

Definition at line 160 of file SENSCalibration.h.

160 { return _cameraMat; }

◆ cameraMatUndistorted()

const cv::Mat& SENSCalibration::cameraMatUndistorted ( ) const
inline

Definition at line 161 of file SENSCalibration.h.

161 { return _cameraMatUndistorted; }

◆ camType()

SENSCameraType SENSCalibration::camType ( ) const
inline

Definition at line 196 of file SENSCalibration.h.

196 { return _camType; }

◆ computerInfos()

string SENSCalibration::computerInfos ( ) const
inline

Definition at line 205 of file SENSCalibration.h.

205 { return _computerInfos; }

◆ createFromGuessedFOV()

void SENSCalibration::createFromGuessedFOV ( int  imageWidthPX,
int  imageHeightPX,
float  fovH 
)
private

Calculates camera intrinsics from a guessed FOV angle.

Most laptop-, webcam- or mobile camera have a horizontal view angle or so called field of view (FOV) of around 65 degrees. From this parameter we can calculate the most important intrinsic parameter the focal length. All other parameters are set as if the lens would be perfect: No lens distortion and the view axis goes through the center of the image. If the focal length and sensor size is provided by the device we deduce the the fovV from it.

Parameters
imageWidthPXimage width in pixels
imageHeightPXimage height in pixels
fovHaverage horizontal view angle in degrees

Definition at line 412 of file SENSCalibration.cpp.

415 {
416  //if (fx == fy) and (cx == imgwidth * 0.5f) and (cy == imgheight * 0.5f)
417  float f = (0.5f * imageWidthPX) / tanf(fovH * 0.5f * Utils::DEG2RAD);
418  float fovV = 2.f * atan(0.5f * imageHeightPX / f) * Utils::RAD2DEG;
419 
420  // Create standard camera matrix
421  // fx, fx, cx, cy are all in pixel values not mm
422  // We asume that we have an ideal image sensor with square pixels
423  // so that the focal length fx and fy are identical
424  // See the OpenCV documentation for more details:
425  // http://docs.opencv.org/3.1.0/dc/dbb/tutorial_py_calibration.html
426 
427  float cx = (float)imageWidthPX * 0.5f;
428  float cy = (float)imageHeightPX * 0.5f;
429  float fx = cx / tanf(fovH * 0.5f * Utils::DEG2RAD);
430  float fy = fx;
431 
432  _imageSize.width = imageWidthPX;
433  _imageSize.height = imageHeightPX;
434  _cameraMat = (cv::Mat_<double>(3, 3) << fx, 0, cx, 0, fy, cy, 0, 0, 1);
435  _distortion = (cv::Mat_<double>(5, 1) << 0, 0, 0, 0, 0); // No distortion
436  _cameraFovHDeg = fovH;
437  _cameraFovVDeg = fovV;
440 }
@ guessed
The camera intrinsics where estimated from FOV.
static const float DEG2RAD
Definition: Utils.h:239

◆ cx()

float SENSCalibration::cx ( ) const
inline

Definition at line 179 of file SENSCalibration.h.

179 { return _cameraMat.cols == 3 && _cameraMat.rows == 3 ? (float)_cameraMat.at<double>(0, 2) : 0.0f; }

◆ cy()

float SENSCalibration::cy ( ) const
inline

Definition at line 180 of file SENSCalibration.h.

180 { return _cameraMat.cols == 3 && _cameraMat.rows == 3 ? (float)_cameraMat.at<double>(1, 2) : 0.0f; }

◆ distortion()

const cv::Mat& SENSCalibration::distortion ( ) const
inline

Definition at line 162 of file SENSCalibration.h.

162 { return _distortion; }

◆ fx()

float SENSCalibration::fx ( ) const
inline

Definition at line 177 of file SENSCalibration.h.

177 { return _cameraMat.cols == 3 && _cameraMat.rows == 3 ? (float)_cameraMat.at<double>(0, 0) : 0.0f; }

◆ fy()

float SENSCalibration::fy ( ) const
inline

Definition at line 178 of file SENSCalibration.h.

178 { return _cameraMat.cols == 3 && _cameraMat.rows == 3 ? (float)_cameraMat.at<double>(1, 1) : 0.0f; }

◆ getInnerAndOuterRectangles()

void SENSCalibration::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 
)
static

get inscribed and circumscribed rectangle

Definition at line 296 of file SENSCalibration.cpp.

303 {
304  const int N = 9;
305  // Fill matrix with N * N sampling points
306  cv::Mat pts(N * N, 2, CV_32F);
307  for (int y = 0, k = 0; y < N; y++)
308  {
309  for (int x = 0; x < N; x++)
310  {
311  pts.at<float>(k, 0) = (float)x * imgSize.width / (N - 1);
312  pts.at<float>(k, 1) = (float)y * imgSize.height / (N - 1);
313  k++;
314  }
315  }
316 
317  pts = pts.reshape(2);
318  cv::undistortPoints(pts, pts, cameraMatrix, distCoeffs, R, newCameraMatrix);
319  pts = pts.reshape(1);
320 
321  float iX0 = -FLT_MAX, iX1 = FLT_MAX, iY0 = -FLT_MAX, iY1 = FLT_MAX;
322  float oX0 = FLT_MAX, oX1 = -FLT_MAX, oY0 = FLT_MAX, oY1 = -FLT_MAX;
323  // find the inscribed rectangle.
324  // the code will likely not work with extreme rotation matrices (R) (>45%)
325  for (int y = 0, k = 0; y < N; y++)
326  for (int x = 0; x < N; x++)
327  {
328  cv::Point2f p = {pts.at<float>(k, 0), pts.at<float>(k, 1)};
329  oX0 = MIN(oX0, p.x);
330  oX1 = MAX(oX1, p.x);
331  oY0 = MIN(oY0, p.y);
332  oY1 = MAX(oY1, p.y);
333 
334  if (x == 0)
335  iX0 = MAX(iX0, p.x);
336  if (x == N - 1)
337  iX1 = MIN(iX1, p.x);
338  if (y == 0)
339  iY0 = MAX(iY0, p.y);
340  if (y == N - 1)
341  iY1 = MIN(iY1, p.y);
342  k++;
343  }
344  inner = cv::Rect_<float>(iX0, iY0, iX1 - iX0, iY1 - iY0);
345  outer = cv::Rect_<float>(oX0, oY0, oX1 - oX0, oY1 - oY0);
346 }

◆ imageAspectRatio()

float SENSCalibration::imageAspectRatio ( ) const
inline

Definition at line 159 of file SENSCalibration.h.

159 { return (float)_imageSize.width / (float)_imageSize.height; }

◆ imageSize()

cv::Size SENSCalibration::imageSize ( ) const
inline

Definition at line 156 of file SENSCalibration.h.

156 { return _imageSize; }

◆ imageSizeOriginal()

cv::Size SENSCalibration::imageSizeOriginal ( ) const
inline

Definition at line 157 of file SENSCalibration.h.

157 { return _imageSizeOrig; }

◆ isMirroredH()

bool SENSCalibration::isMirroredH ( ) const
inline

Definition at line 173 of file SENSCalibration.h.

173 { return _isMirroredH; }

◆ isMirroredV()

bool SENSCalibration::isMirroredV ( ) const
inline

Definition at line 174 of file SENSCalibration.h.

174 { return _isMirroredV; }

◆ k1()

float SENSCalibration::k1 ( ) const
inline

Definition at line 181 of file SENSCalibration.h.

181 { return _distortion.rows >= 4 ? (float)_distortion.at<double>(0, 0) : 0.0f; }

◆ k2()

float SENSCalibration::k2 ( ) const
inline

Definition at line 182 of file SENSCalibration.h.

182 { return _distortion.rows >= 4 ? (float)_distortion.at<double>(1, 0) : 0.0f; }

◆ k3()

float SENSCalibration::k3 ( ) const
inline

Definition at line 185 of file SENSCalibration.h.

185 { return _distortion.rows >= 5 ? (float)_distortion.at<double>(4, 0) : 0.0f; }

◆ k4()

float SENSCalibration::k4 ( ) const
inline

Definition at line 186 of file SENSCalibration.h.

186 { return _distortion.rows >= 6 ? (float)_distortion.at<double>(5, 0) : 0.0f; }

◆ k5()

float SENSCalibration::k5 ( ) const
inline

Definition at line 187 of file SENSCalibration.h.

187 { return _distortion.rows >= 7 ? (float)_distortion.at<double>(6, 0) : 0.0f; }

◆ k6()

float SENSCalibration::k6 ( ) const
inline

Definition at line 188 of file SENSCalibration.h.

188 { return _distortion.rows >= 8 ? (float)_distortion.at<double>(7, 0) : 0.0f; }

◆ load()

bool SENSCalibration::load ( const string &  calibDir,
const string &  calibFileName,
bool  calcUndistortionMaps 
)
private

Loads the calibration information from the config file.

Added a flag to disable calculation of undistortion maps because this may take a lot of time for big images on mobile devices

Definition at line 151 of file SENSCalibration.cpp.

154 {
155  //load camera parameter
156  std::string fullPathAndFilename = Utils::unifySlashes(calibDir) + calibFileName;
157 
158  // try to open the local calibration file
159  cv::FileStorage fs(fullPathAndFilename, cv::FileStorage::READ);
160  if (!fs.isOpened())
161  {
162  Utils::log("SLProject", "Calibration : %s", calibFileName.c_str());
163  Utils::log("SLProject", "Calib. created : No. Calib. will be estimated");
164  _numCaptured = 0;
165  _isMirroredH = false;
166  _isMirroredV = false;
167  _reprojectionError = 0;
168  _calibrationTime = "-";
170  _camSizeIndex = -1;
171  return false;
172  }
173 
174  // Reset if new file format version is available
175  int calibFileVersion = 0;
176  fs["CALIBFILEVERSION"] >> calibFileVersion;
177  if (calibFileVersion < _CALIBFILEVERSION)
178  {
179  _numCaptured = 0;
180  _reprojectionError = -1;
181  _calibrationTime = "-";
183  _camSizeIndex = -1;
184  }
185  else
186  {
187  fs["imageSizeWidth"] >> _imageSize.width;
188  fs["imageSizeHeight"] >> _imageSize.height;
189  fs["numCaptured"] >> _numCaptured;
190  fs["isMirroredH"] >> _isMirroredH;
191  fs["isMirroredV"] >> _isMirroredV;
192  fs["cameraMat"] >> _cameraMat;
193  fs["distortion"] >> _distortion;
194  fs["reprojectionError"] >> _reprojectionError;
195  fs["calibrationTime"] >> _calibrationTime;
196  fs["camSizeIndex"] >> _camSizeIndex;
197  fs["boardSizeWidth"] >> _boardSize.width;
198  fs["boardSizeHeight"] >> _boardSize.height;
199  fs["boardSquareMM"] >> _boardSquareMM;
201  }
202 
203  //estimate computer infos
204  if (!fs["computerInfos"].empty())
205  fs["computerInfos"] >> _computerInfos;
206  else
207  {
208  std::vector<std::string> stringParts;
210  if (stringParts.size() >= 3)
211  _computerInfos = stringParts[1];
212  }
213 
214  // close the input file
215  fs.release();
216 
217  //calculate FOV and undistortion maps
218  if (_state == State::calibrated)
219  {
220  //calcCameraFov();
223  if (calcUndistortionMaps)
225  }
226 
227  Utils::log("SLProject", "Calib. loaded : %s", fullPathAndFilename.c_str());
228  Utils::log("SLProject", "Calib. created : %s", _calibrationTime.c_str());
229  Utils::log("SLProject", "Camera FOV H/V : %3.1f/%3.1f", _cameraFovVDeg, _cameraFovHDeg);
230 
231  _cameraMatOrig = _cameraMat.clone();
233 
234  return true;
235 }
@ calibrated
The camera is calibrated (mainly this means it has distortion coeffs)
static const int _CALIBFILEVERSION
Global const file format version.
string calibFileName() const
string unifySlashes(const string &inputDir, bool withTrailingSlash)
Returns the inputDir string with unified forward slashes, e.g.: "dirA/dirB/".
Definition: Utils.cpp:367
string getFileNameWOExt(const string &pathFilename)
Returns the filename without extension.
Definition: Utils.cpp:615
void splitString(const string &s, char delimiter, vector< string > &splits)
Splits an input string at a delimiter character into a string vector.
Definition: Utils.cpp:152

◆ numCapturedImgs()

int SENSCalibration::numCapturedImgs ( ) const
inline

Definition at line 198 of file SENSCalibration.h.

198 { return _numCaptured; }

◆ operator=()

SENSCalibration& SENSCalibration::operator= ( const SENSCalibration other)
inline

copy assignment operator implicitly defined

Definition at line 108 of file SENSCalibration.h.

109  {
110  _cameraMat = other._cameraMat.clone();
111  _distortion = other._distortion.clone();
112  _cameraMatOrig = other._cameraMatOrig.clone();
114  _state = other._state;
118  _calibFlags = other._calibFlags;
119  _isMirroredH = other._isMirroredH;
120  _isMirroredV = other._isMirroredV;
121  _numCaptured = other._numCaptured;
122  _boardSize = other._boardSize;
125  _imageSize = other._imageSize;
127  _undistortMapX = other._undistortMapX.clone();
128  _undistortMapY = other._undistortMapY.clone();
132  _camType = other._camType;
133 
134  return *this;
135  }

◆ p1()

float SENSCalibration::p1 ( ) const
inline

Definition at line 183 of file SENSCalibration.h.

183 { return _distortion.rows >= 4 ? (float)_distortion.at<double>(2, 0) : 0.0f; }

◆ p2()

float SENSCalibration::p2 ( ) const
inline

Definition at line 184 of file SENSCalibration.h.

184 { return _distortion.rows >= 4 ? (float)_distortion.at<double>(3, 0) : 0.0f; }

◆ remap()

void SENSCalibration::remap ( cv::Mat &  inDistorted,
cv::Mat &  outUndistorted 
) const

Undistorts the inDistorted image into the outUndistorted.

Definition at line 383 of file SENSCalibration.cpp.

385 {
386  assert(!inDistorted.empty() &&
387  "Input image is empty!");
388 
389  assert(!_undistortMapX.empty() &&
390  !_undistortMapY.empty() &&
391  "Undistortion Maps are empty!");
392 
393  cv::remap(inDistorted,
394  outUndistorted,
397  cv::INTER_LINEAR);
398 }

◆ reprojectionError()

float SENSCalibration::reprojectionError ( ) const
inline

Definition at line 199 of file SENSCalibration.h.

199 { return _reprojectionError; }

◆ s1()

float SENSCalibration::s1 ( ) const
inline

Definition at line 189 of file SENSCalibration.h.

189 { return _distortion.rows >= 9 ? (float)_distortion.at<double>(8, 0) : 0.0f; }

◆ s2()

float SENSCalibration::s2 ( ) const
inline

Definition at line 190 of file SENSCalibration.h.

190 { return _distortion.rows >= 10 ? (float)_distortion.at<double>(9, 0) : 0.0f; }

◆ s3()

float SENSCalibration::s3 ( ) const
inline

Definition at line 191 of file SENSCalibration.h.

191 { return _distortion.rows >= 11 ? (float)_distortion.at<double>(10, 0) : 0.0f; }

◆ s4()

float SENSCalibration::s4 ( ) const
inline

Definition at line 192 of file SENSCalibration.h.

192 { return _distortion.rows >= 12 ? (float)_distortion.at<double>(11, 0) : 0.0f; }

◆ save()

bool SENSCalibration::save ( const string &  calibDir,
const string &  calibFileName 
)

Saves the camera calibration parameters to the config file.

Definition at line 238 of file SENSCalibration.cpp.

240 {
241  std::string fullPathAndFilename = Utils::unifySlashes(calibDir) + calibFileName;
242 
243  cv::FileStorage fs(fullPathAndFilename, cv::FileStorage::WRITE);
244 
245  if (!fs.isOpened())
246  {
247  Utils::log("SLProject", "Failed to write calib. %s", fullPathAndFilename.c_str());
248  return false;
249  }
250 
251  char buf[1024];
252  snprintf(buf, sizeof(buf),
253  "flags:%s%s%s%s%s%s%s",
254  _calibFlags & cv::CALIB_USE_INTRINSIC_GUESS ? " +use_intrinsic_guess" : "",
255  _calibFlags & cv::CALIB_FIX_ASPECT_RATIO ? " +fix_aspectRatio" : "",
256  _calibFlags & cv::CALIB_FIX_PRINCIPAL_POINT ? " +fix_principal_point" : "",
257  _calibFlags & cv::CALIB_ZERO_TANGENT_DIST ? " +zero_tangent_dist" : "",
258  _calibFlags & cv::CALIB_RATIONAL_MODEL ? " +rational_model" : "",
259  _calibFlags & cv::CALIB_THIN_PRISM_MODEL ? " +thin_prism_model" : "",
260  _calibFlags & cv::CALIB_TILTED_MODEL ? " +tilted_model" : "");
261  fs.writeComment(buf, 0);
262 
263  fs << "CALIBFILEVERSION" << _CALIBFILEVERSION;
264  fs << "calibrationTime" << _calibrationTime;
265  fs << "imageSizeWidth" << _imageSize.width;
266  fs << "imageSizeHeight" << _imageSize.height;
267  fs << "boardSizeWidth" << _boardSize.width; // do not reload
268  fs << "boardSizeHeight" << _boardSize.height; // do not reload
269  fs << "boardSquareMM" << _boardSquareMM; // do not reload
270  fs << "numCaptured" << _numCaptured;
271  fs << "calibFlags" << _calibFlags;
272  fs << "isMirroredH" << _isMirroredH;
273  fs << "isMirroredV" << _isMirroredV;
274  fs << "calibFixAspectRatio" << (_calibFlags & cv::CALIB_FIX_ASPECT_RATIO);
275  fs << "calibFixPrincipalPoint" << (_calibFlags & cv::CALIB_FIX_PRINCIPAL_POINT);
276  fs << "calibZeroTangentDist" << (_calibFlags & cv::CALIB_ZERO_TANGENT_DIST);
277  fs << "calibRationalModel" << (_calibFlags & cv::CALIB_RATIONAL_MODEL);
278  fs << "calibTiltedModel" << (_calibFlags & cv::CALIB_TILTED_MODEL);
279  fs << "calibThinPrismModel" << (_calibFlags & cv::CALIB_THIN_PRISM_MODEL);
280  fs << "cameraMat" << _cameraMat;
281  fs << "distortion" << _distortion;
282  fs << "reprojectionError" << _reprojectionError;
283  fs << "cameraFovVDeg" << _cameraFovVDeg;
284  fs << "cameraFovHDeg" << _cameraFovHDeg;
285  fs << "camSizeIndex" << _camSizeIndex;
286  fs << "computerInfos" << _computerInfos;
287 
288  // close file
289  fs.release();
290  Utils::log("SLProject", "Calib. saved : %s", fullPathAndFilename.c_str());
291  return true;
292  //uploadCalibration(fullPathAndFilename);
293 }

◆ state()

State SENSCalibration::state ( ) const
inline

Definition at line 197 of file SENSCalibration.h.

197 { return _state; }

◆ stateStr()

string SENSCalibration::stateStr ( ) const
inline

Definition at line 206 of file SENSCalibration.h.

207  {
208  switch (_state)
209  {
210  case State::uncalibrated: return "uncalibrated";
211  case State::calibrated: return "calibrated";
212  case State::guessed: return "guessed";
213  default: return "unknown";
214  }
215  }

◆ tauX()

float SENSCalibration::tauX ( ) const
inline

Definition at line 193 of file SENSCalibration.h.

193 { return _distortion.rows >= 13 ? (float)_distortion.at<double>(12, 0) : 0.0f; }

◆ tauY()

float SENSCalibration::tauY ( ) const
inline

Definition at line 194 of file SENSCalibration.h.

194 { return _distortion.rows >= 14 ? (float)_distortion.at<double>(13, 0) : 0.0f; }

◆ undistortMapsValid()

bool SENSCalibration::undistortMapsValid ( ) const
inline

Definition at line 175 of file SENSCalibration.h.

175 { return (!_undistortMapX.empty() && !_undistortMapY.empty()); }

Member Data Documentation

◆ _boardSize

cv::Size SENSCalibration::_boardSize
private

NO. of inner chessboard corners.

Definition at line 248 of file SENSCalibration.h.

◆ _boardSquareMM

float SENSCalibration::_boardSquareMM = 20.f
private

Size of chessboard square in mm.

Definition at line 249 of file SENSCalibration.h.

◆ _calibFileName

string SENSCalibration::_calibFileName
private

name for calibration file

Definition at line 239 of file SENSCalibration.h.

◆ _CALIBFILEVERSION

const int SENSCalibration::_CALIBFILEVERSION = 6
staticprivate

Global const file format version.

Increase the _CALIBFILEVERSION each time you change the file format.

Definition at line 272 of file SENSCalibration.h.

◆ _calibFlags

int SENSCalibration::_calibFlags = 0
private

OpenCV calibration flags.

Definition at line 240 of file SENSCalibration.h.

◆ _calibrationTime

string SENSCalibration::_calibrationTime = "-"
private

Time stamp string of calibration.

Definition at line 268 of file SENSCalibration.h.

◆ _cameraFovHDeg

float SENSCalibration::_cameraFovHDeg = 0.0f
private

Horizontal field of view in degrees.

Definition at line 238 of file SENSCalibration.h.

◆ _cameraFovVDeg

float SENSCalibration::_cameraFovVDeg = 0.0f
private

Vertical field of view in degrees.

Definition at line 237 of file SENSCalibration.h.

◆ _cameraMat

cv::Mat SENSCalibration::_cameraMat
private

3x3 Matrix for intrinsic camera matrix

Definition at line 228 of file SENSCalibration.h.

◆ _cameraMatOrig

cv::Mat SENSCalibration::_cameraMatOrig
private

3x3 Matrix for intrinsic camera matrix (original from loading or calibration estimation)

Definition at line 233 of file SENSCalibration.h.

◆ _cameraMatUndistorted

cv::Mat SENSCalibration::_cameraMatUndistorted
private

Definition at line 267 of file SENSCalibration.h.

◆ _camSizeIndex

int SENSCalibration::_camSizeIndex = -1
private

The requested camera size index.

Definition at line 252 of file SENSCalibration.h.

◆ _camType

SENSCameraType SENSCalibration::_camType = SENSCameraType::FRONTFACING
private

Definition at line 270 of file SENSCalibration.h.

◆ _computerInfos

string SENSCalibration::_computerInfos
private

Definition at line 269 of file SENSCalibration.h.

◆ _distortion

cv::Mat SENSCalibration::_distortion
private

4x1 Matrix for intrinsic distortion

Definition at line 229 of file SENSCalibration.h.

◆ _imageSize

cv::Size SENSCalibration::_imageSize
private

Input image size in pixels (after cropping)

Definition at line 251 of file SENSCalibration.h.

◆ _imageSizeOrig

cv::Size SENSCalibration::_imageSizeOrig
private

original image size (original from loading or calibration estimation)

Definition at line 234 of file SENSCalibration.h.

◆ _isMirroredH

bool SENSCalibration::_isMirroredH = false
private

Flag if image must be horizontally mirrored.

Definition at line 242 of file SENSCalibration.h.

◆ _isMirroredV

bool SENSCalibration::_isMirroredV = false
private

Flag if image must be vertically mirrored.

Definition at line 244 of file SENSCalibration.h.

◆ _numCaptured

int SENSCalibration::_numCaptured = 0
private

NO. of images captured.

Definition at line 247 of file SENSCalibration.h.

◆ _reprojectionError

float SENSCalibration::_reprojectionError = -1.0f
private

Reprojection error after calibration.

Definition at line 250 of file SENSCalibration.h.

◆ _state

State SENSCalibration::_state = State::uncalibrated
private

calibration state enumeration

Definition at line 236 of file SENSCalibration.h.

◆ _undistortMapX

cv::Mat SENSCalibration::_undistortMapX
private

Undistortion float map in x-direction.

Definition at line 254 of file SENSCalibration.h.

◆ _undistortMapY

cv::Mat SENSCalibration::_undistortMapY
private

Undistortion float map in y-direction.

Definition at line 255 of file SENSCalibration.h.


The documentation for this class was generated from the following files: