modified focal estimation function in opencv_stitching
This commit is contained in:
parent
34e2c78cec
commit
60e1eda149
@ -8,99 +8,63 @@ void focalsFromHomography(const Mat& H, double &f0, double &f1, bool &f0_ok, boo
|
|||||||
{
|
{
|
||||||
CV_Assert(H.type() == CV_64F && H.size() == Size(3, 3));
|
CV_Assert(H.type() == CV_64F && H.size() == Size(3, 3));
|
||||||
|
|
||||||
const double h[9] =
|
const double* h = reinterpret_cast<const double*>(H.data);
|
||||||
{
|
|
||||||
H.at<double>(0, 0), H.at<double>(0, 1), H.at<double>(0, 2),
|
double d1, d2; // Denominators
|
||||||
H.at<double>(1, 0), H.at<double>(1, 1), H.at<double>(1, 2),
|
double v1, v2; // Focal squares value candidates
|
||||||
H.at<double>(2, 0), H.at<double>(2, 1), H.at<double>(2, 2)
|
|
||||||
};
|
|
||||||
|
|
||||||
f1_ok = true;
|
f1_ok = true;
|
||||||
double denom1 = h[6] * h[7];
|
d1 = h[6] * h[7];
|
||||||
double denom2 = (h[7] - h[6]) * (h[7] + h[6]);
|
d2 = (h[7] - h[6]) * (h[7] + h[6]);
|
||||||
if (max(abs(denom1), abs(denom2)) < 1e-5)
|
v1 = -(h[0] * h[1] + h[3] * h[4]) / d1;
|
||||||
f1_ok = false;
|
v2 = (h[0] * h[0] + h[3] * h[3] - h[1] * h[1] - h[4] * h[4]) / d2;
|
||||||
else
|
if (v1 < v2) swap(v1, v2);
|
||||||
{
|
if (v1 > 0 && v2 > 0) f1 = sqrt(abs(d1) > abs(d2) ? v1 : v2);
|
||||||
double val1 = -(h[0] * h[1] + h[3] * h[4]) / denom1;
|
else if (v1 > 0) f1 = sqrt(v1);
|
||||||
double val2 = (h[0] * h[0] + h[3] * h[3] - h[1] * h[1] - h[4] * h[4]) / denom2;
|
else f1_ok = false;
|
||||||
if (val1 < val2)
|
|
||||||
swap(val1, val2);
|
|
||||||
if (val1 > 0 && val2 > 0)
|
|
||||||
f1 = sqrt(abs(denom1) > abs(denom2) ? val1 : val2);
|
|
||||||
else if (val1 > 0)
|
|
||||||
f1 = sqrt(val1);
|
|
||||||
else
|
|
||||||
f1_ok = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
f0_ok = true;
|
f0_ok = true;
|
||||||
denom1 = h[0] * h[3] + h[1] * h[4];
|
d1 = h[0] * h[3] + h[1] * h[4];
|
||||||
denom2 = h[0] * h[0] + h[1] * h[1] - h[3] * h[3] - h[4] * h[4];
|
d2 = h[0] * h[0] + h[1] * h[1] - h[3] * h[3] - h[4] * h[4];
|
||||||
if (max(abs(denom1), abs(denom2)) < 1e-5)
|
v1 = -h[2] * h[5] / d1;
|
||||||
f0_ok = false;
|
v2 = (h[5] * h[5] - h[2] * h[2]) / d2;
|
||||||
else
|
if (v1 < v2) swap(v1, v2);
|
||||||
{
|
if (v1 > 0 && v2 > 0) f0 = sqrt(abs(d1) > abs(d2) ? v1 : v2);
|
||||||
double val1 = -h[2] * h[5] / denom1;
|
else if (v1 > 0) f0 = sqrt(v1);
|
||||||
double val2 = (h[5] * h[5] - h[2] * h[2]) / denom2;
|
else f0_ok = false;
|
||||||
if (val1 < val2)
|
|
||||||
swap(val1, val2);
|
|
||||||
if (val1 > 0 && val2 > 0)
|
|
||||||
f0 = sqrt(abs(denom1) > abs(denom2) ? val1 : val2);
|
|
||||||
else if (val1 > 0)
|
|
||||||
f0 = sqrt(val1);
|
|
||||||
else
|
|
||||||
f0_ok = false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool focalsFromFundamental(const Mat &F, double &f0, double &f1)
|
double estimateFocal(const vector<Mat> &images, const vector<ImageFeatures> &/*features*/,
|
||||||
|
const vector<MatchesInfo> &pairwise_matches)
|
||||||
{
|
{
|
||||||
CV_Assert(F.type() == CV_64F);
|
const int num_images = static_cast<int>(images.size());
|
||||||
CV_Assert(F.size() == Size(3, 3));
|
|
||||||
|
|
||||||
Mat Ft = F.t();
|
vector<double> focals;
|
||||||
Mat k = Mat::zeros(3, 1, CV_64F);
|
for (int src_idx = 0; src_idx < num_images; ++src_idx)
|
||||||
k.at<double>(2, 0) = 1.f;
|
{
|
||||||
|
for (int dst_idx = 0; dst_idx < num_images; ++dst_idx)
|
||||||
|
{
|
||||||
|
const MatchesInfo &m = pairwise_matches[src_idx*num_images + dst_idx];
|
||||||
|
if (m.H.empty())
|
||||||
|
continue;
|
||||||
|
|
||||||
// 1. Compute quantities
|
double f0, f1;
|
||||||
double a = normL2sq(F*Ft*k) / normL2sq(Ft*k);
|
bool f0ok, f1ok;
|
||||||
double b = normL2sq(Ft*F*k) / normL2sq(F*k);
|
focalsFromHomography(m.H, f0, f1, f0ok, f1ok);
|
||||||
double c = sqr(k.dot(F*k)) / (normL2sq(Ft*k) * normL2sq(F*k));
|
if (f0ok && f1ok) focals.push_back(sqrt(f0*f1));
|
||||||
double d = k.dot(F*Ft*F*k) / k.dot(F*k);
|
}
|
||||||
double A = 1/c + a - 2*d;
|
}
|
||||||
double B = 1/c + b - 2*d;
|
|
||||||
double P = 2*(1/c - 2*d + 0.5*normL2sq(F));
|
|
||||||
double Q = -(A + B)/c + 0.5*(normL2sq(F*Ft) - 0.5*sqr(normL2sq(F)));
|
|
||||||
|
|
||||||
// 2. Solve quadratic equation Z*Z*a_ + Z*b_ + c_ = 0
|
if (focals.size() + 1 >= images.size())
|
||||||
double a_ = 1 + c*P;
|
{
|
||||||
double b_ = -(c*P*P + 2*P + 4*c*Q);
|
nth_element(focals.begin(), focals.end(), focals.begin() + focals.size()/2);
|
||||||
double c_ = P*P + 4*c*P*Q + 12*A*B;
|
return focals[focals.size()/2];
|
||||||
double D = b_*b_ - 4*a_*c_;
|
}
|
||||||
if (abs(D) < 1e-5)
|
|
||||||
D = 0;
|
|
||||||
else if (D < 0)
|
|
||||||
return false;
|
|
||||||
double D_sqrt = sqrt(D);
|
|
||||||
double Z0 = (-b_ - D_sqrt) / (2*a_);
|
|
||||||
double Z1 = (-b_ + D_sqrt) / (2*a_);
|
|
||||||
|
|
||||||
// 3. Choose solution
|
LOGLN("Can't estimate focal length, will use naive approach");
|
||||||
double w0 = abs(Z0*Z0*Z0 - 3*P*Z0*Z0 + 2*(P*P + 2*Q)*Z0 - 4*(P*Q + 4*A*B/c));
|
double focals_sum = 0;
|
||||||
double w1 = abs(Z1*Z1*Z1 - 3*P*Z1*Z1 + 2*(P*P + 2*Q)*Z1 - 4*(P*Q + 4*A*B/c));
|
for (int i = 0; i < num_images; ++i)
|
||||||
double Z = Z0;
|
focals_sum += images[i].rows + images[i].cols;
|
||||||
if (w1 < w0)
|
return focals_sum / num_images;
|
||||||
Z = Z1;
|
|
||||||
|
|
||||||
// 4.
|
|
||||||
double X = -1/c*(1 + 2*B/(Z - P));
|
|
||||||
double Y = -1/c*(1 + 2*A/(Z - P));
|
|
||||||
|
|
||||||
// 5. Compute focal lengths
|
|
||||||
f0 = 1/sqrt(1 + X/normL2sq(Ft*k));
|
|
||||||
f1 = 1/sqrt(1 + Y/normL2sq(F*k));
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,15 @@
|
|||||||
#ifndef __OPENCV_AUTOCALIB_HPP__
|
#ifndef __OPENCV_AUTOCALIB_HPP__
|
||||||
#define __OPENCV_AUTOCALIB_HPP__
|
#define __OPENCV_AUTOCALIB_HPP__
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
#include <opencv2/core/core.hpp>
|
#include <opencv2/core/core.hpp>
|
||||||
|
#include "matchers.hpp"
|
||||||
|
|
||||||
// See "Construction of Panoramic Image Mosaics with Global and Local Alignment"
|
// See "Construction of Panoramic Image Mosaics with Global and Local Alignment"
|
||||||
// by Heung-Yeung Shum and Richard Szeliski.
|
// by Heung-Yeung Shum and Richard Szeliski.
|
||||||
void focalsFromHomography(const cv::Mat &H, double &f0, double &f1, bool &f0_ok, bool &f1_ok);
|
void focalsFromHomography(const cv::Mat &H, double &f0, double &f1, bool &f0_ok, bool &f1_ok);
|
||||||
|
|
||||||
bool focalsFromFundamental(const cv::Mat &F, double &f0, double &f1);
|
double estimateFocal(const std::vector<cv::Mat> &images, const std::vector<ImageFeatures> &features,
|
||||||
|
const std::vector<MatchesInfo> &pairwise_matches);
|
||||||
|
|
||||||
#endif // __OPENCV_AUTOCALIB_HPP__
|
#endif // __OPENCV_AUTOCALIB_HPP__
|
||||||
|
@ -64,46 +64,16 @@ struct CalcRotation
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
void HomographyBasedEstimator::estimate(const vector<Mat> &images, const vector<ImageFeatures> &/*features*/,
|
void HomographyBasedEstimator::estimate(const vector<Mat> &images, const vector<ImageFeatures> &features,
|
||||||
const vector<MatchesInfo> &pairwise_matches, vector<CameraParams> &cameras)
|
const vector<MatchesInfo> &pairwise_matches, vector<CameraParams> &cameras)
|
||||||
{
|
{
|
||||||
const int num_images = static_cast<int>(images.size());
|
const int num_images = static_cast<int>(images.size());
|
||||||
|
|
||||||
// Find focals from pair-wise homographies
|
// Estimate focal length and set it for all cameras
|
||||||
vector<bool> is_focal_estimated(num_images, false);
|
double focal = estimateFocal(images, features, pairwise_matches);
|
||||||
vector<double> focals;
|
|
||||||
for (int i = 0; i < num_images; ++i)
|
|
||||||
{
|
|
||||||
for (int j = 0; j < num_images; ++j)
|
|
||||||
{
|
|
||||||
int pair_idx = i * num_images + j;
|
|
||||||
if (pairwise_matches[pair_idx].H.empty())
|
|
||||||
continue;
|
|
||||||
|
|
||||||
double f_to, f_from;
|
|
||||||
bool f_to_ok, f_from_ok;
|
|
||||||
focalsFromHomography(pairwise_matches[pair_idx].H.inv(), f_to, f_from, f_to_ok, f_from_ok);
|
|
||||||
|
|
||||||
if (f_from_ok) focals.push_back(f_from);
|
|
||||||
if (f_to_ok) focals.push_back(f_to);
|
|
||||||
|
|
||||||
if (f_from_ok && f_to_ok)
|
|
||||||
{
|
|
||||||
is_focal_estimated[i] = true;
|
|
||||||
is_focal_estimated[j] = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
is_focals_estimated_ = true;
|
|
||||||
for (int i = 0; i < num_images; ++i)
|
|
||||||
is_focals_estimated_ = is_focals_estimated_ && is_focal_estimated[i];
|
|
||||||
|
|
||||||
// Find focal median and use it as true focal length
|
|
||||||
nth_element(focals.begin(), focals.end(), focals.begin() + focals.size() / 2);
|
|
||||||
cameras.resize(num_images);
|
cameras.resize(num_images);
|
||||||
for (int i = 0; i < num_images; ++i)
|
for (int i = 0; i < num_images; ++i)
|
||||||
cameras[i].focal = focals[focals.size() / 2];
|
cameras[i].focal = focal;
|
||||||
|
|
||||||
// Restore global motion
|
// Restore global motion
|
||||||
Graph span_tree;
|
Graph span_tree;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user