autocalib.cpp 6.37 KB
Newer Older
wester committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                          License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of the copyright holders may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

#include "precomp.hpp"

wester committed
45
using namespace std;
wester committed
46 47 48 49
using namespace cv;

namespace {

wester committed
50 51
template<typename _Tp> static inline bool
decomposeCholesky(_Tp* A, size_t astep, int m)
wester committed
52
{
wester committed
53
    if (!Cholesky(A, astep, m, 0, 0, 0))
wester committed
54
        return false;
a  
Kai Westerkamp committed
55 56
    astep /= sizeof(A[0]);
    for (int i = 0; i < m; ++i)
wester committed
57
        A[i*astep + i] = (_Tp)(1./A[i*astep + i]);
wester committed
58 59 60 61 62 63 64 65 66 67 68 69 70
    return true;
}

} // namespace


namespace cv {
namespace detail {

void focalsFromHomography(const Mat& H, double &f0, double &f1, bool &f0_ok, bool &f1_ok)
{
    CV_Assert(H.type() == CV_64F && H.size() == Size(3, 3));

wester committed
71
    const double* h = reinterpret_cast<const double*>(H.data);
wester committed
72 73 74 75 76 77 78 79 80 81

    double d1, d2; // Denominators
    double v1, v2; // Focal squares value candidates

    f1_ok = true;
    d1 = h[6] * h[7];
    d2 = (h[7] - h[6]) * (h[7] + h[6]);
    v1 = -(h[0] * h[1] + h[3] * h[4]) / d1;
    v2 = (h[0] * h[0] + h[3] * h[3] - h[1] * h[1] - h[4] * h[4]) / d2;
    if (v1 < v2) std::swap(v1, v2);
wester committed
82 83
    if (v1 > 0 && v2 > 0) f1 = sqrt(std::abs(d1) > std::abs(d2) ? v1 : v2);
    else if (v1 > 0) f1 = sqrt(v1);
wester committed
84 85 86 87 88 89 90 91
    else f1_ok = false;

    f0_ok = true;
    d1 = h[0] * h[3] + h[1] * h[4];
    d2 = h[0] * h[0] + h[1] * h[1] - h[3] * h[3] - h[4] * h[4];
    v1 = -h[2] * h[5] / d1;
    v2 = (h[5] * h[5] - h[2] * h[2]) / d2;
    if (v1 < v2) std::swap(v1, v2);
wester committed
92 93
    if (v1 > 0 && v2 > 0) f0 = sqrt(std::abs(d1) > std::abs(d2) ? v1 : v2);
    else if (v1 > 0) f0 = sqrt(v1);
wester committed
94 95 96 97
    else f0_ok = false;
}


wester committed
98 99
void estimateFocal(const vector<ImageFeatures> &features, const vector<MatchesInfo> &pairwise_matches,
                       vector<double> &focals)
wester committed
100 101 102 103
{
    const int num_images = static_cast<int>(features.size());
    focals.resize(num_images);

wester committed
104
    vector<double> all_focals;
wester committed
105 106 107 108 109 110 111 112 113 114 115 116

    for (int i = 0; i < num_images; ++i)
    {
        for (int j = 0; j < num_images; ++j)
        {
            const MatchesInfo &m = pairwise_matches[i*num_images + j];
            if (m.H.empty())
                continue;
            double f0, f1;
            bool f0ok, f1ok;
            focalsFromHomography(m.H, f0, f1, f0ok, f1ok);
            if (f0ok && f1ok)
wester committed
117
                all_focals.push_back(sqrt(f0 * f1));
wester committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
        }
    }

    if (static_cast<int>(all_focals.size()) >= num_images - 1)
    {
        double median;

        std::sort(all_focals.begin(), all_focals.end());
        if (all_focals.size() % 2 == 1)
            median = all_focals[all_focals.size() / 2];
        else
            median = (all_focals[all_focals.size() / 2 - 1] + all_focals[all_focals.size() / 2]) * 0.5;

        for (int i = 0; i < num_images; ++i)
            focals[i] = median;
    }
    else
    {
        LOGLN("Can't estimate focal length, will use naive approach");
        double focals_sum = 0;
        for (int i = 0; i < num_images; ++i)
            focals_sum += features[i].img_size.width + features[i].img_size.height;
        for (int i = 0; i < num_images; ++i)
            focals[i] = focals_sum / num_images;
    }
}


wester committed
146
bool calibrateRotatingCamera(const vector<Mat> &Hs, Mat &K)
wester committed
147 148 149 150
{
    int m = static_cast<int>(Hs.size());
    CV_Assert(m >= 1);

wester committed
151
    vector<Mat> Hs_(m);
wester committed
152 153 154
    for (int i = 0; i < m; ++i)
    {
        CV_Assert(Hs[i].size() == Size(3, 3) && Hs[i].type() == CV_64F);
wester committed
155
        Hs_[i] = Hs[i] / pow(determinant(Hs[i]), 1./3.);
wester committed
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
    }

    const int idx_map[3][3] = {{0, 1, 2}, {1, 3, 4}, {2, 4, 5}};
    Mat_<double> A(6*m, 6);
    A.setTo(0);

    int eq_idx = 0;
    for (int k = 0; k < m; ++k)
    {
        Mat_<double> H(Hs_[k]);
        for (int i = 0; i < 3; ++i)
        {
            for (int j = i; j < 3; ++j, ++eq_idx)
            {
                for (int l = 0; l < 3; ++l)
                {
                    for (int s = 0; s < 3; ++s)
                    {
                        int idx = idx_map[l][s];
                        A(eq_idx, idx) += H(i,l) * H(j,s);
                    }
                }
                A(eq_idx, idx_map[i][j]) -= 1;
            }
        }
    }

    Mat_<double> wcoef;
    SVD::solveZ(A, wcoef);

    Mat_<double> W(3,3);
    for (int i = 0; i < 3; ++i)
        for (int j = i; j < 3; ++j)
            W(i,j) = W(j,i) = wcoef(idx_map[i][j], 0) / wcoef(5,0);
    if (!decomposeCholesky(W.ptr<double>(), W.step, 3))
        return false;
    W(0,1) = W(0,2) = W(1,2) = 0;
    K = W.t();
    return true;
}

} // namespace detail
} // namespace cv