74 Vec3d t = { -100, 0, 0 };
80 delete m_pLeftCalibration;
81 delete m_pRightCalibration;
123 m_pLeftCalibration->
SetDistortion(d1_left, d2_left, d3_left, d4_left);
124 m_pRightCalibration->
SetDistortion(d1_right, d2_right, d3_right, d4_right);
129 m_pLeftCalibration->
Set(leftCalibration);
130 m_pRightCalibration->
Set(rightCalibration);
135 if (bTransformLeftCameraToIdentity)
137 TransformLeftCameraToIdentity();
138 CalculateFundamentalMatrix();
142 Mat3d left_rotation, right_rotation;
143 Vec3d left_translation, right_translation;
153 TransformLeftCameraToIdentity();
154 CalculateFundamentalMatrix();
175 TransformLeftCameraToIdentity();
178 CalculateFundamentalMatrix();
180 if(!bTransformToIdentity)
192 void CStereoCalibration::TransformLeftCameraToIdentity()
211 void CStereoCalibration::CalculateFundamentalMatrix()
218 Mat3d E = { 0, -t.
z, t.
y, t.
z, 0, -t.
x, -t.
y, t.
x, 0 };
237 Vec2d pointLeft, pointRight;
239 if (bInputImagesAreRectified)
266 float A00 = -u.
x;
float A01 = v.
x;
267 float A10 = -u.
y;
float A11 = v.
y;
268 float A20 = -u.
z;
float A21 = v.
z;
269 float b0 = a.
x - b.
x;
270 float b1 = a.
y - b.
y;
271 float b2 = a.
z - b.
z;
273 float ATA00 = A00 * A00 + A10 * A10 + A20 * A20;
274 float ATA10 = A01 * A00 + A11 * A10 + A21 * A20;
275 float ATA11 = A01 * A01 + A11 * A11 + A21 * A21;
276 float ATb0 = A00 * b0 + A10 * b1 + A20 * b2;
277 float ATb1 = A01 * b0 + A11 * b1 + A21 * b2;
279 float L00 = sqrtf(ATA00);
280 float L10 = ATA10 / L00;
281 float L11 = sqrtf(ATA11 - L10 * L10);
283 float yy0 = ATb0 / L00;
284 float yy1 = (ATb1 - L10 * yy0) / L11;
287 float r = (yy0 - L10 * s) / L00;
291 (r * u.
x + a.
x + s * v.
x + b.
x) * 0.5f,
292 (r * u.
y + a.
y + s * v.
y + b.
y) * 0.5f,
293 (r * u.
z + a.
z + s * v.
z + b.
z) * 0.5f
306 Vec3d x = { pointInRightImage.
x, pointInRightImage.
y, 1 };
324 const float v1 = l.
y != 0.0f ? -l.
z / l.
y : -1.0f;
325 const float v2 = l.
y != 0.0f ? (-l.
z - l.
x * float(
width - 1)) / l.
y : -1.0f;
326 const float u1 = l.
x != 0.0f ? -l.
z / l.
x : -1.0f;
327 const float u2 = l.
x != 0.0f ? (-l.
z - l.
y *
float(
height - 1)) / l.
x : -1.0f;
329 if (v1 >= 0.0f && v1 <=
float(
height - 1))
331 else if (v2 >= 0.0f && v2 <=
float(
height - 1))
333 else if (u1 >= 0.0f && u1 <=
float(
width - 1))
335 else if (u2 >= 0.0f && u2 <=
float(
width - 1))
338 if (v1 >= 0.0f && v1 <=
float(
height - 1) && (epipolarLine.
p1.
x != 0.0f || epipolarLine.
p1.
y != v1))
340 else if (v2 >= 0.0f && v2 <=
float(
height - 1) && (epipolarLine.
p1.
x !=
float(
width - 1) || epipolarLine.
p1.
y != v2))
342 else if (u1 >= 0.0f && u1 <=
float(
width - 1) && (epipolarLine.
p1.
x != u1 || epipolarLine.
p1.
y != 0.0f))
344 else if (u2 >= 0.0f && u2 <=
float(
width - 1) && (epipolarLine.
p1.
x != u2 || epipolarLine.
p1.
y !=
float(
height - 1)))
351 Vec3d x = { pointInLeftImage.
x, pointInLeftImage.
y, 1 };
369 const float v1 = l.
y != 0.0f ? -l.
z / l.
y : -1.0f;
370 const float v2 = l.
y != 0.0f ? (-l.
z - l.
x * float(
width - 1)) / l.
y : -1.0f;
371 const float u1 = l.
x != 0.0f ? -l.
z / l.
x : -1.0f;
372 const float u2 = l.
x != 0.0f ? (-l.
z - l.
y *
float(
height - 1)) / l.
x : -1.0f;
374 if (v1 >= 0.0f && v1 <=
float(
height - 1))
376 else if (v2 >= 0.0f && v2 <=
float(
height - 1))
378 else if (u1 >= 0.0f && u1 <=
float(
width - 1))
380 else if (u2 >= 0.0f && u2 <=
float(
width - 1))
383 if (v1 >= 0.0f && v1 <=
float(
height - 1) && (epipolarLine.
p1.
x != 0.0f || epipolarLine.
p1.
y != v1))
385 else if (v2 >= 0.0f && v2 <=
float(
height - 1) && (epipolarLine.
p1.
x !=
float(
width - 1) || epipolarLine.
p1.
y != v2))
387 else if (u1 >= 0.0f && u1 <=
float(
width - 1) && (epipolarLine.
p1.
x != u1 || epipolarLine.
p1.
y != 0.0f))
389 else if (u2 >= 0.0f && u2 <=
float(
width - 1) && (epipolarLine.
p1.
x != u2 || epipolarLine.
p1.
y !=
float(
height - 1)))
399 const float length = sqrtf(l.
x * l.
x + l.
y * l.
y);
400 const float l1 = l.
x / length;
401 const float l2 = l.
y / length;
402 const float l3 = l.
z / length;
404 return l1 * pointInLeftImage.
x + l2 * pointInLeftImage.
y + l3;
412 const float length = sqrtf(l.
x * l.
x + l.
y * l.
y);
413 const float l1 = l.
x / length;
414 const float l2 = l.
y / length;
415 const float l3 = l.
z / length;
417 return l1 * pointInRightImage.
x + l2 * pointInRightImage.
y + l3;
429 if (bTransformLeftCameraToIdentity)
431 TransformLeftCameraToIdentity();
432 CalculateFundamentalMatrix();
436 Mat3d left_rotation, right_rotation;
437 Vec3d left_translation, right_translation;
447 TransformLeftCameraToIdentity();
448 CalculateFundamentalMatrix();
458 int nCameraCount = 0;
460 FILE* f = fopen(pCameraParameterFileName,
"r");
464 if (fscanf(f,
"%d", &nCameraCount) != 1 || nCameraCount != 2)
473 for (
int i = 0; i < 2 * 27 + 16; i++)
474 if (fscanf(f,
"%f", &skip_value) != 1)
511 FILE *f = fopen(pFileName,
"w");
525 float d1 = cameraParametersLeft.
distortion[0];
526 float d2 = cameraParametersLeft.
distortion[1];
527 float d3 = cameraParametersLeft.
distortion[2];
528 float d4 = cameraParametersLeft.
distortion[3];
530 fprintf(f,
"%.10f %.10f %.10f %.10f %.10f %.10f %.10f %.10f %.10f %.10f %.10f ",
float(cameraParametersLeft.
width),
float(cameraParametersLeft.
height), fx, 0.0f, cx, 0.0f, fy, cy, 0.0f, 0.0f, 1.0f);
531 fprintf(f,
"%.10f %.10f %.10f %.10f ", d1, d2, d3, d4);
532 fprintf(f,
"%.10f %.10f %.10f %.10f %.10f %.10f %.10f %.10f %.10f ", cameraParametersLeft.
rotation.
r1, cameraParametersLeft.
rotation.
r2, cameraParametersLeft.
rotation.
r3, cameraParametersLeft.
rotation.
r4, cameraParametersLeft.
rotation.
r5, cameraParametersLeft.
rotation.
r6, cameraParametersLeft.
rotation.
r7, cameraParametersLeft.
rotation.
r8, cameraParametersLeft.
rotation.
r9);
545 fprintf(f,
"%.10f %.10f %.10f %.10f %.10f %.10f %.10f %.10f %.10f %.10f %.10f ",
float(cameraParametersLeft.
width),
float(cameraParametersLeft.
height), fx, 0.0f, cx, 0.0f, fy, cy, 0.0f, 0.0f, 1.0f);
546 fprintf(f,
"%.10f %.10f %.10f %.10f ", d1, d2, d3, d4);
547 fprintf(f,
"%.10f %.10f %.10f %.10f %.10f %.10f %.10f %.10f %.10f ", cameraParametersRight.
rotation.
r1, cameraParametersRight.
rotation.
r2, cameraParametersRight.
rotation.
r3, cameraParametersRight.
rotation.
r4, cameraParametersRight.
rotation.
r5, cameraParametersRight.
rotation.
r6, cameraParametersRight.
rotation.
r7, cameraParametersRight.
rotation.
r8, cameraParametersRight.
rotation.
r9);
551 fprintf(f,
"%.10f %.10f %.10f %.10f %.10f %.10f %.10f %.10f\n", 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
552 fprintf(f,
"%.10f %.10f %.10f %.10f %.10f %.10f %.10f %.10f\n", 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
~CStereoCalibration()
The destructor.
void Invert(const Mat3d &matrix, Mat3d &result)
const CCalibration * GetRightCalibration() const
Access to the instance of CCalibration for the camera model of the right camera.
void ApplyHomography(const Mat3d &A, const Vec2d &p, Vec2d &result)
bool SaveCameraParameters(const char *pCameraParameterFileName) const
Writes the parameters of the camera model to camera parameter file.
Data structure for the representation of a 2D vector.
void CalculateEpipolarLineInRightImage(const Vec2d &pointInLeftImage, Vec3d &l)
Given an image point in the left image, computes the corresponding epipolar line in the right image...
void Calculate3DPoint(const Vec2d &cameraPointLeft, const Vec2d &cameraPointRight, Vec3d &worldPoint, bool bInputImagesAreRectified, bool bUseDistortionParameters=true, PointPair3d *pConnectionLine=0)
Computes a 3D point, given a point correspondence in both images, by performing stereo triangulation...
Vec3d m_translation_inverse
Translation vector of the inverted extrinsic transformation.
bool LoadCameraParameters(const char *pCameraParameterFileName, bool bTransformLeftCameraToIdentity=true)
Initializes the stereo camera model, given a file path to a stereo camera parameter file...
void ImageToWorldCoordinates(const Vec2d &imagePoint, Vec3d &worldPoint, float zc=1.0f, bool bUseDistortionParameters=true) const
Transforms 2D image coordinates to 3D world coordinates.
float CalculateEpipolarLineInLeftImageDistance(const Vec2d &pointInLeftImage, const Vec2d &pointInRightImage)
Given a point correspondence, computes the distance from the epipolar line in the left image...
void SetSingleCalibrations(const CCalibration &leftCalibration, const CCalibration &rightCalibration, bool bTransformLeftCameraToIdentity=false)
Initializes the stereo camera model, given two instances of CCalibration.
void SetDistortionParameters(float d1_left, float d2_left, float d3_left, float d4_left, float d1_right, float d2_right, float d3_right, float d4_right)
Sets the distortion parameters of the distortion models of both cameras.
const CCameraParameters & GetCameraParameters() const
Gives access to the camera parameters.
CStereoCalibration()
The default constructor.
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 SetExtrinsicParameters(const Mat3d &left_rotation, const Vec3d &left_translation, const Mat3d &right_rotation, const Vec3d &right_translation, bool bTransformLeftCameraToIdentity=false)
Sets the extrinsic parameters of both cameras.
void SubtractVecVec(const Vec3d &vector1, const Vec3d &vector2, Vec3d &result)
void MulMatMat(const Mat3d &matrix1, const Mat3d &matrix2, Mat3d &result)
const CCalibration * GetLeftCalibration() const
Access to the instance of CCalibration for the camera model of the left camera.
void Set(const CStereoCalibration &stereoCalibration)
Initializes the stereo camera model, given an instance of CStereoCalibration.
void CalculateEpipolarLineInLeftImage(const Vec2d &pointInRightImage, Vec3d &l)
Given an image point in the right image, computes the corresponding epipolar line in the left image...
Data structure for the representation of a 3D vector.
void Transpose(const Mat3d &matrix, Mat3d &result)
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.
float CalculateEpipolarLineInRightImageDistance(const Vec2d &pointInLeftImage, const Vec2d &pointInRightImage)
Given a point correspondence, computes the distance from the epipolar line in the right image...
Mat3d rectificationHomographyRight
The homography for the rectification mapping of the right image.
void SubtractFromVec(Vec3d &vec, const Vec3d &vectorToSubtract)
void SetRotation(const Mat3d &R)
Sets the extrinsic parameter R (rotation matrix).
Mat3d m_rotation_inverse
Rotation matrix of the inverted extrinsic transformation.
int height
The height of the images of the stereo camera system in pixels.
Data structure for the representation of a 3x3 matrix.
void SetTranslation(const Vec3d &t)
Sets the extrinsic parameter t (translation vector).
void SetVec(Vec2d &vec, float x, float y)
void GetProjectionMatricesForRectifiedImages(Mat3d &P1Left, Vec3d &p2Left, Mat3d &P1Right, Vec3d &p2Right) const
Sets up the projection matrix P for the rectified images.
Camera model and functions for a stereo camera system.
void GetCalibrationMatrix(Mat3d &K) const
Sets up the calibration matrix K.
void SetVec(Vec3d &vec, float x, float y, float z)
Mat3d rectificationHomographyLeft
The homography for the rectification mapping of the right image.
int width
The width of the images of the stereo camera system in pixels.
Struct containing all parameters of the camera model.