IVT
StereoCalibrationCV.cpp
Go to the documentation of this file.
1 // ****************************************************************************
2 // This file is part of the Integrating Vision Toolkit (IVT).
3 //
4 // The IVT is maintained by the Karlsruhe Institute of Technology (KIT)
5 // (www.kit.edu) in cooperation with the company Keyetech (www.keyetech.de).
6 //
7 // Copyright (C) 2014 Karlsruhe Institute of Technology (KIT).
8 // All rights reserved.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are met:
12 //
13 // 1. Redistributions of source code must retain the above copyright
14 // notice, this list of conditions and the following disclaimer.
15 //
16 // 2. Redistributions in binary form must reproduce the above copyright
17 // notice, this list of conditions and the following disclaimer in the
18 // documentation and/or other materials provided with the distribution.
19 //
20 // 3. Neither the name of the KIT nor the names of its contributors may be
21 // used to endorse or promote products derived from this software
22 // without specific prior written permission.
23 //
24 // THIS SOFTWARE IS PROVIDED BY THE KIT AND CONTRIBUTORS “AS IS” AND ANY
25 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 // DISCLAIMED. IN NO EVENT SHALL THE KIT OR CONTRIBUTORS BE LIABLE FOR ANY
28 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 // ****************************************************************************
35 // ****************************************************************************
36 // Filename: StereoCalibrationCV.cpp
37 // Author: Pedram Azad
38 // Date: 2006
39 // ****************************************************************************
40 
41 
42 // ****************************************************************************
43 // Includes
44 // ****************************************************************************
45 
46 #include <new> // for explicitly using correct new/delete operators on VC DSPs
47 
48 #include "StereoCalibrationCV.h"
49 #include "StereoCalibration.h"
50 #include "Calibration.h"
51 #include "Math/Math3d.h"
52 
53 #include <cvaux.h>
54 #include <stdio.h>
55 
56 
57 
58 static void FillMatrix(float *pTargetMatrix, const Mat3d &sourceMatrix)
59 {
60  pTargetMatrix[0] = sourceMatrix.r1;
61  pTargetMatrix[1] = sourceMatrix.r2;
62  pTargetMatrix[2] = sourceMatrix.r3;
63  pTargetMatrix[3] = sourceMatrix.r4;
64  pTargetMatrix[4] = sourceMatrix.r5;
65  pTargetMatrix[5] = sourceMatrix.r6;
66  pTargetMatrix[6] = sourceMatrix.r7;
67  pTargetMatrix[7] = sourceMatrix.r8;
68  pTargetMatrix[8] = sourceMatrix.r9;
69 }
70 
71 static void FillMatrix(double *pTargetMatrix, const Mat3d &sourceMatrix)
72 {
73  pTargetMatrix[0] = sourceMatrix.r1;
74  pTargetMatrix[1] = sourceMatrix.r2;
75  pTargetMatrix[2] = sourceMatrix.r3;
76  pTargetMatrix[3] = sourceMatrix.r4;
77  pTargetMatrix[4] = sourceMatrix.r5;
78  pTargetMatrix[5] = sourceMatrix.r6;
79  pTargetMatrix[6] = sourceMatrix.r7;
80  pTargetMatrix[7] = sourceMatrix.r8;
81  pTargetMatrix[8] = sourceMatrix.r9;
82 }
83 
84 static void FillVector(float *pTargetVector, const Vec3d &sourceVector)
85 {
86  pTargetVector[0] = (float) sourceVector.x;
87  pTargetVector[1] = (float) sourceVector.y;
88  pTargetVector[2] = (float) sourceVector.z;
89 }
90 
91 static void FillCalibrationMatrix(float *pTargetMatrix, const CCalibration::CCameraParameters &cameraParameters)
92 {
93  pTargetMatrix[0] = (float) cameraParameters.focalLength.x;
94  pTargetMatrix[1] = 0;
95  pTargetMatrix[2] = (float) cameraParameters.principalPoint.x;
96  pTargetMatrix[3] = 0;
97  pTargetMatrix[4] = (float) cameraParameters.focalLength.y;
98  pTargetMatrix[5] = (float) cameraParameters.principalPoint.y;
99  pTargetMatrix[6] = 0;
100  pTargetMatrix[7] = 0;
101  pTargetMatrix[8] = 1;
102 }
103 
104 
106 {
107  CvStereoCamera stereoParams;
108  CvCamera leftCamera, rightCamera;
109 
110  stereoParams.camera[0] = &leftCamera;
111  stereoParams.camera[1] = &rightCamera;
112 
113  stereoParams.camera[0]->imgSize[0] = (float) pStereoCalibration->width;
114  stereoParams.camera[0]->imgSize[1] = (float) pStereoCalibration->height;
115  stereoParams.camera[1]->imgSize[0] = (float) pStereoCalibration->width;
116  stereoParams.camera[1]->imgSize[1] = (float) pStereoCalibration->height;
117 
118  const CCalibration::CCameraParameters &leftCameraParameters = pStereoCalibration->GetLeftCalibration()->GetCameraParameters();
119  const CCalibration::CCameraParameters &rightCameraParameters = pStereoCalibration->GetRightCalibration()->GetCameraParameters();
120 
121  FillMatrix(stereoParams.rotMatrix, pStereoCalibration->GetRightCalibration()->m_rotation_inverse);
122  FillVector(stereoParams.transVector, pStereoCalibration->GetRightCalibration()->m_translation_inverse);
123 
124  int i;
125 
126  leftCamera.imgSize[0] = (float) leftCameraParameters.width;
127  leftCamera.imgSize[1] = (float) leftCameraParameters.height;
128  FillCalibrationMatrix(leftCamera.matrix, leftCameraParameters);
129  for (i = 0; i < 4; i++) leftCamera.distortion[i] = (float) leftCameraParameters.distortion[i];
130  FillMatrix(leftCamera.rotMatr, leftCameraParameters.rotation);
131  FillVector(leftCamera.transVect, leftCameraParameters.translation);
132 
133  rightCamera.imgSize[0] = (float) rightCameraParameters.width;
134  rightCamera.imgSize[1] = (float) rightCameraParameters.height;
135  FillCalibrationMatrix(rightCamera.matrix, rightCameraParameters);
136  for (i = 0; i < 4; i++) rightCamera.distortion[i] = (float) rightCameraParameters.distortion[i];
137  FillMatrix(rightCamera.rotMatr, rightCameraParameters.rotation);
138  FillVector(rightCamera.transVect, rightCameraParameters.translation);
139 
140  icvComputeRestStereoParams(&stereoParams);
141 
142  Math3d::SetMat(pStereoCalibration->rectificationHomographyLeft,
143  float(stereoParams.coeffs[0][0][0]),
144  float(stereoParams.coeffs[0][0][1]),
145  float(stereoParams.coeffs[0][0][2]),
146  float(stereoParams.coeffs[0][1][0]),
147  float(stereoParams.coeffs[0][1][1]),
148  float(stereoParams.coeffs[0][1][2]),
149  float(stereoParams.coeffs[0][2][0]),
150  float(stereoParams.coeffs[0][2][1]),
151  float(stereoParams.coeffs[0][2][2])
152  );
153 
154  Math3d::SetMat(pStereoCalibration->rectificationHomographyRight,
155  float(stereoParams.coeffs[1][0][0]),
156  float(stereoParams.coeffs[1][0][1]),
157  float(stereoParams.coeffs[1][0][2]),
158  float(stereoParams.coeffs[1][1][0]),
159  float(stereoParams.coeffs[1][1][1]),
160  float(stereoParams.coeffs[1][1][2]),
161  float(stereoParams.coeffs[1][2][0]),
162  float(stereoParams.coeffs[1][2][1]),
163  float(stereoParams.coeffs[1][2][2])
164  );
165 }
166 
167 /*void StereoCalibrationCV::CalculateRectificationHomographiesExperimental(CStereoCalibration *pStereoCalibration)
168 {
169  // algorithm by Fusiello et al. from 2000:
170  // "A compact algorithm for rectification of stereo pairs"
171  const CCalibration::CCameraParameters &left = pStereoCalibration->GetLeftCalibration()->GetCameraParameters();
172  const CCalibration::CCameraParameters &right = pStereoCalibration->GetRightCalibration()->GetCameraParameters();
173 
174  const Mat3d &R1 = pStereoCalibration->GetLeftCalibration()->m_rotation_inverse;//left.rotation;
175  const Mat3d &R2 = pStereoCalibration->GetRightCalibration()->m_rotation_inverse;//right.rotation;
176 
177  const Vec3d &t1 = pStereoCalibration->GetLeftCalibration()->m_translation_inverse;//left.translation;
178  const Vec3d &t2 = pStereoCalibration->GetRightCalibration()->m_translation_inverse;//right.translation;
179 
180  const Mat3d A1 = { -left.focalLength.x, 0, left.principalPoint.x, 0, -left.focalLength.y, left.principalPoint.y, 0, 0, 1 };
181  const Mat3d A2 = { -right.focalLength.x, 0, right.principalPoint.x, 0, -right.focalLength.y, right.principalPoint.y, 0, 0, 1 };
182 
183  printf("\nA1 = %f %f %f\n%f %f %f\n%f %f %f\n\n", A1.r1, A1.r2, A1.r3, A1.r4, A1.r5, A1.r6, A1.r7, A1.r8, A1.r9);
184  printf("R1 = %f %f %f\n%f %f %f\n%f %f %f\n\n", R1.r1, R1.r2, R1.r3, R1.r4, R1.r5, R1.r6, R1.r7, R1.r8, R1.r9);
185  printf("t1 = %f %f %f\n\n\n", t1.x, t1.y, t1.z);
186 
187  printf("A2 = %f %f %f\n%f %f %f\n%f %f %f\n\n", A2.r1, A2.r2, A2.r3, A2.r4, A2.r5, A2.r6, A2.r7, A2.r8, A2.r9);
188  printf("R2 = %f %f %f\n%f %f %f\n%f %f %f\n\n", R2.r1, R2.r2, R2.r3, R2.r4, R2.r5, R2.r6, R2.r7, R2.r8, R2.r9);
189  printf("t2 = %f %f %f\n\n\n", t2.x, t2.y, t2.z);
190 
191  const Vec3d &c1 = pStereoCalibration->GetLeftCalibration()->m_translation_inverse;
192  const Vec3d &c2 = pStereoCalibration->GetRightCalibration()->m_translation_inverse;
193 
194  Vec3d v1, v2, v3, k = { R1.r7, R1.r8, R1.r9 };
195 
196  Math3d::SubtractVecVec(c1, c2, v1);
197  Math3d::CrossProduct(k, v1, v2);
198  Math3d::CrossProduct(v1, v2, v3);
199 
200  Math3d::NormalizeVec(v1);
201  Math3d::NormalizeVec(v2);
202  Math3d::NormalizeVec(v3);
203 
204  Mat3d R = { v1.x, v1.y, v1.z, v2.x, v2.y, v2.z, v3.x, v3.y, v3.z };
205  Mat3d A =
206  {
207  0.5f * (left.focalLength.x + right.focalLength.x), 0, 0.5f * (left.principalPoint.x + right.principalPoint.x),
208  0, 0.5f * (left.focalLength.y + right.focalLength.y), 0.5f * (left.principalPoint.y + right.principalPoint.y),
209  0, 0, 1
210  };
211 
212  Mat3d T1, T2, M;
213 
214  Math3d::MulMatMat(A, R, T1);
215  Math3d::MulMatMat(A1, R1, M);
216  Math3d::Invert(M, M);
217  Math3d::MulMatMat(T1, M, T1);
218 
219  Math3d::MulMatMat(A, R, T2);
220  Math3d::MulMatMat(A2, R2, M);
221  Math3d::Invert(M, M);
222  Math3d::MulMatMat(T2, M, T2);
223 
224  Math3d::SetMat(pStereoCalibration->rectificationHomographyLeft, T1);
225  Math3d::SetMat(pStereoCalibration->rectificationHomographyRight, T2);
226 }*/
const CCalibration * GetRightCalibration() const
Access to the instance of CCalibration for the camera model of the right camera.
float y
Definition: Math3d.h:75
void CalculateRectificationHomographies(CStereoCalibration *pStereoCalibration)
float r4
Definition: Math3d.h:95
float r3
Definition: Math3d.h:95
Vec3d m_translation_inverse
Translation vector of the inverted extrinsic transformation.
Definition: Calibration.h:454
float x
Definition: Math2d.h:84
float r1
Definition: Math3d.h:95
float r7
Definition: Math3d.h:95
float x
Definition: Math3d.h:75
float r2
Definition: Math3d.h:95
static void FillVector(float *pTargetVector, const Vec3d &sourceVector)
static void FillCalibrationMatrix(float *pTargetMatrix, const CCalibration::CCameraParameters &cameraParameters)
const CCameraParameters & GetCameraParameters() const
Gives access to the camera parameters.
Definition: Calibration.h:268
float r8
Definition: Math3d.h:95
float y
Definition: Math2d.h:84
const CCalibration * GetLeftCalibration() const
Access to the instance of CCalibration for the camera model of the left camera.
Data structure for the representation of a 3D vector.
Definition: Math3d.h:73
void SetMat(Mat3d &matrix, float r1, float r2, float r3, float r4, float r5, float r6, float r7, float r8, float r9)
Definition: Math3d.cpp:257
float z
Definition: Math3d.h:75
Mat3d rectificationHomographyRight
The homography for the rectification mapping of the right image.
float r6
Definition: Math3d.h:95
static void FillMatrix(float *pTargetMatrix, const Mat3d &sourceMatrix)
Mat3d m_rotation_inverse
Rotation matrix of the inverted extrinsic transformation.
Definition: Calibration.h:443
int height
The height of the images of the stereo camera system in pixels.
Data structure for the representation of a 3x3 matrix.
Definition: Math3d.h:93
float r9
Definition: Math3d.h:95
Camera model and functions for a stereo camera system.
float r5
Definition: Math3d.h:95
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.
Definition: Calibration.h:133