StereoCalibration.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 // Filename:  StereoCalibration.cpp
00037 // Author:    Pedram Azad
00038 // Date:      2004
00039 // ****************************************************************************
00040 
00041 
00042 // ****************************************************************************
00043 // Includes
00044 // ****************************************************************************
00045 
00046 #include <new> // for explicitly using correct new/delete operators on VC DSPs
00047 
00048 #include "StereoCalibration.h"
00049 
00050 #include "Math/Math2d.h"
00051 #include "Math/Math3d.h"
00052 #include "Image/ByteImage.h"
00053 #include "Image/ImageProcessor.h"
00054 #include "Calibration/Calibration.h"
00055 #include "Helpers/helpers.h"
00056 
00057 #include <math.h>
00058 #include <stdio.h>
00059 
00060 
00061 
00062 // ****************************************************************************
00063 // Constructor / Destructor
00064 // ****************************************************************************
00065 
00066 CStereoCalibration::CStereoCalibration()
00067 {
00068         m_pLeftCalibration = new CCalibration();
00069         m_pRightCalibration = new CCalibration();
00070 
00071         width = m_pLeftCalibration->GetCameraParameters().width;
00072         height = m_pLeftCalibration->GetCameraParameters().height;
00073         
00074         Vec3d t = { -100, 0, 0 };
00075         SetExtrinsicParameters(Math3d::unit_mat, Math3d::zero_vec, Math3d::unit_mat, t, false);
00076 }
00077 
00078 CStereoCalibration::~CStereoCalibration()
00079 {
00080         delete m_pLeftCalibration;
00081         delete m_pRightCalibration;
00082 }
00083 
00084 CStereoCalibration::CStereoCalibration(const CStereoCalibration &stereoCalibration)
00085 {
00086         m_pLeftCalibration = new CCalibration();
00087         m_pRightCalibration = new CCalibration();
00088         
00089         m_pLeftCalibration->Set(*stereoCalibration.GetLeftCalibration());
00090         m_pRightCalibration->Set(*stereoCalibration.GetRightCalibration());
00091         
00092         width = stereoCalibration.width;
00093         height = stereoCalibration.height;
00094         
00095         Math3d::SetMat(rectificationHomographyLeft, stereoCalibration.rectificationHomographyLeft);
00096         Math3d::SetMat(rectificationHomographyRight, stereoCalibration.rectificationHomographyRight);
00097         Math3d::SetMat(F, stereoCalibration.F);
00098         Math3d::SetMat(FT, stereoCalibration.FT);
00099 }
00100 
00101 
00102 // ****************************************************************************
00103 // Methods
00104 // ****************************************************************************
00105 
00106 void CStereoCalibration::Set(const CStereoCalibration &stereoCalibration)
00107 {
00108         width = stereoCalibration.width;
00109         height = stereoCalibration.height;
00110 
00111         Math3d::SetMat(F, stereoCalibration.F);
00112         Math3d::SetMat(FT, stereoCalibration.FT);
00113 
00114         Math3d::SetMat(rectificationHomographyLeft, stereoCalibration.rectificationHomographyLeft);
00115         Math3d::SetMat(rectificationHomographyRight, stereoCalibration.rectificationHomographyRight);
00116 
00117         m_pLeftCalibration->Set(*stereoCalibration.GetLeftCalibration());
00118         m_pRightCalibration->Set(*stereoCalibration.GetRightCalibration());
00119 }
00120 
00121 void CStereoCalibration::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)
00122 {
00123         m_pLeftCalibration->SetDistortion(d1_left, d2_left, d3_left, d4_left);
00124         m_pRightCalibration->SetDistortion(d1_right, d2_right, d3_right, d4_right);
00125 }
00126 
00127 void CStereoCalibration::SetSingleCalibrations(const CCalibration &leftCalibration, const CCalibration &rightCalibration, bool bTransformLeftCameraToIdentity)
00128 {
00129         m_pLeftCalibration->Set(leftCalibration);
00130         m_pRightCalibration->Set(rightCalibration);
00131 
00132         width = leftCalibration.GetCameraParameters().width;
00133         height = leftCalibration.GetCameraParameters().height;
00134         
00135         if (bTransformLeftCameraToIdentity)
00136         {
00137                 TransformLeftCameraToIdentity();
00138                 CalculateFundamentalMatrix();
00139         }
00140         else
00141         {
00142                 Mat3d left_rotation, right_rotation;
00143                 Vec3d left_translation, right_translation;
00144                 
00145                 // store original transformations
00146                 Math3d::SetMat(left_rotation, leftCalibration.GetCameraParameters().rotation);
00147                 Math3d::SetMat(right_rotation, rightCalibration.GetCameraParameters().rotation);
00148                 
00149                 Math3d::SetVec(left_translation, leftCalibration.GetCameraParameters().translation);
00150                 Math3d::SetVec(right_translation, rightCalibration.GetCameraParameters().translation);
00151                 
00152                 // calculate epipolar geometry
00153                 TransformLeftCameraToIdentity();
00154                 CalculateFundamentalMatrix();
00155         
00156                 // reset transformations
00157                 m_pLeftCalibration->SetRotation(left_rotation);
00158                 m_pLeftCalibration->SetTranslation(left_translation);
00159         
00160                 m_pRightCalibration->SetRotation(right_rotation);
00161                 m_pRightCalibration->SetTranslation(right_translation);
00162         }
00163 }
00164 
00165 void CStereoCalibration::SetExtrinsicParameters(const Mat3d& left_rotation, const Vec3d& left_translation, const Mat3d& right_rotation, const Vec3d& right_translation, bool bTransformToIdentity)
00166 {
00167         // set rotation of left and right calibration
00168         m_pLeftCalibration->SetRotation(left_rotation);
00169         m_pLeftCalibration->SetTranslation(left_translation);
00170         
00171         m_pRightCalibration->SetRotation(right_rotation);
00172         m_pRightCalibration->SetTranslation(right_translation);
00173         
00174         // transform to identy for fundemantal matrix calculation
00175         TransformLeftCameraToIdentity();
00176         
00177         // update fundametal matrix
00178         CalculateFundamentalMatrix();
00179         
00180         if(!bTransformToIdentity)
00181         {
00182                 // reset transformation of left and right calibration
00183                 m_pLeftCalibration->SetRotation(left_rotation);
00184                 m_pLeftCalibration->SetTranslation(left_translation);
00185                 
00186                 m_pRightCalibration->SetRotation(right_rotation);
00187                 m_pRightCalibration->SetTranslation(right_translation); 
00188         }
00189 }
00190 
00191 
00192 void CStereoCalibration::TransformLeftCameraToIdentity()
00193 {
00194         Mat3d tempMat;
00195         Vec3d tempVec;
00196         
00197         const CCalibration::CCameraParameters &cameraParametersLeft = m_pLeftCalibration->GetCameraParameters();
00198         const CCalibration::CCameraParameters &cameraParametersRight = m_pRightCalibration->GetCameraParameters();
00199         
00200         Math3d::MulMatMat(cameraParametersRight.rotation, m_pLeftCalibration->m_rotation_inverse, tempMat);
00201         Math3d::MulMatVec(tempMat, cameraParametersLeft.translation, tempVec);
00202         Math3d::SubtractVecVec(cameraParametersRight.translation, tempVec, tempVec);
00203                 
00204         m_pLeftCalibration->SetRotation(Math3d::unit_mat);
00205         m_pLeftCalibration->SetTranslation(Math3d::zero_vec);
00206         
00207         m_pRightCalibration->SetRotation(tempMat);
00208         m_pRightCalibration->SetTranslation(tempVec);
00209 }
00210 
00211 void CStereoCalibration::CalculateFundamentalMatrix()
00212 {
00213         const CCalibration::CCameraParameters &left = m_pLeftCalibration->GetCameraParameters();
00214         const CCalibration::CCameraParameters &right = m_pRightCalibration->GetCameraParameters();
00215         const Mat3d &R = right.rotation;
00216         const Vec3d &t = right.translation;
00217 
00218         Mat3d E = { 0, -t.z, t.y, t.z, 0, -t.x, -t.y, t.x, 0 };
00219         Math3d::MulMatMat(E, R, E);
00220         
00221         Mat3d K1 = { left.focalLength.x, 0, left.principalPoint.x, 0, left.focalLength.y, left.principalPoint.y, 0, 0, 1 };
00222         Math3d::Invert(K1, K1);
00223         
00224         Mat3d K2 = { right.focalLength.x, 0, right.principalPoint.x, 0, right.focalLength.y, right.principalPoint.y, 0, 0, 1 };
00225         Math3d::Invert(K2, K2);
00226         Math3d::Transpose(K2, K2);
00227         
00228         Math3d::MulMatMat(K2, E, F);
00229         Math3d::MulMatMat(F, K1, F);
00230         
00231         Math3d::Transpose(F, FT);
00232 }
00233 
00234 
00235 void CStereoCalibration::Calculate3DPoint(const Vec2d &cameraPointLeft, const Vec2d &cameraPointRight, Vec3d &worldPoint, bool bInputImagesAreRectified, bool bUseDistortionParameters, PointPair3d *pConnectionLine)
00236 {
00237         Vec2d pointLeft, pointRight;
00238         
00239         if (bInputImagesAreRectified)
00240         {
00241                 Math2d::ApplyHomography(rectificationHomographyLeft, cameraPointLeft, pointLeft);
00242                 Math2d::ApplyHomography(rectificationHomographyRight, cameraPointRight, pointRight);
00243         }
00244         else
00245         {
00246                 Math2d::SetVec(pointLeft, cameraPointLeft);
00247                 Math2d::SetVec(pointRight, cameraPointRight);
00248         }
00249         
00250         // calculate world coordinates of some reference point on the view line (not origin of the camera coordinate system)
00251         Vec3d u, v, a, b;
00252         m_pLeftCalibration->ImageToWorldCoordinates(pointLeft, u, 1, bUseDistortionParameters);
00253         m_pRightCalibration->ImageToWorldCoordinates(pointRight, v, 1, bUseDistortionParameters);
00254         
00255         // get "starting vector" of straight line
00256         Math3d::SetVec(a, m_pLeftCalibration->m_translation_inverse);
00257         Math3d::SetVec(b, m_pRightCalibration->m_translation_inverse);
00258         
00259         // calculate direction vector of straight line
00260         Math3d::SubtractFromVec(u, a);
00261         Math3d::SubtractFromVec(v, b);
00262         
00263         // left straight line:  x = r * u + a
00264         // right straight line: x = s * v + b
00265         // A x = b
00266         float A00 = -u.x; float A01 = v.x;
00267         float A10 = -u.y; float A11 = v.y;
00268         float A20 = -u.z; float A21 = v.z;
00269         float b0 = a.x - b.x;
00270         float b1 = a.y - b.y;
00271         float b2 = a.z - b.z;
00272         
00273         float ATA00 = A00 * A00 + A10 * A10 + A20 * A20;
00274         float ATA10 = A01 * A00 + A11 * A10 + A21 * A20;
00275         float ATA11 = A01 * A01 + A11 * A11 + A21 * A21;
00276         float ATb0 = A00 * b0 + A10 * b1 + A20 * b2;
00277         float ATb1 = A01 * b0 + A11 * b1 + A21 * b2;
00278         
00279         float L00 = sqrtf(ATA00);
00280         float L10 = ATA10 / L00;
00281         float L11 = sqrtf(ATA11 - L10 * L10);
00282         
00283         float yy0 = ATb0 / L00;
00284         float yy1 = (ATb1 - L10 * yy0) / L11;
00285         
00286         float s = yy1 / L11;
00287         float r = (yy0 - L10 * s) / L00;
00288         
00289         // p = (r * u + a + s * v + b) / 2
00290         Math3d::SetVec(worldPoint,
00291                 (r * u.x + a.x + s * v.x + b.x) * 0.5f,
00292                 (r * u.y + a.y + s * v.y + b.y) * 0.5f,
00293                 (r * u.z + a.z + s * v.z + b.z) * 0.5f
00294         );
00295         
00296         if (pConnectionLine)
00297         {
00298                 Math3d::SetVec(pConnectionLine->p1, r * u.x + a.x, r * u.y + a.y, r * u.z + a.z);
00299                 Math3d::SetVec(pConnectionLine->p2, s * v.x + b.x, s * v.y + b.y, s * v.z + b.z);
00300         }
00301 }
00302 
00303 
00304 void CStereoCalibration::CalculateEpipolarLineInLeftImage(const Vec2d &pointInRightImage, Vec3d &l)
00305 {
00306         Vec3d x = { pointInRightImage.x, pointInRightImage.y, 1 };
00307         Math3d::MulMatVec(FT, x, l);
00308 }
00309 
00310 void CStereoCalibration::CalculateEpipolarLineInLeftImage(const Vec2d &pointInRightImage, float &m, float &c)
00311 {
00312         Vec3d l;
00313         CalculateEpipolarLineInLeftImage(pointInRightImage, l);
00314         
00315         m = -l.x / l.y;
00316         c = -l.z / l.y;
00317 }
00318 
00319 void CStereoCalibration::CalculateEpipolarLineInLeftImage(const Vec2d &pointInRightImage, PointPair2d &epipolarLine)
00320 {
00321         Vec3d l;
00322         CalculateEpipolarLineInLeftImage(pointInRightImage, l);
00323         
00324         const float v1 = l.y != 0.0f ? -l.z / l.y : -1.0f;
00325         const float v2 = l.y != 0.0f ? (-l.z - l.x * float(width - 1)) / l.y : -1.0f;
00326         const float u1 = l.x != 0.0f ? -l.z / l.x : -1.0f;
00327         const float u2 = l.x != 0.0f ? (-l.z - l.y * float(height - 1)) / l.x : -1.0f;
00328         
00329         if (v1 >= 0.0f && v1 <= float(height - 1))
00330                 Math2d::SetVec(epipolarLine.p1, 0.0f, v1);
00331         else if (v2 >= 0.0f && v2 <= float(height - 1))
00332                 Math2d::SetVec(epipolarLine.p1, float(width - 1), v2);
00333         else if (u1 >= 0.0f && u1 <= float(width - 1))
00334                 Math2d::SetVec(epipolarLine.p1, u1, 0.0f);
00335         else if (u2 >= 0.0f && u2 <= float(width - 1))
00336                 Math2d::SetVec(epipolarLine.p1, u2, float(height - 1));
00337         
00338         if (v1 >= 0.0f && v1 <= float(height - 1) && (epipolarLine.p1.x != 0.0f || epipolarLine.p1.y != v1))
00339                 Math2d::SetVec(epipolarLine.p2, 0.0f, v1);
00340         else if (v2 >= 0.0f && v2 <= float(height - 1) && (epipolarLine.p1.x != float(width - 1) || epipolarLine.p1.y != v2))
00341                 Math2d::SetVec(epipolarLine.p2, float(width - 1), v2);
00342         else if (u1 >= 0.0f && u1 <= float(width - 1) && (epipolarLine.p1.x != u1 || epipolarLine.p1.y != 0.0f))
00343                 Math2d::SetVec(epipolarLine.p2, u1, 0.0f);
00344         else if (u2 >= 0.0f && u2 <= float(width - 1) && (epipolarLine.p1.x != u2 || epipolarLine.p1.y != float(height - 1)))
00345                 Math2d::SetVec(epipolarLine.p2, u2, float(height - 1));
00346 }
00347 
00348 
00349 void CStereoCalibration::CalculateEpipolarLineInRightImage(const Vec2d &pointInLeftImage, Vec3d &l)
00350 {
00351         Vec3d x = { pointInLeftImage.x, pointInLeftImage.y, 1 };
00352         Math3d::MulMatVec(F, x, l);
00353 }
00354 
00355 void CStereoCalibration::CalculateEpipolarLineInRightImage(const Vec2d &pointInLeftImage, float &m, float &c)
00356 {
00357         Vec3d l;
00358         CalculateEpipolarLineInRightImage(pointInLeftImage, l);
00359         
00360         m = -l.x / l.y;
00361         c = -l.z / l.y;
00362 }
00363 
00364 void CStereoCalibration::CalculateEpipolarLineInRightImage(const Vec2d &pointInLeftImage, PointPair2d &epipolarLine)
00365 {
00366         Vec3d l;
00367         CalculateEpipolarLineInRightImage(pointInLeftImage, l);
00368         
00369         const float v1 = l.y != 0.0f ? -l.z / l.y : -1.0f;
00370         const float v2 = l.y != 0.0f ? (-l.z - l.x * float(width - 1)) / l.y : -1.0f;
00371         const float u1 = l.x != 0.0f ? -l.z / l.x : -1.0f;
00372         const float u2 = l.x != 0.0f ? (-l.z - l.y * float(height - 1)) / l.x : -1.0f;
00373         
00374         if (v1 >= 0.0f && v1 <= float(height - 1))
00375                 Math2d::SetVec(epipolarLine.p1, 0.0f, v1);
00376         else if (v2 >= 0.0f && v2 <= float(height - 1))
00377                 Math2d::SetVec(epipolarLine.p1, float(width - 1), v2);
00378         else if (u1 >= 0.0f && u1 <= float(width - 1))
00379                 Math2d::SetVec(epipolarLine.p1, u1, 0.0f);
00380         else if (u2 >= 0.0f && u2 <= float(width - 1))
00381                 Math2d::SetVec(epipolarLine.p1, u2, float(height - 1));
00382         
00383         if (v1 >= 0.0f && v1 <= float(height - 1) && (epipolarLine.p1.x != 0.0f || epipolarLine.p1.y != v1))
00384                 Math2d::SetVec(epipolarLine.p2, 0.0f, v1);
00385         else if (v2 >= 0.0f && v2 <= float(height - 1) && (epipolarLine.p1.x != float(width - 1) || epipolarLine.p1.y != v2))
00386                 Math2d::SetVec(epipolarLine.p2, float(width - 1), v2);
00387         else if (u1 >= 0.0f && u1 <= float(width - 1) && (epipolarLine.p1.x != u1 || epipolarLine.p1.y != 0.0f))
00388                 Math2d::SetVec(epipolarLine.p2, u1, 0.0f);
00389         else if (u2 >= 0.0f && u2 <= float(width - 1) && (epipolarLine.p1.x != u2 || epipolarLine.p1.y != float(height - 1)))
00390                 Math2d::SetVec(epipolarLine.p2, u2, float(height - 1));
00391 }
00392 
00393 
00394 float CStereoCalibration::CalculateEpipolarLineInLeftImageDistance(const Vec2d &pointInLeftImage, const Vec2d &pointInRightImage)
00395 {
00396         Vec3d l;
00397         CalculateEpipolarLineInLeftImage(pointInRightImage, l);
00398         
00399         const float length = sqrtf(l.x * l.x + l.y * l.y);
00400         const float l1 = l.x / length;
00401         const float l2 = l.y / length;
00402         const float l3 = l.z / length;
00403         
00404         return l1 * pointInLeftImage.x + l2 * pointInLeftImage.y + l3;
00405 }
00406 
00407 float CStereoCalibration::CalculateEpipolarLineInRightImageDistance(const Vec2d &pointInLeftImage, const Vec2d &pointInRightImage)
00408 {
00409         Vec3d l;
00410         CalculateEpipolarLineInRightImage(pointInLeftImage, l);
00411         
00412         const float length = sqrtf(l.x * l.x + l.y * l.y);
00413         const float l1 = l.x / length;
00414         const float l2 = l.y / length;
00415         const float l3 = l.z / length;
00416         
00417         return l1 * pointInRightImage.x + l2 * pointInRightImage.y + l3;
00418 }
00419 
00420 
00421 bool CStereoCalibration::LoadCameraParameters(const char *pCameraParameterFileName, bool bTransformLeftCameraToIdentity)
00422 {
00423         if (!m_pLeftCalibration->LoadCameraParameters(pCameraParameterFileName, 0))
00424                 return false;
00425         
00426         if (!m_pRightCalibration->LoadCameraParameters(pCameraParameterFileName, 1))
00427                 return false;
00428         
00429         if (bTransformLeftCameraToIdentity)
00430         {
00431                 TransformLeftCameraToIdentity();
00432                 CalculateFundamentalMatrix();
00433         }
00434         else
00435         {
00436                 Mat3d left_rotation, right_rotation;
00437                 Vec3d left_translation, right_translation;
00438                 
00439                 // store original transformations
00440                 Math3d::SetMat(left_rotation, m_pLeftCalibration->GetCameraParameters().rotation);
00441                 Math3d::SetMat(right_rotation, m_pRightCalibration->GetCameraParameters().rotation);
00442                 
00443                 Math3d::SetVec(left_translation, m_pLeftCalibration->GetCameraParameters().translation);
00444                 Math3d::SetVec(right_translation, m_pRightCalibration->GetCameraParameters().translation);
00445                 
00446                 // calculate epipolar geometry
00447                 TransformLeftCameraToIdentity();
00448                 CalculateFundamentalMatrix();
00449                 
00450                 // reset transformations
00451                 m_pLeftCalibration->SetRotation(left_rotation);
00452                 m_pLeftCalibration->SetTranslation(left_translation);
00453                 
00454                 m_pRightCalibration->SetRotation(right_rotation);
00455                 m_pRightCalibration->SetTranslation(right_translation);
00456         }
00457         
00458         int nCameraCount = 0;
00459         
00460         FILE* f = fopen(pCameraParameterFileName, "r");
00461         if(!f)
00462                 return false;
00463         
00464         if (fscanf(f, "%d", &nCameraCount) != 1 || nCameraCount != 2)
00465         {
00466                 fclose(f);
00467         return false;
00468         }
00469         
00470         float skip_value;
00471         
00472         // skip values (27 for each camera, 2 * 8 for stereo quads)
00473         for (int i = 0; i < 2 * 27 + 16; i++)
00474                 if (fscanf(f, "%f", &skip_value) != 1)
00475                 {
00476                         fclose(f);
00477                         return false;
00478                 }
00479         
00480         // read rectification parameters
00481         if (fscanf(f, "%f", &rectificationHomographyLeft.r1) != 1) { fclose(f); return false; }
00482         if (fscanf(f, "%f", &rectificationHomographyLeft.r2) != 1) { fclose(f); return false; }
00483         if (fscanf(f, "%f", &rectificationHomographyLeft.r3) != 1) { fclose(f); return false; }
00484         if (fscanf(f, "%f", &rectificationHomographyLeft.r4) != 1) { fclose(f); return false; }
00485         if (fscanf(f, "%f", &rectificationHomographyLeft.r5) != 1) { fclose(f); return false; }
00486         if (fscanf(f, "%f", &rectificationHomographyLeft.r6) != 1) { fclose(f); return false; }
00487         if (fscanf(f, "%f", &rectificationHomographyLeft.r7) != 1) { fclose(f); return false; }
00488         if (fscanf(f, "%f", &rectificationHomographyLeft.r8) != 1) { fclose(f); return false; }
00489         if (fscanf(f, "%f", &rectificationHomographyLeft.r9) != 1) { fclose(f); return false; }
00490         
00491         if (fscanf(f, "%f", &rectificationHomographyRight.r1) != 1) { fclose(f); return false; }
00492         if (fscanf(f, "%f", &rectificationHomographyRight.r2) != 1) { fclose(f); return false; }
00493         if (fscanf(f, "%f", &rectificationHomographyRight.r3) != 1) { fclose(f); return false; }
00494         if (fscanf(f, "%f", &rectificationHomographyRight.r4) != 1) { fclose(f); return false; }
00495         if (fscanf(f, "%f", &rectificationHomographyRight.r5) != 1) { fclose(f); return false; }
00496         if (fscanf(f, "%f", &rectificationHomographyRight.r6) != 1) { fclose(f); return false; }
00497         if (fscanf(f, "%f", &rectificationHomographyRight.r7) != 1) { fclose(f); return false; }
00498         if (fscanf(f, "%f", &rectificationHomographyRight.r8) != 1) { fclose(f); return false; }
00499         if (fscanf(f, "%f", &rectificationHomographyRight.r9) != 1) { fclose(f); return false; }
00500         
00501         fclose(f);
00502         
00503         width = m_pLeftCalibration->GetCameraParameters().width;
00504         height = m_pLeftCalibration->GetCameraParameters().height;
00505         
00506         return true;
00507 }
00508 
00509 bool CStereoCalibration::SaveCameraParameters(const char *pFileName) const
00510 {
00511         FILE *f = fopen(pFileName, "w");
00512         if (!f)
00513                 return false;
00514         
00515         fprintf(f, "2\n\n");
00516         
00517         const CCalibration::CCameraParameters &cameraParametersLeft = m_pLeftCalibration->GetCameraParameters();
00518         const CCalibration::CCameraParameters &cameraParametersRight = m_pRightCalibration->GetCameraParameters();
00519         
00520         // write camera parameters of left camera
00521         float cx = cameraParametersLeft.principalPoint.x;
00522         float cy = cameraParametersLeft.principalPoint.y;
00523         float fx = cameraParametersLeft.focalLength.x;
00524         float fy = cameraParametersLeft.focalLength.y;
00525         float d1 = cameraParametersLeft.distortion[0];
00526         float d2 = cameraParametersLeft.distortion[1];
00527         float d3 = cameraParametersLeft.distortion[2];
00528         float d4 = cameraParametersLeft.distortion[3];
00529         
00530         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);
00531         fprintf(f, "%.10f %.10f %.10f %.10f ", d1, d2, d3, d4);
00532         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);
00533         fprintf(f, "%.10f %.10f %.10f\n\n", cameraParametersLeft.translation.x, cameraParametersLeft.translation.y, cameraParametersLeft.translation.z);
00534         
00535         // write camera parameters of right camera
00536         cx = cameraParametersRight.principalPoint.x;
00537         cy = cameraParametersRight.principalPoint.y;
00538         fx = cameraParametersRight.focalLength.x;
00539         fy = cameraParametersRight.focalLength.y;
00540         d1 = cameraParametersRight.distortion[0];
00541         d2 = cameraParametersRight.distortion[1];
00542         d3 = cameraParametersRight.distortion[2];
00543         d4 = cameraParametersRight.distortion[3];
00544         
00545         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);
00546         fprintf(f, "%.10f %.10f %.10f %.10f ", d1, d2, d3, d4);
00547         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);
00548         fprintf(f, "%.10f %.10f %.10f\n\n", cameraParametersRight.translation.x, cameraParametersRight.translation.y, cameraParametersRight.translation.z);
00549         
00550         // skipped values of OpenCV format
00551         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);
00552         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);
00553         
00554         // write rectification homography of left camera
00555         fprintf(f, "%.10f ", rectificationHomographyLeft.r1);
00556         fprintf(f, "%.10f ", rectificationHomographyLeft.r2);
00557         fprintf(f, "%.10f ", rectificationHomographyLeft.r3);
00558         fprintf(f, "%.10f ", rectificationHomographyLeft.r4);
00559         fprintf(f, "%.10f ", rectificationHomographyLeft.r5);
00560         fprintf(f, "%.10f ", rectificationHomographyLeft.r6);
00561         fprintf(f, "%.10f ", rectificationHomographyLeft.r7);
00562         fprintf(f, "%.10f ", rectificationHomographyLeft.r8);
00563         fprintf(f, "%.10f ", rectificationHomographyLeft.r9);
00564         fprintf(f, "\n");
00565         
00566         // write rectification homography of right camera
00567         fprintf(f, "%.10f ", rectificationHomographyRight.r1);
00568         fprintf(f, "%.10f ", rectificationHomographyRight.r2);
00569         fprintf(f, "%.10f ", rectificationHomographyRight.r3);
00570         fprintf(f, "%.10f ", rectificationHomographyRight.r4);
00571         fprintf(f, "%.10f ", rectificationHomographyRight.r5);
00572         fprintf(f, "%.10f ", rectificationHomographyRight.r6);
00573         fprintf(f, "%.10f ", rectificationHomographyRight.r7);
00574         fprintf(f, "%.10f ", rectificationHomographyRight.r8);
00575         fprintf(f, "%.10f ", rectificationHomographyRight.r9);
00576         fprintf(f, "\n");
00577         
00578         fclose(f);
00579         
00580         return true;
00581 }
00582 
00583 void CStereoCalibration::GetProjectionMatricesForRectifiedImages(Mat3d &P1Left, Vec3d &p2Left, Mat3d &P1Right, Vec3d &p2Right) const
00584 {
00585         Mat3d K, H;
00586         
00587         // left camera
00588         Math3d::Invert(rectificationHomographyLeft, H);
00589         m_pLeftCalibration->GetCalibrationMatrix(K);
00590         Math3d::MulMatMat(H, K, K);
00591         Math3d::MulMatMat(K, m_pLeftCalibration->GetCameraParameters().rotation, P1Left);
00592         Math3d::MulMatVec(K, m_pLeftCalibration->GetCameraParameters().translation, p2Left);
00593         
00594         // right camera
00595         Math3d::Invert(rectificationHomographyRight, H);
00596         m_pRightCalibration->GetCalibrationMatrix(K);
00597         Math3d::MulMatMat(H, K, K);
00598         Math3d::MulMatMat(K, m_pRightCalibration->GetCameraParameters().rotation, P1Right);
00599         Math3d::MulMatVec(K, m_pRightCalibration->GetCameraParameters().translation, p2Right);
00600 }


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