Skip to content
Closed
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
1 change: 1 addition & 0 deletions libs/3rdparty/libgtest/googletest/src/gtest-death-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
# include <errno.h>
# include <fcntl.h>
# include <limits.h>
# include <cstdint>

# if GTEST_OS_LINUX
# include <signal.h>
Expand Down
39 changes: 23 additions & 16 deletions src/phg/matching/cl/bruteforce_matcher.cl
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,33 @@ __kernel void bruteforce_matcher(__global const float* train,
// храним два лучших сопоставления для каждого дескриптора-query:
__local uint res_train_idx_local[KEYPOINTS_PER_WG * 2];
__local float res_distance2_local[KEYPOINTS_PER_WG * 2]; // храним квадраты чтобы не считать корень до самого последнего момента
__local float dist2_for_reduction[NDIM];

// заполняем текущие лучшие дистанции большими значениями
if (dim_id < KEYPOINTS_PER_WG * 2) {
res_distance2_local[dim_id] = FLT_MAX; // полагаемся на то что res_distance2_local размера KEYPOINTS_PER_WG*2==4*2<dim_id<=NDIM==128
}

// грузим 4 дескриптора-query (для каждого из четырех дескрипторов каждый поток грузит значение своей размерности dim_id)
// TODO: т.е. надо прогрузить в query_local все KEYPOINTS_PER_WG=4 дескриптора из query (начиная с индекса query_id0) (а если часть из них выходит за пределы n_query_desc - грузить нули)
for (int ki = 0; ki < KEYPOINTS_PER_WG; ++ki) {
unsigned int query_id = query_id0 + ki;
query_local[ki * NDIM + dim_id] = (query_id < n_query_desc) ? query[query_id * NDIM + dim_id] : 0.0f;
}

barrier(CLK_LOCAL_MEM_FENCE); // дожидаемся прогрузки наших дескрипторов-запросов
barrier(CLK_LOCAL_MEM_FENCE);

for (int train_idx = 0; train_idx < n_train_desc; ++train_idx) {
float train_value_dim = train[train_idx * NDIM + dim_id];
for (int query_local_i = 0; query_local_i < KEYPOINTS_PER_WG; ++query_local_i) {
// хотим посчитать расстояние:
// от дескриптора-query в локальной памяти (#query_local_i)
// до дескриптора-train в глобальной памяти (#train_idx)

// TODO посчитать квадрат расстояния по нашей размерности (dim_id) и сохранить его в нашу ячейку в dist2_for_reduction
float q_val = query_local[query_local_i * NDIM + dim_id];
float diff = train_value_dim - q_val;
dist2_for_reduction[dim_id] = diff * diff;

barrier(CLK_LOCAL_MEM_FENCE);
// TODO суммируем редукцией все что есть в dist2_for_reduction
int step = NDIM / 2;
while (step > 0) {
if (dim_id < step) {
// TODO
dist2_for_reduction[dim_id] += dist2_for_reduction[dim_id + step];
}
barrier(CLK_LOCAL_MEM_FENCE);
step /= 2;
Expand All @@ -63,28 +65,33 @@ __kernel void bruteforce_matcher(__global const float* train,
#define SECOND_BEST_INDEX 1

// пытаемся улучшить самое лучшее сопоставление для локального дескриптора
if (dist2 <= res_distance2_local[query_local_i * 2 + BEST_INDEX]) {
if (dist2 < res_distance2_local[query_local_i * 2 + BEST_INDEX]) {
// не забываем что прошлое лучшее сопоставление теперь стало вторым по лучшевизне (на данный момент)
res_distance2_local[query_local_i * 2 + SECOND_BEST_INDEX] = res_distance2_local[query_local_i * 2 + BEST_INDEX];
res_train_idx_local[query_local_i * 2 + SECOND_BEST_INDEX] = res_train_idx_local[query_local_i * 2 + BEST_INDEX];
// TODO заменяем нашим (dist2, train_idx) самое лучшее сопоставление для локального дескриптора
} else if (dist2 <= res_distance2_local[query_local_i * 2 + SECOND_BEST_INDEX]) { // может мы улучшили хотя бы второе по лучшевизне сопоставление?
// TODO заменяем второе по лучшевизне сопоставление для локального дескриптора
res_distance2_local[query_local_i * 2 + BEST_INDEX] = dist2;
res_train_idx_local[query_local_i * 2 + BEST_INDEX] = train_idx;
} else if (dist2 < res_distance2_local[query_local_i * 2 + SECOND_BEST_INDEX]) { // может мы улучшили хотя бы второе по лучшевизне сопоставление?
// заменяем второе по лучшевизне сопоставление для локального дескриптора
res_distance2_local[query_local_i * 2 + SECOND_BEST_INDEX] = dist2;
res_train_idx_local[query_local_i * 2 + SECOND_BEST_INDEX] = train_idx;
}
}
}
}

barrier(CLK_LOCAL_MEM_FENCE); // дожидаемся финального обновления res_distance2_local/res_train_idx_local

// итак, мы нашли два лучших сопоставления для наших KEYPOINTS_PER_WG дескрипторов, надо сохрнить эти результаты в глобальную память
if (dim_id < KEYPOINTS_PER_WG * 2) { // полагаемся на то что нам надо прогрузить KEYPOINTS_PER_WG*2==4*2<dim_id<=NDIM==128
int query_local_i = dim_id / 2;
int k = dim_id % 2;

int query_id = query_id0 + query_local_i;
if (query_id < n_query_desc) {
res_train_idx[query_id * 2 + k] = // TODO
res_query_idx[query_id * 2 + k] = // TODO хм, не масло масленное ли?
res_distance [query_id * 2 + k] = // TODO не забудьте извлечь корень
res_train_idx[query_id * 2 + k] = res_train_idx_local[query_local_i * 2 + k];
res_query_idx[query_id * 2 + k] = query_id;
res_distance [query_id * 2 + k] = sqrt(res_distance2_local[query_local_i * 2 + k]);
}
}
}
103 changes: 62 additions & 41 deletions src/phg/matching/descriptor_matcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ void phg::DescriptorMatcher::filterMatchesRatioTest(const std::vector<std::vecto
{
filtered_matches.clear();

throw std::runtime_error("not implemented yet");
for (const auto& m : matches) {
if (m.size() == 2 && m[0].distance < 0.5625f * m[1].distance) {
filtered_matches.push_back(m[0]);
}
}
}


Expand All @@ -20,8 +24,8 @@ void phg::DescriptorMatcher::filterMatchesClusters(const std::vector<cv::DMatch>
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 consistent_matches = 4; // minimum number of consistent matches (including candidate)
const float radius_limit_scale = 1.5f; // limit search radius by scaled median

const int n_matches = matches.size();

Expand All @@ -35,42 +39,59 @@ 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

std::shared_ptr<cv::flann::IndexParams> index_params = flannKdTreeIndexParams(1);
std::shared_ptr<cv::flann::SearchParams> search_params = flannKsTreeSearchParams(64);

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);

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);

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;
}

for (int i = 0; i < n_matches; ++i) {
std::vector<int> neigh_q, neigh_t;
for (int j = 0; j < (int)total_neighbours; ++j) {
if (distances2_query.at<float>(i, j) <= radius2_query)
neigh_q.push_back(indices_query.at<int>(i, j));
if (distances2_train.at<float>(i, j) <= radius2_train)
neigh_t.push_back(indices_train.at<int>(i, j));
}

int common = 0;
for (int qi : neigh_q) {
for (int ti : neigh_t) {
if (qi == ti) {
++common;
break;
}
}
}

if (common >= (int)consistent_matches) {
filtered_matches.push_back(matches[i]);
}
}
}
22 changes: 18 additions & 4 deletions src/phg/matching/flann_matcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@

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

void phg::FlannMatcher::train(const cv::Mat &train_desc)
Expand All @@ -17,5 +16,20 @@ 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");
const int n_query = query_desc.rows;
cv::Mat indices(n_query, k, CV_32SC1);
cv::Mat dists(n_query, k, CV_32FC1);
flann_index->knnSearch(query_desc, indices, dists, k, *search_params);

matches.resize(n_query);
for (int i = 0; i < n_query; ++i) {
matches[i].clear();
for (int j = 0; j < k; ++j) {
int idx = indices.at<int>(i, j);
float dist = dists.at<float>(i, j);
if (idx >= 0) {
matches[i].emplace_back(i, idx, dist);
}
}
}
}
Loading
Loading