Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
**/*_cl.h
.idea
build
/build*
cmake-build*
/.cache/
/.venv/
/.vscode
/data/debug/
147 changes: 96 additions & 51 deletions src/phg/matching/descriptor_matcher.cpp
Original file line number Diff line number Diff line change
@@ -1,27 +1,46 @@
#include "descriptor_matcher.h"

#include <opencv2/flann/miniflann.hpp>
#include "flann_factory.h"
#include <algorithm>
#include <opencv2/core/types.hpp>
#include <opencv2/flann/miniflann.hpp>
#include <set>

/* @TUNABLE */
static constexpr float RATIO_THRESHOLD = 0.75;
static constexpr int SEARCH_CHECKS = 10;

void phg::DescriptorMatcher::filterMatchesRatioTest(const std::vector<std::vector<cv::DMatch>> &matches,
std::vector<cv::DMatch> &filtered_matches)
void phg::DescriptorMatcher::filterMatchesRatioTest(const std::vector<std::vector<cv::DMatch>>& matches, std::vector<cv::DMatch>& filtered_matches)
{
filtered_matches.clear();

throw std::runtime_error("not implemented yet");
const cv::DMatch* heap[3];
auto cmp = [](const cv::DMatch* a, const cv::DMatch* b) { return a->distance < b->distance; };
for (auto& match_row : matches) {
int size = 0;
for (auto& match : match_row) {
heap[size++] = &match;
std::push_heap(heap, heap + size, cmp);
if (size == 3)
std::pop_heap(heap, heap + size--, cmp);
}
if (size == 2) {
std::pop_heap(heap, heap + size, cmp);
if (heap[0]->distance / heap[1]->distance < RATIO_THRESHOLD * RATIO_THRESHOLD) {
filtered_matches.push_back(*heap[0]);
}
} else if (size == 1) {
filtered_matches.push_back(*heap[0]);
}
}
}


void phg::DescriptorMatcher::filterMatchesClusters(const std::vector<cv::DMatch> &matches,
const std::vector<cv::KeyPoint> keypoints_query,
const std::vector<cv::KeyPoint> keypoints_train,
std::vector<cv::DMatch> &filtered_matches)
void phg::DescriptorMatcher::filterMatchesClusters(const std::vector<cv::DMatch>& matches, const std::vector<cv::KeyPoint> keypoints_query, const std::vector<cv::KeyPoint> keypoints_train, std::vector<cv::DMatch>& filtered_matches)
{
filtered_matches.clear();

const size_t total_neighbours = 5; // total number of neighbours to test (including candidate)
const size_t consistent_matches = 3; // minimum number of consistent matches (including candidate)
const float radius_limit_scale = 2.f; // limit search radius by scaled median
const size_t total_neighbours = 5; // total number of neighbours to test (including candidate)
const size_t consistent_matches = 3; // minimum number of consistent matches (including candidate)
const float radius_limit_scale = 2.f; // limit search radius by scaled median

const int n_matches = matches.size();

Expand All @@ -35,42 +54,68 @@ void phg::DescriptorMatcher::filterMatchesClusters(const std::vector<cv::DMatch>
points_query.at<cv::Point2f>(i) = keypoints_query[matches[i].queryIdx].pt;
points_train.at<cv::Point2f>(i) = keypoints_train[matches[i].trainIdx].pt;
}
//
// // размерность всего 2, так что точное KD-дерево
// std::shared_ptr<cv::flann::IndexParams> index_params = flannKdTreeIndexParams(TODO);
// std::shared_ptr<cv::flann::SearchParams> search_params = flannKsTreeSearchParams(TODO);
//
// std::shared_ptr<cv::flann::Index> index_query = flannKdTreeIndex(points_query, index_params);
// std::shared_ptr<cv::flann::Index> index_train = flannKdTreeIndex(points_train, index_params);
//
// // для каждой точки найти total neighbors ближайших соседей
// cv::Mat indices_query(n_matches, total_neighbours, CV_32SC1);
// cv::Mat distances2_query(n_matches, total_neighbours, CV_32FC1);
// cv::Mat indices_train(n_matches, total_neighbours, CV_32SC1);
// cv::Mat distances2_train(n_matches, total_neighbours, CV_32FC1);
//
// index_query->knnSearch(points_query, indices_query, distances2_query, total_neighbours, *search_params);
// index_train->knnSearch(points_train, indices_train, distances2_train, total_neighbours, *search_params);
//
// // оценить радиус поиска для каждой картинки
// // NB: radius2_query, radius2_train: квадраты радиуса!
// float radius2_query, radius2_train;
// {
// std::vector<double> max_dists2_query(n_matches);
// std::vector<double> max_dists2_train(n_matches);
// for (int i = 0; i < n_matches; ++i) {
// max_dists2_query[i] = distances2_query.at<float>(i, total_neighbours - 1);
// max_dists2_train[i] = distances2_train.at<float>(i, total_neighbours - 1);
// }
//
// int median_pos = n_matches / 2;
// std::nth_element(max_dists2_query.begin(), max_dists2_query.begin() + median_pos, max_dists2_query.end());
// std::nth_element(max_dists2_train.begin(), max_dists2_train.begin() + median_pos, max_dists2_train.end());
//
// radius2_query = max_dists2_query[median_pos] * radius_limit_scale * radius_limit_scale;
// radius2_train = max_dists2_train[median_pos] * radius_limit_scale * radius_limit_scale;
// }
//
// метч остается, если левое и правое множества первых total_neighbors соседей в радиусах поиска(radius2_query, radius2_train) имеют как минимум consistent_matches общих элементов
// // TODO заполнить filtered_matches

// размерность всего 2, так что точное KD-дерево
std::shared_ptr<cv::flann::IndexParams> index_params = flannKdTreeIndexParams(1);
std::shared_ptr<cv::flann::SearchParams> search_params = flannKsTreeSearchParams(SEARCH_CHECKS);

std::shared_ptr<cv::flann::Index> index_query = flannKdTreeIndex(points_query, index_params);
std::shared_ptr<cv::flann::Index> index_train = flannKdTreeIndex(points_train, index_params);

// для каждой точки найти total neighbors ближайших соседей
cv::Mat indices_query(n_matches, total_neighbours, CV_32SC1);
cv::Mat distances2_query(n_matches, total_neighbours, CV_32FC1);
cv::Mat indices_train(n_matches, total_neighbours, CV_32SC1);
cv::Mat distances2_train(n_matches, total_neighbours, CV_32FC1);

index_query->knnSearch(points_query, indices_query, distances2_query, total_neighbours, *search_params);
index_train->knnSearch(points_train, indices_train, distances2_train, total_neighbours, *search_params);

// оценить радиус поиска для каждой картинки
// NB: radius2_query, radius2_train: квадраты радиуса!
float radius2_query, radius2_train;
{
std::vector<double> max_dists2_query(n_matches);
std::vector<double> max_dists2_train(n_matches);
for (int i = 0; i < n_matches; ++i) {
max_dists2_query[i] = distances2_query.at<float>(i, total_neighbours - 1);
max_dists2_train[i] = distances2_train.at<float>(i, total_neighbours - 1);
}

int median_pos = n_matches / 2;
std::nth_element(max_dists2_query.begin(), max_dists2_query.begin() + median_pos, max_dists2_query.end());
std::nth_element(max_dists2_train.begin(), max_dists2_train.begin() + median_pos, max_dists2_train.end());

radius2_query = max_dists2_query[median_pos] * radius_limit_scale * radius_limit_scale;
radius2_train = max_dists2_train[median_pos] * radius_limit_scale * radius_limit_scale;
}

// метч остается, если левое и правое множества первых total_neighbors соседей в радиусах поиска(radius2_query, radius2_train) имеют как минимум consistent_matches общих элементов
// заполнить filtered_matches

for (int i = 0; i < n_matches; ++i) {
int n_consistent = 0;
for (int src = 0; src < total_neighbours; ++src) {
float dsrc = distances2_query.at<float>(i, src);
if (dsrc > radius2_query)
continue;

for (int dst = 0; dst < total_neighbours; ++dst) {
float ddst = distances2_train.at<float>(i, dst);
if (ddst > radius2_train)
continue;

if (indices_query.at<int>(i, src) == indices_train.at<int>(i, dst)) {
n_consistent += 1;

if (n_consistent >= consistent_matches)
goto early_success;
}
}
}
if (n_consistent >= consistent_matches) {
early_success:
filtered_matches.push_back(matches[i]);
}
}
}
21 changes: 18 additions & 3 deletions src/phg/matching/flann_matcher.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
#include <iostream>
#include <opencv2/core/hal/interface.h>
#include <opencv2/core/mat.hpp>
#include <opencv2/core/types.hpp>
#include <vector>
#include "flann_matcher.h"
#include "flann_factory.h"

static constexpr int N_TREES = 4;
static constexpr int N_CHECKS = 40;

phg::FlannMatcher::FlannMatcher()
{
// параметры для приближенного поиска
// index_params = flannKdTreeIndexParams(TODO);
// search_params = flannKsTreeSearchParams(TODO);
index_params = flannKdTreeIndexParams(N_TREES);
search_params = flannKsTreeSearchParams(N_CHECKS);
}

void phg::FlannMatcher::train(const cv::Mat &train_desc)
Expand All @@ -17,5 +23,14 @@ void phg::FlannMatcher::train(const cv::Mat &train_desc)

void phg::FlannMatcher::knnMatch(const cv::Mat &query_desc, std::vector<std::vector<cv::DMatch>> &matches, int k) const
{
throw std::runtime_error("not implemented yet");
cv::Mat idx(query_desc.rows, k, CV_32SC1);
cv::Mat dst(query_desc.rows, k, CV_32FC1);
flann_index->knnSearch(query_desc, idx, dst, k, *search_params);
matches.resize(query_desc.rows);
for (int i = 0; i < matches.size(); i++) {
matches[i].reserve(k);
for (int j = 0; j < k; ++j) {
matches[i].push_back(cv::DMatch(i, idx.at<int>(i, j), dst.at<float>(i, j)));
}
}
}
129 changes: 73 additions & 56 deletions src/phg/sfm/homography.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
#include "homography.h"

#include <cmath>
#include <functional>
#include <opencv2/calib3d/calib3d.hpp>
#include <iostream>
#include <opencv2/core.hpp>

namespace {
constexpr float MIN_DST2 = 1e-6f;

// источник: https://e-maxx.ru/algo/linear_systems_gauss
// очень важно при выполнении метода гаусса использовать выбор опорного элемента: об этом можно почитать в источнике кода
Expand Down Expand Up @@ -84,8 +88,8 @@ namespace {
double w1 = ws1[i];

// 8 elements of matrix + free term as needed by gauss routine
// A.push_back({TODO});
// A.push_back({TODO});
A.push_back({ w1 * x0, w1 * y0, w1 * w0, 0, 0, 0, -x1 * x0, -x1 * y0, x1 * w0 });
A.push_back({ 0, 0, 0, -w1 * x0, -w1 * y0, -w1 * w0, y1 * x0, y1 * y0, -y1 * w0 });
}

int res = gauss(A, H);
Expand Down Expand Up @@ -136,7 +140,7 @@ namespace {
return *state = x;
}

void randomSample(std::vector<int> &dst, int max_id, int sample_size, uint64_t *state)
void randomSample(std::vector<int> &dst, int max_id, int sample_size, uint64_t *state, const std::function<bool(int)> &check_new_element)
{
dst.clear();

Expand All @@ -145,7 +149,7 @@ namespace {
for (int i = 0; i < sample_size; ++i) {
for (int k = 0; k < max_attempts; ++k) {
int v = xorshift64(state) % max_id;
if (dst.empty() || std::find(dst.begin(), dst.end(), v) == dst.end()) {
if (dst.empty() || std::find(dst.begin(), dst.end(), v) == dst.end() && check_new_element(v)) {
dst.push_back(v);
break;
}
Expand All @@ -168,57 +172,67 @@ namespace {
// * (простое описание для понимания)
// * [3] http://ikrisoft.blogspot.com/2015/01/ransac-with-contrario-approach.html

// const int n_matches = points_lhs.size();
//
// // https://en.wikipedia.org/wiki/Random_sample_consensus#Parameters
// const int n_trials = TODO;
//
// const int n_samples = TODO;
// uint64_t seed = 1;
// const double reprojection_error_threshold_px = 2;
//
// int best_support = 0;
// cv::Mat best_H;
//
// std::vector<int> sample;
// for (int i_trial = 0; i_trial < n_trials; ++i_trial) {
// randomSample(sample, n_matches, n_samples, &seed);
//
// cv::Mat H = estimateHomography4Points(points_lhs[sample[0]], points_lhs[sample[1]], points_lhs[sample[2]], points_lhs[sample[3]],
// points_rhs[sample[0]], points_rhs[sample[1]], points_rhs[sample[2]], points_rhs[sample[3]]);
//
// int support = 0;
// for (int i_point = 0; i_point < n_matches; ++i_point) {
// try {
// cv::Point2d proj = phg::transformPoint(points_lhs[i_point], H);
// if (cv::norm(proj - cv::Point2d(points_rhs[i_point])) < reprojection_error_threshold_px) {
// ++support;
// }
// } catch (const std::exception &e)
// {
// std::cerr << e.what() << std::endl;
// }
// }
//
// if (support > best_support) {
// best_support = support;
// best_H = H;
//
// std::cout << "estimateHomographyRANSAC : support: " << best_support << "/" << n_matches << std::endl;
//
// if (best_support == n_matches) {
// break;
// }
// }
// }
//
// std::cout << "estimateHomographyRANSAC : best support: " << best_support << "/" << n_matches << std::endl;
//
// if (best_support == 0) {
// throw std::runtime_error("estimateHomographyRANSAC : failed to estimate homography");
// }
//
// return best_H;
const int n_matches = points_lhs.size();

// https://en.wikipedia.org/wiki/Random_sample_consensus#Parameters
const int n_trials = std::log(1e-3)/std::log(1 - std::pow(0.2, 4));

const int n_samples = 4;
uint64_t seed = 1;
const double reprojection_error_threshold_px = 2;

int best_support = 0;
cv::Mat best_H;

std::vector<int> sample;
for (int i_trial = 0; i_trial < n_trials; ++i_trial) {
randomSample(sample, n_matches, n_samples, &seed, [&sample, &points_lhs, &points_rhs](int x) {
for (int prev : sample) {
auto left_vec = (points_lhs[prev] - points_lhs[x]);
if (std::hypot(left_vec.x, left_vec.y) < MIN_DST2)
return false;
auto right_vec = (points_rhs[prev] - points_rhs[x]);
if (std::hypot(right_vec.x, right_vec.y) < MIN_DST2)
return false;
}
return true;
});

cv::Mat H = estimateHomography4Points(points_lhs[sample[0]], points_lhs[sample[1]], points_lhs[sample[2]], points_lhs[sample[3]],
points_rhs[sample[0]], points_rhs[sample[1]], points_rhs[sample[2]], points_rhs[sample[3]]);

int support = 0;
for (int i_point = 0; i_point < n_matches; ++i_point) {
try {
cv::Point2d proj = phg::transformPoint(points_lhs[i_point], H);
if (cv::norm(proj - cv::Point2d(points_rhs[i_point])) < reprojection_error_threshold_px) {
++support;
}
} catch (const std::exception &e)
{
std::cerr << e.what() << std::endl;
}
}

if (support > best_support) {
best_support = support;
best_H = H;

std::cout << "estimateHomographyRANSAC : support: " << best_support << "/" << n_matches << std::endl;

if (best_support == n_matches) {
break;
}
}
}

std::cout << "estimateHomographyRANSAC : best support: " << best_support << "/" << n_matches << std::endl;

if (best_support == 0) {
throw std::runtime_error("estimateHomographyRANSAC : failed to estimate homography");
}

return best_H;
}

}
Expand All @@ -238,7 +252,10 @@ cv::Mat phg::findHomographyCV(const std::vector<cv::Point2f> &points_lhs, const
// таким преобразованием внутри занимается функции cv::perspectiveTransform и cv::warpPerspective
cv::Point2d phg::transformPoint(const cv::Point2d &pt, const cv::Mat &T)
{
throw std::runtime_error("not implemented yet");
double x = T.at<double>(0, 0) * pt.x + T.at<double>(0, 1) * pt.y + T.at<double>(0, 2);
double y = T.at<double>(1, 0) * pt.x + T.at<double>(1, 1) * pt.y + T.at<double>(1, 2);
double w = T.at<double>(2, 0) * pt.x + T.at<double>(2, 1) * pt.y + T.at<double>(2, 2);
return { x / w, y / w };
}

cv::Point2d phg::transformPointCV(const cv::Point2d &pt, const cv::Mat &T) {
Expand Down
Loading
Loading