StereoCalibrationCV.cpp
Go to the documentation of this file.
00001 // ****************************************************************************
00002 // This file is part of the Integrating Vision Toolkit (IVT).
00003 //
00004 // The IVT is maintained by the Karlsruhe Institute of Technology (KIT)
00005 // (www.kit.edu) in cooperation with the company Keyetech (www.keyetech.de).
00006 //
00007 // Copyright (C) 2014 Karlsruhe Institute of Technology (KIT).
00008 // All rights reserved.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are met:
00012 //
00013 // 1. Redistributions of source code must retain the above copyright
00014 //    notice, this list of conditions and the following disclaimer.
00015 //
00016 // 2. Redistributions in binary form must reproduce the above copyright
00017 //    notice, this list of conditions and the following disclaimer in the
00018 //    documentation and/or other materials provided with the distribution.
00019 //
00020 // 3. Neither the name of the KIT nor the names of its contributors may be
00021 //    used to endorse or promote products derived from this software
00022 //    without specific prior written permission.
00023 //
00024 // THIS SOFTWARE IS PROVIDED BY THE KIT AND CONTRIBUTORS “AS IS” AND ANY
00025 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00026 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027 // DISCLAIMED. IN NO EVENT SHALL THE KIT OR CONTRIBUTORS BE LIABLE FOR ANY
00028 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00029 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00031 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00032 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00033 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034 // ****************************************************************************
00035 
00036 
00037 // ****************************************************************************
00038 // Includes
00039 // ****************************************************************************
00040 
00041 #include <new> // for explicitly using correct new/delete operators on VC DSPs
00042 
00043 #include "StereoCalibrationCV.h"
00044 #include "StereoCalibration.h"
00045 #include "Calibration.h"
00046 #include "Math/Math3d.h"
00047 
00048 #include <stdio.h>
00049 
00050 
00051 
00052 static void FillMatrix(float *pTargetMatrix, const Mat3d &sourceMatrix)
00053 {
00054         pTargetMatrix[0] = sourceMatrix.r1;
00055         pTargetMatrix[1] = sourceMatrix.r2;
00056         pTargetMatrix[2] = sourceMatrix.r3;
00057         pTargetMatrix[3] = sourceMatrix.r4;
00058         pTargetMatrix[4] = sourceMatrix.r5;
00059         pTargetMatrix[5] = sourceMatrix.r6;
00060         pTargetMatrix[6] = sourceMatrix.r7;
00061         pTargetMatrix[7] = sourceMatrix.r8;
00062         pTargetMatrix[8] = sourceMatrix.r9;
00063 }
00064 
00065 static void FillMatrix(double *pTargetMatrix, const Mat3d &sourceMatrix)
00066 {
00067         pTargetMatrix[0] = sourceMatrix.r1;
00068         pTargetMatrix[1] = sourceMatrix.r2;
00069         pTargetMatrix[2] = sourceMatrix.r3;
00070         pTargetMatrix[3] = sourceMatrix.r4;
00071         pTargetMatrix[4] = sourceMatrix.r5;
00072         pTargetMatrix[5] = sourceMatrix.r6;
00073         pTargetMatrix[6] = sourceMatrix.r7;
00074         pTargetMatrix[7] = sourceMatrix.r8;
00075         pTargetMatrix[8] = sourceMatrix.r9;
00076 }
00077 
00078 static void FillVector(float *pTargetVector, const Vec3d &sourceVector)
00079 {
00080         pTargetVector[0] = (float) sourceVector.x;
00081         pTargetVector[1] = (float) sourceVector.y;
00082         pTargetVector[2] = (float) sourceVector.z;
00083 }
00084 
00085 static void FillCalibrationMatrix(float *pTargetMatrix, const CCalibration::CCameraParameters &cameraParameters)
00086 {
00087         pTargetMatrix[0] = (float) cameraParameters.focalLength.x;
00088         pTargetMatrix[1] = 0;
00089         pTargetMatrix[2] = (float) cameraParameters.principalPoint.x;
00090         pTargetMatrix[3] = 0;
00091         pTargetMatrix[4] = (float) cameraParameters.focalLength.y;
00092         pTargetMatrix[5] = (float) cameraParameters.principalPoint.y;
00093         pTargetMatrix[6] = 0;
00094         pTargetMatrix[7] = 0;
00095         pTargetMatrix[8] = 1;
00096 }
00097 
00098 
00099 void CalculateRectificationHomographies(CStereoCalibration *pStereoCalibration)
00100 {
00101         CvStereoCamera stereoParams;
00102         CvCamera leftCamera, rightCamera;
00103         
00104         stereoParams.camera[0] = &leftCamera;
00105         stereoParams.camera[1] = &rightCamera;
00106         
00107         stereoParams.camera[0]->imgSize[0] = (float) pStereoCalibration->width;
00108     stereoParams.camera[0]->imgSize[1] = (float) pStereoCalibration->height;
00109     stereoParams.camera[1]->imgSize[0] = (float) pStereoCalibration->width;
00110     stereoParams.camera[1]->imgSize[1] = (float) pStereoCalibration->height;
00111         
00112         const CCalibration::CCameraParameters &leftCameraParameters = pStereoCalibration->GetLeftCalibration()->GetCameraParameters();
00113         const CCalibration::CCameraParameters &rightCameraParameters = pStereoCalibration->GetRightCalibration()->GetCameraParameters();
00114         
00115         FillMatrix(stereoParams.rotMatrix, pStereoCalibration->GetRightCalibration()->m_rotation_inverse);
00116         FillVector(stereoParams.transVector, pStereoCalibration->GetRightCalibration()->m_translation_inverse);
00117         
00118         int i;
00119         
00120         leftCamera.imgSize[0] = (float) leftCameraParameters.width;
00121         leftCamera.imgSize[1] = (float) leftCameraParameters.height;
00122         FillCalibrationMatrix(leftCamera.matrix, leftCameraParameters);
00123         for (i = 0; i < 4; i++) leftCamera.distortion[i] = (float) leftCameraParameters.distortion[i];
00124         FillMatrix(leftCamera.rotMatr, leftCameraParameters.rotation);
00125         FillVector(leftCamera.transVect, leftCameraParameters.translation);
00126         
00127         rightCamera.imgSize[0] = (float) rightCameraParameters.width;
00128         rightCamera.imgSize[1] = (float) rightCameraParameters.height;
00129         FillCalibrationMatrix(rightCamera.matrix, rightCameraParameters);
00130         for (i = 0; i < 4; i++) rightCamera.distortion[i] = (float) rightCameraParameters.distortion[i];
00131         FillMatrix(rightCamera.rotMatr, rightCameraParameters.rotation);
00132         FillVector(rightCamera.transVect, rightCameraParameters.translation);
00133         
00134         icvComputeRestStereoParams(&stereoParams);
00135         
00136         Math3d::SetMat(pStereoCalibration->rectificationHomographyLeft,
00137                 float(stereoParams.coeffs[0][0][0]),
00138                 float(stereoParams.coeffs[0][0][1]),
00139                 float(stereoParams.coeffs[0][0][2]),
00140                 float(stereoParams.coeffs[0][1][0]),
00141                 float(stereoParams.coeffs[0][1][1]),
00142                 float(stereoParams.coeffs[0][1][2]),
00143                 float(stereoParams.coeffs[0][2][0]),
00144                 float(stereoParams.coeffs[0][2][1]),
00145                 float(stereoParams.coeffs[0][2][2])
00146         );
00147         
00148         Math3d::SetMat(pStereoCalibration->rectificationHomographyRight,
00149                 float(stereoParams.coeffs[1][0][0]),
00150                 float(stereoParams.coeffs[1][0][1]),
00151                 float(stereoParams.coeffs[1][0][2]),
00152                 float(stereoParams.coeffs[1][1][0]),
00153                 float(stereoParams.coeffs[1][1][1]),
00154                 float(stereoParams.coeffs[1][1][2]),
00155                 float(stereoParams.coeffs[1][2][0]),
00156                 float(stereoParams.coeffs[1][2][1]),
00157                 float(stereoParams.coeffs[1][2][2])
00158         );
00159 }
00160 
00161 /*void StereoCalibrationCV::CalculateRectificationHomographiesExperimental(CStereoCalibration *pStereoCalibration)
00162 {
00163         // algorithm by Fusiello et al. from 2000:
00164         // "A compact algorithm for rectification of stereo pairs"
00165         const CCalibration::CCameraParameters &left = pStereoCalibration->GetLeftCalibration()->GetCameraParameters();
00166         const CCalibration::CCameraParameters &right = pStereoCalibration->GetRightCalibration()->GetCameraParameters();
00167         
00168         const Mat3d &R1 = pStereoCalibration->GetLeftCalibration()->m_rotation_inverse;//left.rotation;
00169         const Mat3d &R2 = pStereoCalibration->GetRightCalibration()->m_rotation_inverse;//right.rotation;
00170         
00171         const Vec3d &t1 = pStereoCalibration->GetLeftCalibration()->m_translation_inverse;//left.translation;
00172         const Vec3d &t2 = pStereoCalibration->GetRightCalibration()->m_translation_inverse;//right.translation;
00173         
00174         const Mat3d A1 = { -left.focalLength.x, 0, left.principalPoint.x, 0, -left.focalLength.y, left.principalPoint.y, 0, 0, 1 };
00175         const Mat3d A2 = { -right.focalLength.x, 0, right.principalPoint.x, 0, -right.focalLength.y, right.principalPoint.y, 0, 0, 1 };
00176         
00177         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);
00178         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);
00179         printf("t1 = %f %f %f\n\n\n", t1.x, t1.y, t1.z);
00180         
00181         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);
00182         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);
00183         printf("t2 = %f %f %f\n\n\n", t2.x, t2.y, t2.z);
00184 
00185         const Vec3d &c1 = pStereoCalibration->GetLeftCalibration()->m_translation_inverse;
00186         const Vec3d &c2 = pStereoCalibration->GetRightCalibration()->m_translation_inverse;
00187         
00188         Vec3d v1, v2, v3, k = { R1.r7, R1.r8, R1.r9 };
00189         
00190         Math3d::SubtractVecVec(c1, c2, v1);
00191         Math3d::CrossProduct(k, v1, v2);
00192         Math3d::CrossProduct(v1, v2, v3);
00193         
00194         Math3d::NormalizeVec(v1);
00195         Math3d::NormalizeVec(v2);
00196         Math3d::NormalizeVec(v3);
00197         
00198         Mat3d R = { v1.x, v1.y, v1.z, v2.x, v2.y, v2.z, v3.x, v3.y, v3.z };
00199         Mat3d A =
00200         {
00201                 0.5f * (left.focalLength.x + right.focalLength.x), 0, 0.5f * (left.principalPoint.x + right.principalPoint.x),
00202                 0, 0.5f * (left.focalLength.y + right.focalLength.y), 0.5f * (left.principalPoint.y + right.principalPoint.y),
00203                 0, 0, 1
00204         };
00205         
00206         Mat3d T1, T2, M;
00207         
00208         Math3d::MulMatMat(A, R, T1);
00209         Math3d::MulMatMat(A1, R1, M);
00210         Math3d::Invert(M, M);
00211         Math3d::MulMatMat(T1, M, T1);
00212         
00213         Math3d::MulMatMat(A, R, T2);
00214         Math3d::MulMatMat(A2, R2, M);
00215         Math3d::Invert(M, M);
00216         Math3d::MulMatMat(T2, M, T2);
00217         
00218         Math3d::SetMat(pStereoCalibration->rectificationHomographyLeft, T1);
00219         Math3d::SetMat(pStereoCalibration->rectificationHomographyRight, T2);
00220 }*/
00221 


asr_ivt
Author(s): Allgeyer Tobias, Hutmacher Robin, Kleinert Daniel, Meißner Pascal, Scholz Jonas, Stöckle Patrick
autogenerated on Thu Jun 6 2019 21:46:58