1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use std::os::raw::{c_char, c_int};
use *;
enum CDescriptorMatcher {}
extern "C" {
fn cv_matcher_new(descriptor_matcher_type: *const c_char) -> *mut CDescriptorMatcher;
fn cv_matcher_drop(descriptor_matcher: *mut CDescriptorMatcher);
fn cv_matcher_add(descriptor_matcher: *mut CDescriptorMatcher, descriptors: *const CVecView<*mut CMat>);
fn cv_matcher_train(descriptor_matcher: *mut CDescriptorMatcher);
fn cv_matcher_is_empty(descriptor_matcher: *mut CDescriptorMatcher) -> bool;
fn cv_matcher_match(
descriptor_matcher: *mut CDescriptorMatcher,
query_descriptors: *mut CMat,
matches: *mut CVec<DMatch>,
);
fn cv_matcher_match_two(
descriptor_matcher: *mut CDescriptorMatcher,
query_descriptors: *mut CMat,
train_descriptors: *mut CMat,
matches: *mut CVec<DMatch>,
);
fn cv_matcher_knn_match(
descriptor_matcher: *mut CDescriptorMatcher,
query_descriptors: *mut CMat,
k: c_int,
matches: *mut CVec<CVec<DMatch>>,
);
}
#[repr(C)]
#[derive(Default, Debug, Clone, Copy)]
pub struct DMatch {
distance: f32,
img_idx: i32,
query_idx: i32,
train_idx: i32,
}
#[derive(Debug, Clone, Copy)]
#[allow(missing_docs)]
pub enum DescriptorMatcherType {
BruteForce,
BruteForceL1,
BruteForceHamming,
BruteForceHamming2,
FlannBased,
}
impl DescriptorMatcherType {
pub(crate) fn as_str(&self) -> &'static str {
match *self {
DescriptorMatcherType::BruteForce => "BruteForce",
DescriptorMatcherType::BruteForceL1 => "BruteForce-L1",
DescriptorMatcherType::BruteForceHamming => "BruteForce-Hamming",
DescriptorMatcherType::BruteForceHamming2 => "BruteForce-Hamming(2)",
DescriptorMatcherType::FlannBased => "FlannBased",
}
}
}
#[derive(Debug)]
pub struct DescriptorMatcher {
value: *mut CDescriptorMatcher,
}
impl Drop for DescriptorMatcher {
fn drop(&mut self) {
unsafe {
cv_matcher_drop(self.value);
}
}
}
impl DescriptorMatcher {
pub fn new(descriptor_matcher_type: DescriptorMatcherType) -> DescriptorMatcher {
let descriptor_matcher_type = CString::new(descriptor_matcher_type.as_str()).unwrap();
let value = unsafe { cv_matcher_new(descriptor_matcher_type.as_ptr()) };
DescriptorMatcher { value: value }
}
pub fn add(&mut self, descriptors: &Vec<&Mat>) {
let descriptors = descriptors.iter().map(|x| x.inner).collect();
let vec_view = CVecView::pack(&descriptors);
unsafe {
cv_matcher_add(self.value, &vec_view);
}
}
pub fn train(&mut self) {
unsafe { cv_matcher_train(self.value) }
}
pub fn is_empty(&self) -> bool {
unsafe { cv_matcher_is_empty(self.value) }
}
pub fn match_(&self, query_descriptors: &Mat) -> Vec<DMatch> {
let mut matches = CVec::<DMatch>::default();
unsafe {
cv_matcher_match(self.value, query_descriptors.inner, &mut matches);
}
matches.unpack()
}
pub fn match_two(&self, query_descriptors: &Mat, train_descriptors: &Mat) -> Vec<DMatch> {
let mut matches = CVec::<DMatch>::default();
unsafe {
cv_matcher_match_two(
self.value,
query_descriptors.inner,
train_descriptors.inner,
&mut matches,
);
}
matches.unpack()
}
pub fn knn_match(&self, query_descriptors: &Mat, k: usize) -> Vec<Vec<DMatch>> {
let mut matches = CVec::<CVec<DMatch>>::default();
unsafe {
cv_matcher_knn_match(self.value, query_descriptors.inner, k as c_int, &mut matches);
}
matches.unpack()
}
}