70 m_cameraParameters.
width = 640;
71 m_cameraParameters.
height = 480;
78 for (
int i = 0; i < 4; i++)
109 void CCalibration::CalculateInverseTransformation()
118 float d1,
float d2,
float d3,
float d4,
119 const Mat3d &R,
const Vec3d &t,
int width,
int height)
121 m_cameraParameters.
width = width;
122 m_cameraParameters.
height = height;
135 CalculateInverseTransformation();
142 m_cameraParameters.
width = cameraParameters.
width;
156 CalculateInverseTransformation();
162 CalculateInverseTransformation();
168 CalculateInverseTransformation();
215 if (bUseDistortionParameters)
223 if (bUseDistortionParameters)
236 const float d1 = m_cameraParameters.
distortion[0];
237 const float d2 = m_cameraParameters.
distortion[1];
238 const float d3 = m_cameraParameters.
distortion[2];
239 const float d4 = m_cameraParameters.
distortion[3];
248 for (
int i = 0; i < 10; i++)
251 const float rr = x * x + y * y;
252 const float factor = 1 + d1 * rr + d2 * rr * rr;
255 const float dx = d3 * (2 * x * y) + d4 * (rr + 2 * x * x);
256 const float dy = d3 * (rr + 2 * y * y) + d4 * (2 * x * y);
258 x = (x_const - dx) / factor;
259 y = (y_const - dy) / factor;
272 const float d1 = m_cameraParameters.
distortion[0];
273 const float d2 = m_cameraParameters.
distortion[1];
274 const float d3 = m_cameraParameters.
distortion[2];
275 const float d4 = m_cameraParameters.
distortion[3];
278 const float rr = x * x + y * y;
279 const float factor = 1 + d1 * rr + d2 * rr * rr;
282 const float dx = d3 * (2 * x * y) + d4 * (rr + 2 * x * x);
283 const float dy = d3 * (rr + 2 * y * y) + d4 * (2 * x * y);
314 int i, nCameraCount = 0;
316 FILE* f = fopen(pCameraParameterFileName,
"r");
320 if (fscanf(f,
"%d", &nCameraCount) != 1 || nCameraCount < nCamera + 1)
326 float skip_value, w, h;
328 for (i = 0; i < nCamera; i++)
329 for (
int j = 0; j < 27; j++)
330 if (fscanf(f,
"%f", &skip_value) != 1)
336 if (fscanf(f,
"%f", &w) != 1) { fclose(f);
return false; }
337 if (fscanf(f,
"%f", &h) != 1) { fclose(f);
return false; }
338 if (fscanf(f,
"%f", &m_cameraParameters.
focalLength.
x) != 1) { fclose(f);
return false; }
339 if (fscanf(f,
"%f", &skip_value) != 1) { fclose(f);
return false; }
340 if (fscanf(f,
"%f", &m_cameraParameters.
principalPoint.
x) != 1) { fclose(f);
return false; }
341 if (fscanf(f,
"%f", &skip_value) != 1) { fclose(f);
return false; }
342 if (fscanf(f,
"%f", &m_cameraParameters.
focalLength.
y) != 1) { fclose(f);
return false; }
343 if (fscanf(f,
"%f", &m_cameraParameters.
principalPoint.
y) != 1) { fclose(f);
return false; }
344 if (fscanf(f,
"%f", &skip_value) != 1) { fclose(f);
return false; }
345 if (fscanf(f,
"%f", &skip_value) != 1) { fclose(f);
return false; }
346 if (fscanf(f,
"%f", &skip_value) != 1) { fclose(f);
return false; }
347 for (i = 0; i < 4; i++)
348 if (fscanf(f,
"%f", &m_cameraParameters.
distortion[i]) != 1) { fclose(f);
return false; }
349 if (fscanf(f,
"%f", &m_cameraParameters.
rotation.
r1) != 1) { fclose(f);
return false; }
350 if (fscanf(f,
"%f", &m_cameraParameters.
rotation.
r2) != 1) { fclose(f);
return false; }
351 if (fscanf(f,
"%f", &m_cameraParameters.
rotation.
r3) != 1) { fclose(f);
return false; }
352 if (fscanf(f,
"%f", &m_cameraParameters.
rotation.
r4) != 1) { fclose(f);
return false; }
353 if (fscanf(f,
"%f", &m_cameraParameters.
rotation.
r5) != 1) { fclose(f);
return false; }
354 if (fscanf(f,
"%f", &m_cameraParameters.
rotation.
r6) != 1) { fclose(f);
return false; }
355 if (fscanf(f,
"%f", &m_cameraParameters.
rotation.
r7) != 1) { fclose(f);
return false; }
356 if (fscanf(f,
"%f", &m_cameraParameters.
rotation.
r8) != 1) { fclose(f);
return false; }
357 if (fscanf(f,
"%f", &m_cameraParameters.
rotation.
r9) != 1) { fclose(f);
return false; }
358 if (fscanf(f,
"%f", &m_cameraParameters.
translation.
x) != 1) { fclose(f);
return false; }
359 if (fscanf(f,
"%f", &m_cameraParameters.
translation.
y) != 1) { fclose(f);
return false; }
360 if (fscanf(f,
"%f", &m_cameraParameters.
translation.
z) != 1) { fclose(f);
return false; }
362 m_cameraParameters.
width = int(w + 0.5f);
363 m_cameraParameters.
height = int(h + 0.5f);
367 if (bSetExtrinsicToIdentity)
374 CalculateInverseTransformation();
390 const float d1 = m_cameraParameters.
distortion[0];
391 const float d2 = m_cameraParameters.
distortion[1];
392 const float d3 = m_cameraParameters.
distortion[2];
393 const float d4 = m_cameraParameters.
distortion[3];
395 FILE *f = fopen(pFileName,
"w");
400 fprintf(f,
"%i %i %.10f %.10f %.10f %.10f %.10f %.10f %.10f %.10f %.10f ", m_cameraParameters.
width, m_cameraParameters.
height, fx, 0.0, cx, 0.0, fy, cy, 0.0, 0.0, 1.0);
401 fprintf(f,
"%.10f %.10f %.10f %.10f ", d1, d2, d3, d4);
402 fprintf(f,
"%.10f %.10f %.10f %.10f %.10f %.10f %.10f %.10f %.10f ", rotation.
r1, rotation.
r2, rotation.
r3, rotation.
r4, rotation.
r5, rotation.
r6, rotation.
r7, rotation.
r8, rotation.
r9);
403 fprintf(f,
"%.10f %.10f %.10f\n", translation.
x, translation.
y, translation.
z);
412 printf(
"cx/cy = %.10f / %.10f\nfx/fy = %.10f / %.10f\nt = %.10f %.10f %.10f\nR = %.10f %.10f %.10f %.10f %.10f %.10f %.10f %.10f %.10f\nd1/d2/d3/d4 = %.10f %.10f %.10f %.10f\n\n",
void PrintCameraParameters() const
Prints all camera parameters in the console window (STDOUT).
Data structure for the representation of a 2D vector.
void UndistortImageCoordinates(const Vec2d &distortedImagePoint, Vec2d &undistortedImagePoint) const
Transforms 2D distorted image coordinates to 2D undistorted image coordinates.
void SetCameraParameters(float fx, float fy, float cx, float cy, float d1, float d2, float d3, float d4, const Mat3d &R, const Vec3d &t, int width, int height)
Initializes the camera model, given a complete parameter set.
void CameraToWorldCoordinates(const Vec3d &cameraPoint, Vec3d &worldPoint) const
Transforms 3D camera coordinates to 3D world coordinates.
Vec3d m_translation_inverse
Translation vector of the inverted extrinsic transformation.
void CameraToImageCoordinates(const Vec3d &cameraPoint, Vec2d &imagePoint, bool bUseDistortionParameters=true) const
Transforms 3D camera coordinates to 2D image coordinates.
void ImageToWorldCoordinates(const Vec2d &imagePoint, Vec3d &worldPoint, float zc=1.0f, bool bUseDistortionParameters=true) const
Transforms 2D image coordinates to 3D world coordinates.
CCalibration()
The default constructor.
void GetCameraCoordinates(const Vec3d &worldPoint, Vec2d &imagePoint, bool bUseDistortionParameters=true) const
Deprecated.
bool SaveCameraParameters(const char *pCameraParameterFileName) const
Writes the parameters of the camera model to camera parameter file.
void WorldToImageCoordinates(const Vec3d &worldPoint, Vec2d &imagePoint, bool bUseDistortionParameters=true) const
Transforms 3D world coordinates to 2D image coordinates.
const CCameraParameters & GetCameraParameters() const
Gives access to the camera parameters.
bool LoadCameraParameters(const char *pCameraParameterFileName, int nCamera=0, bool bSetExtrinsicToIdentity=false)
Initializes the camera model, given a file path to a camera parameter file.
void MulMatMat(const Mat3d &matrix1, const Mat3d &matrix2, Mat3d &result)
Data structure for the representation of a 3D vector.
void Transpose(const Mat3d &matrix, Mat3d &result)
void DistortCameraCoordinates(const Vec2d &undistortedCameraPoint, Vec2d &distortedCameraPoint) const
Deprecated.
~CCalibration()
The destructor.
void MulMatVec(const Mat3d &matrix, const Vec3d &vec, Vec3d &result)
void Set(const CCalibration &calibration)
Initializes the camera model, given an instance of CCalibration.
void SetDistortion(float d1, float d2, float d3, float d4)
Sets the distortion parameters of the distortion model.
void SetMat(Mat3d &matrix, float r1, float r2, float r3, float r4, float r5, float r6, float r7, float r8, float r9)
Camera model parameters and functions for a single camera.
void UndistortCameraCoordinates(const Vec2d &distortedCameraPoint, Vec2d &undistortedCameraPoint) const
Deprecated.
void SetIntrinsicBase(float cx, float cy, float fx, float fy)
Sets the principal point and the focal lengths.
void SetRotation(const Mat3d &R)
Sets the extrinsic parameter R (rotation matrix).
void GetProjectionMatrix(Mat3d &P1, Vec3d &p2) const
Sets up the projection matrix P.
Mat3d m_rotation_inverse
Rotation matrix of the inverted extrinsic transformation.
Data structure for the representation of a 3x3 matrix.
void DistortImageCoordinates(const Vec2d &undistortedImagePoint, Vec2d &distortedImagePoint) const
Transforms 2D undistorted image coordinates to 2D distorted image coordinates.
void SetTranslation(const Vec3d &t)
Sets the extrinsic parameter t (translation vector).
void SetVec(Vec2d &vec, float x, float y)
void MulVecScalar(const Vec3d &vec, float scalar, Vec3d &result)
void ImageToCameraCoordinates(const Vec2d &imagePoint, Vec3d &cameraPoint, float zc=1.0f, bool bUseDistortionParameters=true) const
Transforms 2D image coordinates to 3D camera coordinates.
void GetCalibrationMatrix(Mat3d &K) const
Sets up the calibration matrix K.
void WorldToCameraCoordinates(const Vec3d &worldPoint, Vec3d &cameraPoint) const
Transforms 3D world coordinates to 3D camera coordinates.
void SetVec(Vec3d &vec, float x, float y, float z)
void GetWorldCoordinates(const Vec2d &imagePoint, Vec3d &worldPoint, float zc=1.0f, bool bUseDistortionParameters=true) const
Deprecated.
Struct containing all parameters of the camera model.