Skip to content

Commit 6091ea3

Browse files
committed
added flann based feature matching
1 parent 71c95af commit 6091ea3

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

src/ImageFeatures.jl

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module ImageFeatures
22

33
# package code goes here
4-
using Images, ColorTypes, FixedPointNumbers, Distributions
4+
using Images, ColorTypes, FixedPointNumbers, Distributions, FLANN, Distances
55

66
include("core.jl")
77
include("const.jl")
@@ -21,6 +21,7 @@ export
2121
create_descriptor,
2222
hamming_distance,
2323
match_keypoints,
24+
match_keypoints_flann,
2425
grade_matches,
2526

2627
#Local Binary Patterns

src/core.jl

+52-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ features = Features(boolean_img)
3939
features = Features(keypoints)
4040
```
4141
42-
Returns a `Vector{Feature}` of features generated from the `true` values in a boolean image or from a
42+
Returns a `Vector{Feature}` of features generated from the `true` values in a boolean image or from a
4343
list of keypoints.
4444
"""
4545
typealias Features Vector{Feature}
@@ -59,7 +59,7 @@ function Keypoints(img::AbstractArray)
5959
map((ri, ci) -> Keypoint(ri, ci), r, c)
6060
end
6161

62-
Keypoints(features::Features) = map(f -> f.keypoint, features)
62+
Keypoints(features::Features) = map(f -> f.keypoint, features)
6363

6464
typealias OrientationPair Tuple{Int16, Int16}
6565
typealias OrientationWeights Tuple{Float16, Float16}
@@ -87,7 +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-
if length(desc_1) > length(desc_2)
90+
if length(desc_1) > length(desc_2)
9191
smaller = desc_2
9292
larger = desc_1
9393
s_key = keypoints_2
@@ -106,6 +106,55 @@ function match_keypoints(keypoints_1::Keypoints, keypoints_2::Keypoints, desc_1,
106106
matches
107107
end
108108

109+
"""
110+
```
111+
matches = match_keypoints_flann(keypoints_1, keypoints_2, desc_1, desc_2, threshold = 0.1)
112+
```
113+
114+
Finds matched keypoints with hamming distance value less than `threshold` using FLANN.
115+
"""
116+
function match_keypoints_flann(keypoints_1::Keypoints, keypoints_2::Keypoints, desc_1, desc_2, threshold::Float64 = 0.1)
117+
smaller = desc_1
118+
larger = desc_2
119+
s_key = keypoints_1
120+
l_key = keypoints_2
121+
order = false
122+
if length(desc_1) > length(desc_2)
123+
smaller = desc_2::Array{BitArray{1},1}
124+
larger = desc_1
125+
s_key = keypoints_2
126+
l_key = keypoints_1
127+
order = true
128+
end
129+
130+
ndims=length(larger[1])
131+
nplarge=length(larger)
132+
npsmall=length(smaller)
133+
134+
data=Matrix{Float64}(ndims, nplarge);
135+
for i in 1:ndims
136+
for j in 1:nplarge
137+
data[i,j]=larger[j][i]?1:0
138+
end
139+
end
140+
141+
f=flann(data, FLANNParameters(), Cityblock())
142+
143+
matches = Keypoints[]
144+
test=Vector{Float64}(ndims);
145+
for i in 1:npsmall
146+
for j in 1:ndims
147+
test[j]=smaller[i][j]?1.0:0.0
148+
end
149+
idx, dist = nearest(f, test, 1)
150+
if dist[1]/ndims < threshold
151+
id_min = idx[1]
152+
push!(matches, order ? [l_key[id_min], s_key[i]] : [s_key[i], l_key[id_min]])
153+
end
154+
end
155+
matches
156+
end
157+
109158
"""
110159
```
111160
grade = grade_matches(keypoints_1, keypoints_2, limit, difference_method)

0 commit comments

Comments
 (0)