|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "../precomp.hpp"
|
|
|
#include "../usac.hpp"
|
|
|
#include "opencv2/flann/miniflann.hpp"
|
|
|
#include <map>
|
|
|
|
|
|
namespace cv { namespace usac {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SolvePoly : public SolverPoly {
|
|
|
private:
|
|
|
static int sgn(double val) {
|
|
|
return (double(0) < val) - (val < double(0));
|
|
|
}
|
|
|
class Poly {
|
|
|
public:
|
|
|
Poly () = default;
|
|
|
Poly (const std::vector<double> &coef_) {
|
|
|
coef = coef_;
|
|
|
checkDegree();
|
|
|
}
|
|
|
Poly (const Poly &p) { coef = p.coef; }
|
|
|
|
|
|
|
|
|
std::vector<double> coef = {0};
|
|
|
inline int degree() const { return (int)coef.size()-1; }
|
|
|
void multiplyScalar (double s) {
|
|
|
|
|
|
if (fabs(s) < DBL_EPSILON) {
|
|
|
coef = {0};
|
|
|
return;
|
|
|
}
|
|
|
for (double &c : coef) c *= s;
|
|
|
}
|
|
|
void checkDegree() {
|
|
|
int deg = degree();
|
|
|
|
|
|
while (fabs(coef[deg]) < DBL_EPSILON) {
|
|
|
coef.pop_back();
|
|
|
if (--deg == 0)
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
double eval (double x) const {
|
|
|
|
|
|
const int d = degree();
|
|
|
double y = coef[d];
|
|
|
for (int i = d; i >= 1; i--)
|
|
|
y = coef[i-1] + x * y;
|
|
|
return y;
|
|
|
}
|
|
|
|
|
|
std::pair<int,int> signsAtInf () const {
|
|
|
|
|
|
const int d = degree();
|
|
|
const int s = sgn(coef[d]);
|
|
|
|
|
|
return std::make_pair(s, d % 2 == 0 ? s : -s);
|
|
|
}
|
|
|
Poly derivative () const {
|
|
|
Poly deriv;
|
|
|
if (degree() == 0)
|
|
|
return deriv;
|
|
|
|
|
|
deriv.coef = std::vector<double>(coef.size()-1);
|
|
|
for (int i = degree(); i > 0; i--)
|
|
|
|
|
|
deriv.coef[i-1] = i * coef[i];
|
|
|
return deriv;
|
|
|
}
|
|
|
void copyFrom (const Poly &p) { coef = p.coef; }
|
|
|
};
|
|
|
|
|
|
static void dividePoly (const Poly &p1, const Poly &p2, Poly &remainder) {
|
|
|
remainder.copyFrom(p1);
|
|
|
int p2_degree = p2.degree(), remainder_degree = remainder.degree();
|
|
|
if (p1.degree() < p2_degree)
|
|
|
return;
|
|
|
if (p2_degree == 0) {
|
|
|
remainder.multiplyScalar(1/p2.coef[0]);
|
|
|
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
const double p2_term = 1/p2.coef[p2_degree];
|
|
|
while (remainder_degree >= p2_degree) {
|
|
|
const double temp = remainder.coef[remainder_degree] * p2_term;
|
|
|
|
|
|
|
|
|
for (int i = p2_degree, j = remainder_degree; i >= 0; i--, j--)
|
|
|
remainder.coef[j] -= temp * p2.coef[i];
|
|
|
remainder.checkDegree();
|
|
|
remainder_degree = remainder.degree();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
constexpr static int REGULA_FALSI_MAX_ITERS = 500, MAX_POWER = 10, MAX_LEVEL = 200;
|
|
|
constexpr static double TOLERANCE = 1e-10, DIFF_TOLERANCE = 1e-7;
|
|
|
|
|
|
static bool findRootRegulaFalsi (const Poly &poly, double min, double max, double &root) {
|
|
|
double f_min = poly.eval(min), f_max = poly.eval(max);
|
|
|
if (f_min * f_max > 0 || min > max) {
|
|
|
return false;
|
|
|
}
|
|
|
int sign = 0, iter = 0;
|
|
|
for (; iter < REGULA_FALSI_MAX_ITERS; iter++) {
|
|
|
root = (f_min * max - f_max * min) / (f_min - f_max);
|
|
|
const double f_root = poly.eval(root);
|
|
|
if (fabs(f_root) < TOLERANCE || fabs(min - max) < DIFF_TOLERANCE) {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
if (f_root * f_max > 0) {
|
|
|
max = root; f_max = f_root;
|
|
|
if (sign == -1)
|
|
|
f_min *= 0.5;
|
|
|
sign = -1;
|
|
|
} else if (f_min * f_root > 0) {
|
|
|
min = root; f_min = f_root;
|
|
|
if (sign == 1)
|
|
|
f_max *= 0.5;
|
|
|
sign = 1;
|
|
|
}
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
static int numberOfSignChanges (const std::vector<Poly> &sturm, double x) {
|
|
|
int prev_sign = 0, sign_changes = 0;
|
|
|
for (const auto &poly : sturm) {
|
|
|
const int s = sgn(poly.eval(x));
|
|
|
if (s != 0 && prev_sign != 0 && s != prev_sign)
|
|
|
sign_changes++;
|
|
|
prev_sign = s;
|
|
|
}
|
|
|
return sign_changes;
|
|
|
}
|
|
|
|
|
|
static void findRootsRecursive (const Poly &poly, const std::vector<Poly> &sturm, double min, double max,
|
|
|
int sign_changes_at_min, int sign_changes_at_max, std::vector<double> &roots, int level) {
|
|
|
const int num_roots = sign_changes_at_min - sign_changes_at_max;
|
|
|
if (level == MAX_LEVEL) {
|
|
|
|
|
|
const double mid = (min + max) * 0.5;
|
|
|
if (fabs(poly.eval(mid)) < DBL_EPSILON) {
|
|
|
roots.emplace_back(mid);
|
|
|
}
|
|
|
} else if (num_roots == 1) {
|
|
|
double root;
|
|
|
if (findRootRegulaFalsi(poly, min, max, root)) {
|
|
|
roots.emplace_back(root);
|
|
|
}
|
|
|
} else if (num_roots > 1) {
|
|
|
const double mid = (min + max) * 0.5;
|
|
|
const int sign_changes_at_mid = numberOfSignChanges(sturm, mid);
|
|
|
|
|
|
if (sign_changes_at_min - sign_changes_at_mid > 0)
|
|
|
findRootsRecursive(poly, sturm, min, mid, sign_changes_at_min, sign_changes_at_mid, roots, level+1);
|
|
|
if (sign_changes_at_mid - sign_changes_at_max > 0)
|
|
|
findRootsRecursive(poly, sturm, mid, max, sign_changes_at_mid, sign_changes_at_max, roots, level+1);
|
|
|
}
|
|
|
}
|
|
|
public:
|
|
|
int getRealRoots (const std::vector<double> &coeffs, std::vector<double> &real_roots) override {
|
|
|
if (coeffs.empty())
|
|
|
return 0;
|
|
|
for (auto c : coeffs)
|
|
|
if (cvIsNaN(c) || cvIsInf(c))
|
|
|
return 0;
|
|
|
Poly input(coeffs);
|
|
|
if (input.degree() < 1)
|
|
|
return 0;
|
|
|
|
|
|
const Poly input_der = input.derivative();
|
|
|
|
|
|
Poly p (input), q (input_der), remainder;
|
|
|
std::vector<std::pair<int,int>> signs_at_inf; signs_at_inf.reserve(p.degree());
|
|
|
signs_at_inf.emplace_back(p.signsAtInf());
|
|
|
signs_at_inf.emplace_back(q.signsAtInf());
|
|
|
std::vector<Poly> sturm_sequence; sturm_sequence.reserve(input.degree());
|
|
|
sturm_sequence.emplace_back(input);
|
|
|
sturm_sequence.emplace_back(input_der);
|
|
|
while (q.degree() > 0) {
|
|
|
dividePoly(p, q, remainder);
|
|
|
remainder.multiplyScalar(-1);
|
|
|
p.copyFrom(q);
|
|
|
q.copyFrom(remainder);
|
|
|
sturm_sequence.emplace_back(remainder);
|
|
|
signs_at_inf.emplace_back(remainder.signsAtInf());
|
|
|
}
|
|
|
|
|
|
int num_sign_changes_at_pos_inf = 0, num_sign_changes_at_neg_inf = 0;
|
|
|
int prev_sign_pos_inf = signs_at_inf[0].first, prev_sign_neg_inf = signs_at_inf[0].second;
|
|
|
for (int i = 1; i < (int)signs_at_inf.size(); i++) {
|
|
|
const auto s_pos_inf = signs_at_inf[i].first, s_neg_inf = signs_at_inf[i].second;
|
|
|
|
|
|
if (s_pos_inf != 0) {
|
|
|
if (prev_sign_pos_inf != 0 && prev_sign_pos_inf != s_pos_inf)
|
|
|
num_sign_changes_at_pos_inf++;
|
|
|
prev_sign_pos_inf = s_pos_inf;
|
|
|
}
|
|
|
if (s_neg_inf != 0) {
|
|
|
if (prev_sign_neg_inf != 0 && prev_sign_neg_inf != s_neg_inf)
|
|
|
num_sign_changes_at_neg_inf++;
|
|
|
prev_sign_neg_inf = s_neg_inf;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
double root_neg_bound = -0.01, root_pos_bound = 0.01;
|
|
|
int num_sign_changes_min_x = -1, num_sign_changes_pos_x = -1;
|
|
|
for (int i = 0; i < MAX_POWER; i++) {
|
|
|
if (num_sign_changes_min_x != num_sign_changes_at_neg_inf) {
|
|
|
root_neg_bound *= 10;
|
|
|
num_sign_changes_min_x = numberOfSignChanges(sturm_sequence, root_neg_bound);
|
|
|
}
|
|
|
if (num_sign_changes_pos_x != num_sign_changes_at_pos_inf) {
|
|
|
root_pos_bound *= 10;
|
|
|
num_sign_changes_pos_x = numberOfSignChanges(sturm_sequence, root_pos_bound);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
real_roots.clear();
|
|
|
findRootsRecursive(input, sturm_sequence, root_neg_bound, root_pos_bound, num_sign_changes_min_x, num_sign_changes_pos_x, real_roots, 0 );
|
|
|
|
|
|
if ((int)real_roots.size() > input.degree())
|
|
|
real_roots.resize(input.degree());
|
|
|
return (int) real_roots.size();
|
|
|
}
|
|
|
};
|
|
|
Ptr<SolverPoly> SolverPoly::create() {
|
|
|
return makePtr<SolvePoly>();
|
|
|
}
|
|
|
|
|
|
double Utils::getCalibratedThreshold (double threshold, const Mat &K1, const Mat &K2) {
|
|
|
const auto * const k1 = (double *) K1.data, * const k2 = (double *) K2.data;
|
|
|
return threshold / ((k1[0] + k1[4] + k2[0] + k2[4]) / 4.0);
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Utils::calibratePoints (const Mat &K1, const Mat &K2, const Mat &points, Mat &calib_points) {
|
|
|
const auto * const points_ = (float *) points.data;
|
|
|
const auto * const k1 = (double *) K1.data;
|
|
|
const auto inv1_k11 = float(1 / k1[0]);
|
|
|
const auto inv1_k12 = float(-k1[1] / (k1[0]*k1[4]));
|
|
|
|
|
|
const auto inv1_k13 = float((-k1[2]*k1[4] + k1[1]*k1[5]) / (k1[0]*k1[4]));
|
|
|
const auto inv1_k22 = float(1 / k1[4]);
|
|
|
const auto inv1_k23 = float(-k1[5] / k1[4]);
|
|
|
|
|
|
const auto * const k2 = (double *) K2.data;
|
|
|
const auto inv2_k11 = float(1 / k2[0]);
|
|
|
const auto inv2_k12 = float(-k2[1] / (k2[0]*k2[4]));
|
|
|
const auto inv2_k13 = float((-k2[2]*k2[4] + k2[1]*k2[5]) / (k2[0]*k2[4]));
|
|
|
const auto inv2_k22 = float(1 / k2[4]);
|
|
|
const auto inv2_k23 = float(-k2[5] / k2[4]);
|
|
|
|
|
|
calib_points = Mat ( points.rows, 4, points.type());
|
|
|
auto * calib_points_ = (float *) calib_points.data;
|
|
|
|
|
|
for (int i = 0; i < points.rows; i++) {
|
|
|
const int idx = 4*i;
|
|
|
(*calib_points_++) = inv1_k11 * points_[idx ] + inv1_k12 * points_[idx+1] + inv1_k13;
|
|
|
(*calib_points_++) = inv1_k22 * points_[idx+1] + inv1_k23;
|
|
|
(*calib_points_++) = inv2_k11 * points_[idx+2] + inv2_k12 * points_[idx+3] + inv2_k13;
|
|
|
(*calib_points_++) = inv2_k22 * points_[idx+3] + inv2_k23;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Utils::calibrateAndNormalizePointsPnP (const Mat &K, const Mat &pts, Mat &calib_norm_pts) {
|
|
|
const auto * const points = (float *) pts.data;
|
|
|
const auto * const k = (double *) K.data;
|
|
|
const auto inv_k11 = float(1 / k[0]);
|
|
|
const auto inv_k12 = float(-k[1] / (k[0]*k[4]));
|
|
|
const auto inv_k13 = float((-k[2]*k[4] + k[1]*k[5]) / (k[0]*k[4]));
|
|
|
const auto inv_k22 = float(1 / k[4]);
|
|
|
const auto inv_k23 = float(-k[5] / k[4]);
|
|
|
|
|
|
calib_norm_pts = Mat (pts.rows, 3, pts.type());
|
|
|
auto * calib_norm_pts_ = (float *) calib_norm_pts.data;
|
|
|
|
|
|
for (int i = 0; i < pts.rows; i++) {
|
|
|
const int idx = 5 * i;
|
|
|
const float k_inv_u = inv_k11 * points[idx] + inv_k12 * points[idx+1] + inv_k13;
|
|
|
const float k_inv_v = inv_k22 * points[idx+1] + inv_k23;
|
|
|
const float norm = 1.f / sqrtf(k_inv_u*k_inv_u + k_inv_v*k_inv_v + 1);
|
|
|
(*calib_norm_pts_++) = k_inv_u * norm;
|
|
|
(*calib_norm_pts_++) = k_inv_v * norm;
|
|
|
(*calib_norm_pts_++) = norm;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void Utils::normalizeAndDecalibPointsPnP (const Mat &K_, Mat &pts, Mat &calib_norm_pts) {
|
|
|
const auto * const K = (double *) K_.data;
|
|
|
const auto k11 = (float)K[0], k12 = (float)K[1], k13 = (float)K[2],
|
|
|
k22 = (float)K[4], k23 = (float)K[5];
|
|
|
calib_norm_pts = Mat (pts.rows, 3, pts.type());
|
|
|
auto * points = (float *) pts.data;
|
|
|
auto * calib_norm_pts_ = (float *) calib_norm_pts.data;
|
|
|
|
|
|
for (int i = 0; i < pts.rows; i++) {
|
|
|
const int idx = 5 * i;
|
|
|
const float k_inv_u = points[idx ];
|
|
|
const float k_inv_v = points[idx+1];
|
|
|
const float norm = 1.f / sqrtf(k_inv_u*k_inv_u + k_inv_v*k_inv_v + 1);
|
|
|
(*calib_norm_pts_++) = k_inv_u * norm;
|
|
|
(*calib_norm_pts_++) = k_inv_v * norm;
|
|
|
(*calib_norm_pts_++) = norm;
|
|
|
points[idx ] = k11 * k_inv_u + k12 * k_inv_v + k13;
|
|
|
points[idx+1] = k22 * k_inv_v + k23;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Utils::decomposeProjection (const Mat &P, Matx33d &K, Matx33d &R, Vec3d &t, bool same_focal) {
|
|
|
const Matx33d M = P.colRange(0,3);
|
|
|
double scale = norm(M.row(2)); scale *= scale;
|
|
|
K = Matx33d::eye();
|
|
|
K(1,2) = M.row(1).dot(M.row(2)) / scale;
|
|
|
K(0,2) = M.row(0).dot(M.row(2)) / scale;
|
|
|
K(1,1) = sqrt(M.row(1).dot(M.row(1)) / scale - K(1,2)*K(1,2));
|
|
|
K(0,0) = sqrt(M.row(0).dot(M.row(0)) / scale - K(0,2)*K(0,2));
|
|
|
if (same_focal)
|
|
|
K(0,0) = K(1,1) = (K(0,0) + K(1,1)) / 2;
|
|
|
R = K.inv() * M / sqrt(scale);
|
|
|
if (determinant(M) < 0) R *= -1;
|
|
|
t = R * M.inv() * Vec3d(P.col(3));
|
|
|
}
|
|
|
|
|
|
double Utils::getPoissonCDF (double lambda, int inliers) {
|
|
|
double exp_lambda = exp(-lambda), cdf = exp_lambda, lambda_i_div_fact_i = 1;
|
|
|
for (int i = 1; i <= inliers; i++) {
|
|
|
lambda_i_div_fact_i *= (lambda / i);
|
|
|
cdf += exp_lambda * lambda_i_div_fact_i;
|
|
|
if (fabs(cdf - 1) < DBL_EPSILON)
|
|
|
break;
|
|
|
}
|
|
|
return cdf;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Vec3d Utils::getLeftEpipole (const Mat &F) {
|
|
|
Vec3d _e = F.col(0).cross(F.col(2));
|
|
|
const auto * const e = _e.val;
|
|
|
if (e[0] <= DBL_EPSILON && e[0] > -DBL_EPSILON &&
|
|
|
e[1] <= DBL_EPSILON && e[1] > -DBL_EPSILON &&
|
|
|
e[2] <= DBL_EPSILON && e[2] > -DBL_EPSILON)
|
|
|
_e = Vec3d(Mat(F.col(1))).cross(F.col(2));
|
|
|
return _e;
|
|
|
}
|
|
|
Vec3d Utils::getRightEpipole (const Mat &F) {
|
|
|
Vec3d _e = F.row(0).cross(F.row(2));
|
|
|
const auto * const e = _e.val;
|
|
|
if (e[0] <= DBL_EPSILON && e[0] > -DBL_EPSILON &&
|
|
|
e[1] <= DBL_EPSILON && e[1] > -DBL_EPSILON &&
|
|
|
e[2] <= DBL_EPSILON && e[2] > -DBL_EPSILON)
|
|
|
_e = F.row(1).cross(F.row(2));
|
|
|
return _e;
|
|
|
}
|
|
|
|
|
|
void Utils::densitySort (const Mat &points, int knn, Mat &sorted_points, std::vector<int> &sorted_mask) {
|
|
|
|
|
|
const int points_size = points.rows, dim = points.cols;
|
|
|
sorted_mask = std::vector<int >(points_size);
|
|
|
for (int i = 0; i < points_size; i++)
|
|
|
sorted_mask[i] = i;
|
|
|
|
|
|
|
|
|
FlannNeighborhoodGraph &graph = *FlannNeighborhoodGraph::create(points, points_size, knn,
|
|
|
true , 6, 1);
|
|
|
|
|
|
std::vector<double> sum_knn_distances (points_size, 0);
|
|
|
for (int p = 0; p < points_size; p++) {
|
|
|
const std::vector<double> &dists = graph.getNeighborsDistances(p);
|
|
|
for (int k = 0; k < knn; k++)
|
|
|
sum_knn_distances[p] += dists[k];
|
|
|
}
|
|
|
|
|
|
|
|
|
std::sort(sorted_mask.begin(), sorted_mask.end(), [&](int a, int b) {
|
|
|
return sum_knn_distances[a] < sum_knn_distances[b];
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sorted_points = Mat(points_size, dim, points.type());
|
|
|
const auto * const points_ptr = (float *) points.data;
|
|
|
auto * spoints_ptr = (float *) sorted_points.data;
|
|
|
for (int i = 0; i < points_size; i++) {
|
|
|
const int pt2 = sorted_mask[i] * dim;
|
|
|
for (int j = 0; j < dim; j++)
|
|
|
(*spoints_ptr++) = points_ptr[pt2+j];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
Matx33d Math::getSkewSymmetric(const Vec3d &v) {
|
|
|
return {0, -v[2], v[1],
|
|
|
v[2], 0, -v[0],
|
|
|
-v[1], v[0], 0};
|
|
|
}
|
|
|
|
|
|
Matx33d Math::rotVec2RotMat (const Vec3d &v) {
|
|
|
const double phi = sqrt(v[0]*v[0]+v[1]*v[1]+v[2]*v[2]);
|
|
|
const double x = v[0] / phi, y = v[1] / phi, z = v[2] / phi;
|
|
|
const double a = sin(phi), b = cos(phi);
|
|
|
|
|
|
return {(b - 1)*y*y + (b - 1)*z*z + 1, -a*z - x*y*(b - 1), a*y - x*z*(b - 1),
|
|
|
a*z - x*y*(b - 1), (b - 1)*x*x + (b - 1)*z*z + 1, -a*x - y*z*(b - 1),
|
|
|
-a*y - x*z*(b - 1), a*x - y*z*(b - 1), (b - 1)*x*x + (b - 1)*y*y + 1};
|
|
|
}
|
|
|
|
|
|
Vec3d Math::rotMat2RotVec (const Matx33d &R) {
|
|
|
|
|
|
Vec3d rot_vec;
|
|
|
const double trace = R(0,0)+R(1,1)+R(2,2);
|
|
|
if (trace >= 3 - FLT_EPSILON) {
|
|
|
rot_vec = (0.5 * (trace-3)/12)*Vec3d(R(2,1)-R(1,2),
|
|
|
R(0,2)-R(2,0),
|
|
|
R(1,0)-R(0,1));
|
|
|
} else if (3 - FLT_EPSILON > trace && trace > -1 + FLT_EPSILON) {
|
|
|
double theta = acos((trace - 1) / 2);
|
|
|
rot_vec = (theta / (2 * sin(theta))) * Vec3d(R(2,1)-R(1,2),
|
|
|
R(0,2)-R(2,0),
|
|
|
R(1,0)-R(0,1));
|
|
|
} else {
|
|
|
int a;
|
|
|
if (R(0,0) > R(1,1))
|
|
|
a = R(0,0) > R(2,2) ? 0 : 2;
|
|
|
else
|
|
|
a = R(1,1) > R(2,2) ? 1 : 2;
|
|
|
Vec3d v;
|
|
|
int b = (a + 1) % 3, c = (a + 2) % 3;
|
|
|
double s = sqrt(R(a,a) - R(b,b) - R(c,c) + 1);
|
|
|
v[a] = s / 2;
|
|
|
v[b] = (R(b,a) + R(a,b)) / (2 * s);
|
|
|
v[c] = (R(c,a) + R(a,c)) / (2 * s);
|
|
|
rot_vec = M_PI * v / norm(v);
|
|
|
}
|
|
|
return rot_vec;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool Math::eliminateUpperTriangular (std::vector<double> &a, int m, int n) {
|
|
|
for (int r = 0; r < m; r++){
|
|
|
double pivot = a[r*n+r];
|
|
|
int row_with_pivot = r;
|
|
|
|
|
|
|
|
|
for (int k = r+1; k < m; k++)
|
|
|
if (fabs(pivot) < fabs(a[k*n+r])) {
|
|
|
pivot = a[k*n+r];
|
|
|
row_with_pivot = k;
|
|
|
}
|
|
|
|
|
|
|
|
|
if (fabs(pivot) < DBL_EPSILON)
|
|
|
continue;
|
|
|
|
|
|
|
|
|
for (int c = r; c < n; c++)
|
|
|
std::swap(a[row_with_pivot*n+c], a[r*n+c]);
|
|
|
|
|
|
|
|
|
for (int j = r+1; j < m; j++){
|
|
|
const int row_idx1 = j*n, row_idx2 = r*n;
|
|
|
const auto fac = a[row_idx1+r] / pivot;
|
|
|
a[row_idx1+r] = 0;
|
|
|
for (int c = r+1; c < n; c++)
|
|
|
a[row_idx1+c] -= fac * a[row_idx2+c];
|
|
|
}
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
double Utils::intersectionOverUnion (const std::vector<bool> &a, const std::vector<bool> &b) {
|
|
|
int intersects = 0, unions = 0;
|
|
|
for (int i = 0; i < (int)a.size(); i++)
|
|
|
if (a[i] || b[i]) {
|
|
|
unions++;
|
|
|
if (a[i] && b[i])
|
|
|
intersects++;
|
|
|
}
|
|
|
if (unions == 0) return 0.0;
|
|
|
return (double) intersects / unions;
|
|
|
}
|
|
|
|
|
|
|
|
|
class UniformRandomGeneratorImpl : public UniformRandomGenerator {
|
|
|
private:
|
|
|
int subset_size = 0, max_range = 0;
|
|
|
std::vector<int> subset;
|
|
|
RNG rng;
|
|
|
public:
|
|
|
explicit UniformRandomGeneratorImpl (int state) : rng(state) {}
|
|
|
|
|
|
|
|
|
UniformRandomGeneratorImpl (int state, int max_range_, int subset_size_) : rng(state) {
|
|
|
subset_size = subset_size_;
|
|
|
max_range = max_range_;
|
|
|
subset = std::vector<int>(subset_size_);
|
|
|
}
|
|
|
int getRandomNumber () override {
|
|
|
return rng.uniform(0, max_range);
|
|
|
}
|
|
|
int getRandomNumber (int max_rng) override {
|
|
|
return rng.uniform(0, max_rng);
|
|
|
}
|
|
|
|
|
|
void resetGenerator (int max_range_) override {
|
|
|
CV_CheckGE(0, max_range_, "max range must be greater than 0");
|
|
|
max_range = max_range_;
|
|
|
}
|
|
|
|
|
|
void generateUniqueRandomSet (std::vector<int>& sample) override {
|
|
|
CV_CheckLE(subset_size, max_range, "RandomGenerator. Subset size must be LE than range!");
|
|
|
int j, num;
|
|
|
sample[0] = rng.uniform(0, max_range);
|
|
|
for (int i = 1; i < subset_size;) {
|
|
|
num = rng.uniform(0, max_range);
|
|
|
|
|
|
for (j = i - 1; j >= 0; j--)
|
|
|
if (num == sample[j])
|
|
|
|
|
|
break;
|
|
|
|
|
|
if (j == -1) sample[i++] = num;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
void generateUniqueRandomSet (std::vector<int>& sample, int max_range_) override {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CV_CheckLE(subset_size, max_range_, "RandomGenerator. Subset size must be LE than range!");
|
|
|
int num, j;
|
|
|
sample[0] = rng.uniform(0, max_range_);
|
|
|
for (int i = 1; i < subset_size;) {
|
|
|
num = rng.uniform(0, max_range_);
|
|
|
for (j = i - 1; j >= 0; j--)
|
|
|
if (num == sample[j])
|
|
|
break;
|
|
|
if (j == -1) sample[i++] = num;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
void generateUniqueRandomSet (std::vector<int>& sample, int subset_size_, int max_range_) override {
|
|
|
CV_CheckLE(subset_size_, max_range_, "RandomGenerator. Subset size must be LE than range!");
|
|
|
int num, j;
|
|
|
sample[0] = rng.uniform(0, max_range_);
|
|
|
for (int i = 1; i < subset_size_;) {
|
|
|
num = rng.uniform(0, max_range_);
|
|
|
for (j = i - 1; j >= 0; j--)
|
|
|
if (num == sample[j])
|
|
|
break;
|
|
|
if (j == -1) sample[i++] = num;
|
|
|
}
|
|
|
}
|
|
|
const std::vector<int> &generateUniqueRandomSubset (std::vector<int> &array1, int size1) override {
|
|
|
CV_CheckLE(subset_size, size1, "RandomGenerator. Subset size must be LE than range!");
|
|
|
int temp_size1 = size1;
|
|
|
for (int i = 0; i < subset_size; i++) {
|
|
|
const int idx1 = rng.uniform(0, temp_size1);
|
|
|
subset[i] = array1[idx1];
|
|
|
std::swap(array1[idx1], array1[--temp_size1]);
|
|
|
}
|
|
|
return subset;
|
|
|
}
|
|
|
void setSubsetSize (int subset_size_) override {
|
|
|
if (subset_size < subset_size_)
|
|
|
subset.resize(subset_size_);
|
|
|
subset_size = subset_size_;
|
|
|
}
|
|
|
int getSubsetSize () const override { return subset_size; }
|
|
|
};
|
|
|
|
|
|
Ptr<UniformRandomGenerator> UniformRandomGenerator::create (int state) {
|
|
|
return makePtr<UniformRandomGeneratorImpl>(state);
|
|
|
}
|
|
|
Ptr<UniformRandomGenerator> UniformRandomGenerator::create
|
|
|
(int state, int max_range, int subset_size_) {
|
|
|
return makePtr<UniformRandomGeneratorImpl>(state, max_range, subset_size_);
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
float quicksort_median (std::vector<float> &array, int k_minth, int left, int right);
|
|
|
float quicksort_median (std::vector<float> &array, int k_minth, int left, int right) {
|
|
|
|
|
|
if (right - left == 0) return array[left];
|
|
|
|
|
|
|
|
|
const auto pivot = array[right];
|
|
|
int right_ = right - 1;
|
|
|
|
|
|
int j = left, values_less_eq_pivot = 1;
|
|
|
for (; j <= right_;) {
|
|
|
if (array[j] <= pivot) {
|
|
|
j++;
|
|
|
values_less_eq_pivot++;
|
|
|
} else
|
|
|
|
|
|
|
|
|
std::swap(array[j], array[right_--]);
|
|
|
}
|
|
|
if (values_less_eq_pivot == k_minth) return pivot;
|
|
|
if (k_minth > values_less_eq_pivot)
|
|
|
return quicksort_median(array, k_minth - values_less_eq_pivot, j, right-1);
|
|
|
else
|
|
|
return quicksort_median(array, k_minth, left, j-1);
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
float Utils::findMedian (std::vector<float> &array) {
|
|
|
const int length = static_cast<int>(array.size());
|
|
|
if (length % 2) {
|
|
|
|
|
|
return quicksort_median (array, length/2+1, 0, length-1);
|
|
|
} else {
|
|
|
|
|
|
return (quicksort_median(array, length/2 , 0, length-1) +
|
|
|
quicksort_median(array, length/2+1, 0, length-1))*.5f;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
class RadiusSearchNeighborhoodGraphImpl : public RadiusSearchNeighborhoodGraph {
|
|
|
private:
|
|
|
std::vector<std::vector<int>> graph;
|
|
|
public:
|
|
|
RadiusSearchNeighborhoodGraphImpl (const Mat &container_, int points_size,
|
|
|
double radius, int flann_search_params, int num_kd_trees) {
|
|
|
|
|
|
CV_Assert(container_.type() == CV_32F);
|
|
|
|
|
|
FlannBasedMatcher flann(makePtr<flann::KDTreeIndexParams>(num_kd_trees), makePtr<flann::SearchParams>(flann_search_params));
|
|
|
std::vector<std::vector<DMatch>> neighbours;
|
|
|
flann.radiusMatch(container_, container_, neighbours, (float)radius);
|
|
|
|
|
|
|
|
|
graph = std::vector<std::vector<int>> (points_size);
|
|
|
|
|
|
int pt = 0;
|
|
|
for (const auto &n : neighbours) {
|
|
|
if (n.size() <= 1)
|
|
|
continue;
|
|
|
auto &graph_row = graph[pt];
|
|
|
graph_row = std::vector<int>(n.size()-1);
|
|
|
int j = 0;
|
|
|
for (const auto &idx : n)
|
|
|
|
|
|
if (idx.trainIdx != pt)
|
|
|
graph_row[j++] = idx.trainIdx;
|
|
|
pt++;
|
|
|
}
|
|
|
}
|
|
|
const std::vector<std::vector<int>> &getGraph () const override { return graph; }
|
|
|
inline const std::vector<int> &getNeighbors(int point_idx) const override {
|
|
|
return graph[point_idx];
|
|
|
}
|
|
|
};
|
|
|
Ptr<RadiusSearchNeighborhoodGraph> RadiusSearchNeighborhoodGraph::create (const Mat &points,
|
|
|
int points_size, double radius_, int flann_search_params, int num_kd_trees) {
|
|
|
return makePtr<RadiusSearchNeighborhoodGraphImpl> (points, points_size, radius_,
|
|
|
flann_search_params, num_kd_trees);
|
|
|
}
|
|
|
|
|
|
|
|
|
class FlannNeighborhoodGraphImpl : public FlannNeighborhoodGraph {
|
|
|
private:
|
|
|
std::vector<std::vector<int>> graph;
|
|
|
std::vector<std::vector<double>> distances;
|
|
|
public:
|
|
|
FlannNeighborhoodGraphImpl (const Mat &container_, int points_size, int k_nearest_neighbors,
|
|
|
bool get_distances, int flann_search_params_, int num_kd_trees) {
|
|
|
CV_Assert(k_nearest_neighbors <= points_size);
|
|
|
|
|
|
CV_Assert(container_.type() == CV_32F);
|
|
|
|
|
|
flann::Index flannIndex (container_.reshape(1), flann::KDTreeIndexParams(num_kd_trees));
|
|
|
Mat dists, nearest_neighbors;
|
|
|
|
|
|
flannIndex.knnSearch(container_, nearest_neighbors, dists, k_nearest_neighbors+1,
|
|
|
flann::SearchParams(flann_search_params_));
|
|
|
|
|
|
|
|
|
|
|
|
nearest_neighbors.colRange(1, k_nearest_neighbors+1).copyTo (nearest_neighbors);
|
|
|
|
|
|
graph = std::vector<std::vector<int>>(points_size, std::vector<int>(k_nearest_neighbors));
|
|
|
const auto * const nn = (int *) nearest_neighbors.data;
|
|
|
const auto * const dists_ptr = (float *) dists.data;
|
|
|
|
|
|
if (get_distances)
|
|
|
distances = std::vector<std::vector<double>>(points_size, std::vector<double>(k_nearest_neighbors));
|
|
|
|
|
|
for (int pt = 0; pt < points_size; pt++) {
|
|
|
std::copy(nn + k_nearest_neighbors*pt, nn + k_nearest_neighbors*pt + k_nearest_neighbors, &graph[pt][0]);
|
|
|
if (get_distances)
|
|
|
std::copy(dists_ptr + k_nearest_neighbors*pt, dists_ptr + k_nearest_neighbors*pt + k_nearest_neighbors,
|
|
|
&distances[pt][0]);
|
|
|
}
|
|
|
}
|
|
|
const std::vector<double>& getNeighborsDistances (int idx) const override {
|
|
|
return distances[idx];
|
|
|
}
|
|
|
const std::vector<std::vector<int>> &getGraph () const override { return graph; }
|
|
|
inline const std::vector<int> &getNeighbors(int point_idx) const override {
|
|
|
|
|
|
return graph[point_idx];
|
|
|
}
|
|
|
};
|
|
|
|
|
|
Ptr<FlannNeighborhoodGraph> FlannNeighborhoodGraph::create(const Mat &points,
|
|
|
int points_size, int k_nearest_neighbors_, bool get_distances,
|
|
|
int flann_search_params_, int num_kd_trees) {
|
|
|
return makePtr<FlannNeighborhoodGraphImpl>(points, points_size,
|
|
|
k_nearest_neighbors_, get_distances, flann_search_params_, num_kd_trees);
|
|
|
}
|
|
|
|
|
|
|
|
|
class GridNeighborhoodGraphImpl : public GridNeighborhoodGraph {
|
|
|
private:
|
|
|
|
|
|
struct CellCoord {
|
|
|
int c1x, c1y, c2x, c2y;
|
|
|
CellCoord (int c1x_, int c1y_, int c2x_, int c2y_) {
|
|
|
c1x = c1x_; c1y = c1y_; c2x = c2x_; c2y = c2y_;
|
|
|
}
|
|
|
bool operator==(const CellCoord &o) const {
|
|
|
return c1x == o.c1x && c1y == o.c1y && c2x == o.c2x && c2y == o.c2y;
|
|
|
}
|
|
|
bool operator<(const CellCoord &o) const {
|
|
|
if (c1x < o.c1x) return true;
|
|
|
if (c1x == o.c1x && c1y < o.c1y) return true;
|
|
|
if (c1x == o.c1x && c1y == o.c1y && c2x < o.c2x) return true;
|
|
|
return c1x == o.c1x && c1y == o.c1y && c2x == o.c2x && c2y < o.c2y;
|
|
|
}
|
|
|
};
|
|
|
|
|
|
std::vector<std::vector<int>> graph;
|
|
|
public:
|
|
|
GridNeighborhoodGraphImpl (const Mat &container_, int points_size,
|
|
|
int cell_size_x_img1, int cell_size_y_img1, int cell_size_x_img2, int cell_size_y_img2,
|
|
|
int max_neighbors) {
|
|
|
|
|
|
std::map<CellCoord, std::vector<int >> neighbors_map;
|
|
|
const auto * const container = (float *) container_.data;
|
|
|
|
|
|
|
|
|
|
|
|
const float cell_sz_x1 = 1.f / (float) cell_size_x_img1,
|
|
|
cell_sz_y1 = 1.f / (float) cell_size_y_img1,
|
|
|
cell_sz_x2 = 1.f / (float) cell_size_x_img2,
|
|
|
cell_sz_y2 = 1.f / (float) cell_size_y_img2;
|
|
|
const int dimension = container_.cols;
|
|
|
for (int i = 0; i < points_size; i++) {
|
|
|
const int idx = dimension * i;
|
|
|
neighbors_map[CellCoord((int)(container[idx ] * cell_sz_x1),
|
|
|
(int)(container[idx+1] * cell_sz_y1),
|
|
|
(int)(container[idx+2] * cell_sz_x2),
|
|
|
(int)(container[idx+3] * cell_sz_y2))].emplace_back(i);
|
|
|
}
|
|
|
|
|
|
|
|
|
graph = std::vector<std::vector<int>>(points_size);
|
|
|
|
|
|
|
|
|
for (const auto &cell : neighbors_map) {
|
|
|
const int neighbors_in_cell = static_cast<int>(cell.second.size());
|
|
|
|
|
|
if (neighbors_in_cell < 2) continue;
|
|
|
|
|
|
const std::vector<int> &neighbors = cell.second;
|
|
|
|
|
|
for (int v_in_cell : neighbors) {
|
|
|
|
|
|
auto &graph_row = graph[v_in_cell];
|
|
|
graph_row = std::vector<int>(std::min(max_neighbors, neighbors_in_cell-1));
|
|
|
int j = 0;
|
|
|
for (int n : neighbors)
|
|
|
if (n != v_in_cell){
|
|
|
graph_row[j++] = n;
|
|
|
if (j >= max_neighbors)
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
const std::vector<std::vector<int>> &getGraph () const override { return graph; }
|
|
|
inline const std::vector<int> &getNeighbors(int point_idx) const override {
|
|
|
|
|
|
|
|
|
return graph[point_idx];
|
|
|
}
|
|
|
};
|
|
|
|
|
|
Ptr<GridNeighborhoodGraph> GridNeighborhoodGraph::create(const Mat &points,
|
|
|
int points_size, int cell_size_x_img1_, int cell_size_y_img1_,
|
|
|
int cell_size_x_img2_, int cell_size_y_img2_, int max_neighbors) {
|
|
|
return makePtr<GridNeighborhoodGraphImpl>(points, points_size,
|
|
|
cell_size_x_img1_, cell_size_y_img1_, cell_size_x_img2_, cell_size_y_img2_, max_neighbors);
|
|
|
}
|
|
|
}} |