Path: blob/master/modules/stitching/src/autocalib.cpp
16337 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.13// Copyright (C) 2009, Willow Garage Inc., all rights reserved.14// Third party copyrights are property of their respective owners.15//16// Redistribution and use in source and binary forms, with or without modification,17// are permitted provided that the following conditions are met:18//19// * Redistribution's of source code must retain the above copyright notice,20// this list of conditions and the following disclaimer.21//22// * Redistribution's in binary form must reproduce the above copyright notice,23// this list of conditions and the following disclaimer in the documentation24// and/or other materials provided with the distribution.25//26// * The name of the copyright holders may not be used to endorse or promote products27// derived from this software without specific prior written permission.28//29// This software is provided by the copyright holders and contributors "as is" and30// any express or implied warranties, including, but not limited to, the implied31// warranties of merchantability and fitness for a particular purpose are disclaimed.32// In no event shall the Intel Corporation or contributors be liable for any direct,33// indirect, incidental, special, exemplary, or consequential damages34// (including, but not limited to, procurement of substitute goods or services;35// loss of use, data, or profits; or business interruption) however caused36// and on any theory of liability, whether in contract, strict liability,37// or tort (including negligence or otherwise) arising in any way out of38// the use of this software, even if advised of the possibility of such damage.39//40//M*/4142#include "precomp.hpp"43#include "opencv2/core/hal/hal.hpp"4445using namespace cv;4647namespace {4849static inline bool decomposeCholesky(double* A, size_t astep, int m)50{51if (!hal::Cholesky64f(A, astep, m, 0, 0, 0))52return false;53return true;54}5556} // namespace575859namespace cv {60namespace detail {6162void focalsFromHomography(const Mat& H, double &f0, double &f1, bool &f0_ok, bool &f1_ok)63{64CV_Assert(H.type() == CV_64F && H.size() == Size(3, 3));6566const double* h = H.ptr<double>();6768double d1, d2; // Denominators69double v1, v2; // Focal squares value candidates7071f1_ok = true;72d1 = h[6] * h[7];73d2 = (h[7] - h[6]) * (h[7] + h[6]);74v1 = -(h[0] * h[1] + h[3] * h[4]) / d1;75v2 = (h[0] * h[0] + h[3] * h[3] - h[1] * h[1] - h[4] * h[4]) / d2;76if (v1 < v2) std::swap(v1, v2);77if (v1 > 0 && v2 > 0) f1 = std::sqrt(std::abs(d1) > std::abs(d2) ? v1 : v2);78else if (v1 > 0) f1 = std::sqrt(v1);79else f1_ok = false;8081f0_ok = true;82d1 = h[0] * h[3] + h[1] * h[4];83d2 = h[0] * h[0] + h[1] * h[1] - h[3] * h[3] - h[4] * h[4];84v1 = -h[2] * h[5] / d1;85v2 = (h[5] * h[5] - h[2] * h[2]) / d2;86if (v1 < v2) std::swap(v1, v2);87if (v1 > 0 && v2 > 0) f0 = std::sqrt(std::abs(d1) > std::abs(d2) ? v1 : v2);88else if (v1 > 0) f0 = std::sqrt(v1);89else f0_ok = false;90}919293void estimateFocal(const std::vector<ImageFeatures> &features, const std::vector<MatchesInfo> &pairwise_matches,94std::vector<double> &focals)95{96const int num_images = static_cast<int>(features.size());97focals.resize(num_images);9899std::vector<double> all_focals;100101for (int i = 0; i < num_images; ++i)102{103for (int j = 0; j < num_images; ++j)104{105const MatchesInfo &m = pairwise_matches[i*num_images + j];106if (m.H.empty())107continue;108double f0, f1;109bool f0ok, f1ok;110focalsFromHomography(m.H, f0, f1, f0ok, f1ok);111if (f0ok && f1ok)112all_focals.push_back(std::sqrt(f0 * f1));113}114}115116if (static_cast<int>(all_focals.size()) >= num_images - 1)117{118double median;119120std::sort(all_focals.begin(), all_focals.end());121if (all_focals.size() % 2 == 1)122median = all_focals[all_focals.size() / 2];123else124median = (all_focals[all_focals.size() / 2 - 1] + all_focals[all_focals.size() / 2]) * 0.5;125126for (int i = 0; i < num_images; ++i)127focals[i] = median;128}129else130{131LOGLN("Can't estimate focal length, will use naive approach");132double focals_sum = 0;133for (int i = 0; i < num_images; ++i)134focals_sum += features[i].img_size.width + features[i].img_size.height;135for (int i = 0; i < num_images; ++i)136focals[i] = focals_sum / num_images;137}138}139140141bool calibrateRotatingCamera(const std::vector<Mat> &Hs, Mat &K)142{143int m = static_cast<int>(Hs.size());144CV_Assert(m >= 1);145146std::vector<Mat> Hs_(m);147for (int i = 0; i < m; ++i)148{149CV_Assert(Hs[i].size() == Size(3, 3) && Hs[i].type() == CV_64F);150Hs_[i] = Hs[i] / std::pow(determinant(Hs[i]), 1./3.);151}152153const int idx_map[3][3] = {{0, 1, 2}, {1, 3, 4}, {2, 4, 5}};154Mat_<double> A(6*m, 6);155A.setTo(0);156157int eq_idx = 0;158for (int k = 0; k < m; ++k)159{160Mat_<double> H(Hs_[k]);161for (int i = 0; i < 3; ++i)162{163for (int j = i; j < 3; ++j, ++eq_idx)164{165for (int l = 0; l < 3; ++l)166{167for (int s = 0; s < 3; ++s)168{169int idx = idx_map[l][s];170A(eq_idx, idx) += H(i,l) * H(j,s);171}172}173A(eq_idx, idx_map[i][j]) -= 1;174}175}176}177178Mat_<double> wcoef;179SVD::solveZ(A, wcoef);180181Mat_<double> W(3,3);182for (int i = 0; i < 3; ++i)183for (int j = i; j < 3; ++j)184W(i,j) = W(j,i) = wcoef(idx_map[i][j], 0) / wcoef(5,0);185if (!decomposeCholesky(W.ptr<double>(), W.step, 3))186return false;187W(0,1) = W(0,2) = W(1,2) = 0;188K = W.t();189return true;190}191192} // namespace detail193} // namespace cv194195196