Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/stitching/src/autocalib.cpp
16337 views
1
/*M///////////////////////////////////////////////////////////////////////////////////////
2
//
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4
//
5
// By downloading, copying, installing or using the software you agree to this license.
6
// If you do not agree to this license, do not download, install,
7
// copy or use the software.
8
//
9
//
10
// License Agreement
11
// For Open Source Computer Vision Library
12
//
13
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15
// Third party copyrights are property of their respective owners.
16
//
17
// Redistribution and use in source and binary forms, with or without modification,
18
// are permitted provided that the following conditions are met:
19
//
20
// * Redistribution's of source code must retain the above copyright notice,
21
// this list of conditions and the following disclaimer.
22
//
23
// * Redistribution's in binary form must reproduce the above copyright notice,
24
// this list of conditions and the following disclaimer in the documentation
25
// and/or other materials provided with the distribution.
26
//
27
// * The name of the copyright holders may not be used to endorse or promote products
28
// derived from this software without specific prior written permission.
29
//
30
// This software is provided by the copyright holders and contributors "as is" and
31
// any express or implied warranties, including, but not limited to, the implied
32
// warranties of merchantability and fitness for a particular purpose are disclaimed.
33
// In no event shall the Intel Corporation or contributors be liable for any direct,
34
// indirect, incidental, special, exemplary, or consequential damages
35
// (including, but not limited to, procurement of substitute goods or services;
36
// loss of use, data, or profits; or business interruption) however caused
37
// and on any theory of liability, whether in contract, strict liability,
38
// or tort (including negligence or otherwise) arising in any way out of
39
// the use of this software, even if advised of the possibility of such damage.
40
//
41
//M*/
42
43
#include "precomp.hpp"
44
#include "opencv2/core/hal/hal.hpp"
45
46
using namespace cv;
47
48
namespace {
49
50
static inline bool decomposeCholesky(double* A, size_t astep, int m)
51
{
52
if (!hal::Cholesky64f(A, astep, m, 0, 0, 0))
53
return false;
54
return true;
55
}
56
57
} // namespace
58
59
60
namespace cv {
61
namespace detail {
62
63
void focalsFromHomography(const Mat& H, double &f0, double &f1, bool &f0_ok, bool &f1_ok)
64
{
65
CV_Assert(H.type() == CV_64F && H.size() == Size(3, 3));
66
67
const double* h = H.ptr<double>();
68
69
double d1, d2; // Denominators
70
double v1, v2; // Focal squares value candidates
71
72
f1_ok = true;
73
d1 = h[6] * h[7];
74
d2 = (h[7] - h[6]) * (h[7] + h[6]);
75
v1 = -(h[0] * h[1] + h[3] * h[4]) / d1;
76
v2 = (h[0] * h[0] + h[3] * h[3] - h[1] * h[1] - h[4] * h[4]) / d2;
77
if (v1 < v2) std::swap(v1, v2);
78
if (v1 > 0 && v2 > 0) f1 = std::sqrt(std::abs(d1) > std::abs(d2) ? v1 : v2);
79
else if (v1 > 0) f1 = std::sqrt(v1);
80
else f1_ok = false;
81
82
f0_ok = true;
83
d1 = h[0] * h[3] + h[1] * h[4];
84
d2 = h[0] * h[0] + h[1] * h[1] - h[3] * h[3] - h[4] * h[4];
85
v1 = -h[2] * h[5] / d1;
86
v2 = (h[5] * h[5] - h[2] * h[2]) / d2;
87
if (v1 < v2) std::swap(v1, v2);
88
if (v1 > 0 && v2 > 0) f0 = std::sqrt(std::abs(d1) > std::abs(d2) ? v1 : v2);
89
else if (v1 > 0) f0 = std::sqrt(v1);
90
else f0_ok = false;
91
}
92
93
94
void estimateFocal(const std::vector<ImageFeatures> &features, const std::vector<MatchesInfo> &pairwise_matches,
95
std::vector<double> &focals)
96
{
97
const int num_images = static_cast<int>(features.size());
98
focals.resize(num_images);
99
100
std::vector<double> all_focals;
101
102
for (int i = 0; i < num_images; ++i)
103
{
104
for (int j = 0; j < num_images; ++j)
105
{
106
const MatchesInfo &m = pairwise_matches[i*num_images + j];
107
if (m.H.empty())
108
continue;
109
double f0, f1;
110
bool f0ok, f1ok;
111
focalsFromHomography(m.H, f0, f1, f0ok, f1ok);
112
if (f0ok && f1ok)
113
all_focals.push_back(std::sqrt(f0 * f1));
114
}
115
}
116
117
if (static_cast<int>(all_focals.size()) >= num_images - 1)
118
{
119
double median;
120
121
std::sort(all_focals.begin(), all_focals.end());
122
if (all_focals.size() % 2 == 1)
123
median = all_focals[all_focals.size() / 2];
124
else
125
median = (all_focals[all_focals.size() / 2 - 1] + all_focals[all_focals.size() / 2]) * 0.5;
126
127
for (int i = 0; i < num_images; ++i)
128
focals[i] = median;
129
}
130
else
131
{
132
LOGLN("Can't estimate focal length, will use naive approach");
133
double focals_sum = 0;
134
for (int i = 0; i < num_images; ++i)
135
focals_sum += features[i].img_size.width + features[i].img_size.height;
136
for (int i = 0; i < num_images; ++i)
137
focals[i] = focals_sum / num_images;
138
}
139
}
140
141
142
bool calibrateRotatingCamera(const std::vector<Mat> &Hs, Mat &K)
143
{
144
int m = static_cast<int>(Hs.size());
145
CV_Assert(m >= 1);
146
147
std::vector<Mat> Hs_(m);
148
for (int i = 0; i < m; ++i)
149
{
150
CV_Assert(Hs[i].size() == Size(3, 3) && Hs[i].type() == CV_64F);
151
Hs_[i] = Hs[i] / std::pow(determinant(Hs[i]), 1./3.);
152
}
153
154
const int idx_map[3][3] = {{0, 1, 2}, {1, 3, 4}, {2, 4, 5}};
155
Mat_<double> A(6*m, 6);
156
A.setTo(0);
157
158
int eq_idx = 0;
159
for (int k = 0; k < m; ++k)
160
{
161
Mat_<double> H(Hs_[k]);
162
for (int i = 0; i < 3; ++i)
163
{
164
for (int j = i; j < 3; ++j, ++eq_idx)
165
{
166
for (int l = 0; l < 3; ++l)
167
{
168
for (int s = 0; s < 3; ++s)
169
{
170
int idx = idx_map[l][s];
171
A(eq_idx, idx) += H(i,l) * H(j,s);
172
}
173
}
174
A(eq_idx, idx_map[i][j]) -= 1;
175
}
176
}
177
}
178
179
Mat_<double> wcoef;
180
SVD::solveZ(A, wcoef);
181
182
Mat_<double> W(3,3);
183
for (int i = 0; i < 3; ++i)
184
for (int j = i; j < 3; ++j)
185
W(i,j) = W(j,i) = wcoef(idx_map[i][j], 0) / wcoef(5,0);
186
if (!decomposeCholesky(W.ptr<double>(), W.step, 3))
187
return false;
188
W(0,1) = W(0,2) = W(1,2) = 0;
189
K = W.t();
190
return true;
191
}
192
193
} // namespace detail
194
} // namespace cv
195
196