Skip to content

Commit 9c50c36

Browse files
committed
remove duplicate matches
1 parent 2c4ae93 commit 9c50c36

File tree

3 files changed

+27
-25
lines changed

3 files changed

+27
-25
lines changed

REQUIRE

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,4 @@ Distributions 0.12
55
FixedPointNumbers 0.3
66
Compat 0.17
77
FLANN
8-
NearestNeighbors
98
Distances

src/ImageFeatures.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ module ImageFeatures
66
using Images, ColorTypes, FixedPointNumbers, Distributions
77
using Compat
88
using FLANN, Distances
9-
using NearestNeighbors
109

1110
include("core.jl")
1211
include("const.jl")

src/core.jl

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ function match_keypoints(keypoints_1::Keypoints, keypoints_2::Keypoints, desc_1,
8787
s_key = keypoints_1
8888
l_key = keypoints_2
8989
order = false
90+
9091
if length(desc_1) > length(desc_2)
9192
smaller = desc_2
9293
larger = desc_1
@@ -97,38 +98,41 @@ function match_keypoints(keypoints_1::Keypoints, keypoints_2::Keypoints, desc_1,
9798

9899
matches = Keypoints[]
99100

100-
ndims=length(larger[1])
101-
n_large=length(larger)
102-
n_small=length(smaller)
103-
104-
data=Matrix{Float64}(ndims, n_large);
105-
for i in 1:ndims
106-
for j in 1:n_large
107-
data[i,j]=larger[j][i]?1:0
108-
end
109-
end
110-
111101
if is_windows() && Sys.WORD_SIZE==32
112-
tree = KDTree(data, Cityblock())
113-
114-
for i in 1:n_small
115-
idx, dist = NearestNeighbors.knn(tree, smaller[i], 1)
116-
if dist[1]/ndims < threshold
117-
id_min = idx[1]
102+
hamming_distances = [hamming_distance(s, l) for s in smaller, l in larger]
103+
for i in 1:length(smaller)
104+
if any(hamming_distances[i, :] .< threshold)
105+
id_min = indmin(hamming_distances[i, :])
118106
push!(matches, order ? [l_key[id_min], s_key[i]] : [s_key[i], l_key[id_min]])
107+
hamming_distances[:, id_min] = 1.0
119108
end
120-
end
109+
end
121110
else
122-
tree = flann(data, FLANNParameters(), Cityblock())
111+
ndims=length(larger[1])
112+
n_large=length(larger)
113+
n_small=length(smaller)
114+
data=Matrix{Float64}(ndims, n_large);
115+
for i in 1:ndims
116+
for j in 1:n_large
117+
data[i,j]=larger[j][i]?1:0
118+
end
119+
end
123120

121+
tree = flann(data, FLANNParameters(), Cityblock())
122+
matched = zeros(Bool, n_large)
124123
for i in 1:n_small
125-
idx, dist = FLANN.knn(tree, Vector{Float64}(smaller[i]), 1)
126-
if dist[1]/ndims < threshold
127-
id_min = idx[1]
128-
push!(matches, order ? [l_key[id_min], s_key[i]] : [s_key[i], l_key[id_min]])
124+
idx = inrange(tree, Vector{Float64}(smaller[i]), 0.1)
125+
for j in idx[1]
126+
if !matched[j]
127+
id_min = j
128+
push!(matches, order ? [l_key[id_min], s_key[i]] : [s_key[i], l_key[id_min]])
129+
matched[j] = true
130+
break
131+
end
129132
end
130133
end
131134
end
135+
132136
matches
133137
end
134138

0 commit comments

Comments
 (0)