Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added flann based feature matching #33

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ notifications:
email: false
script:
- if [[ -a .git/shallow ]]; then git fetch --unshallow; fi
- julia -e 'Pkg.clone("https://github.com/wildart/FLANN.jl"); Pkg.build("FLANN")'
- julia -e 'Pkg.clone(pwd()); Pkg.build("ImageFeatures")'
# used in tests and Documentation (just install once)
- julia -e 'Pkg.add("TestImages")'
Expand Down
2 changes: 2 additions & 0 deletions REQUIRE
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ Images 0.6
Distributions 0.12
FixedPointNumbers 0.3
Compat 0.17
FLANN
Distances
1 change: 1 addition & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ build_script:
# Need to convert from shallow to complete for Pkg.clone to work
- IF EXIST .git\shallow (git fetch --unshallow)
- C:\projects\julia\bin\julia -e "versioninfo();
Pkg.clone("https://github.com/wildart/FLANN.jl"); Pkg.build("FLANN");
Pkg.clone(pwd(), \"ImageFeatures\"); Pkg.build(\"ImageFeatures\")"

test_script:
Expand Down
3 changes: 3 additions & 0 deletions src/ImageFeatures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ module ImageFeatures
# package code goes here
using Images, ColorTypes, FixedPointNumbers, Distributions
using Compat
if !is_windows() || Sys.WORD_SIZE!=32
using FLANN, Distances
end

include("core.jl")
include("const.jl")
Expand Down
42 changes: 36 additions & 6 deletions src/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,22 +87,52 @@ function match_keypoints(keypoints_1::Keypoints, keypoints_2::Keypoints, desc_1,
s_key = keypoints_1
l_key = keypoints_2
order = false

if length(desc_1) > length(desc_2)
smaller = desc_2
larger = desc_1
s_key = keypoints_2
l_key = keypoints_1
order = true
end
hamming_distances = [hamming_distance(s, l) for s in smaller, l in larger]

matches = Keypoints[]
for i in 1:length(smaller)
if any(hamming_distances[i, :] .< threshold)
id_min = indmin(hamming_distances[i, :])
push!(matches, order ? [l_key[id_min], s_key[i]] : [s_key[i], l_key[id_min]])
hamming_distances[:, id_min] = 1.0

if is_windows() && Sys.WORD_SIZE==32
hamming_distances = [hamming_distance(s, l) for s in smaller, l in larger]
for i in 1:length(smaller)
if any(hamming_distances[i, :] .< threshold)
id_min = indmin(hamming_distances[i, :])
push!(matches, order ? [l_key[id_min], s_key[i]] : [s_key[i], l_key[id_min]])
hamming_distances[:, id_min] = 1.0
end
end
else
ndims=length(larger[1])
n_large=length(larger)
n_small=length(smaller)
data=Matrix{Float64}(ndims, n_large);
for i in 1:ndims
for j in 1:n_large
data[i,j]=larger[j][i]?1:0
end
end

tree = flann(data, FLANNParameters(), Cityblock())
matched = zeros(Bool, n_large)
for i in 1:n_small
idx = inrange(tree, Vector{Float64}(smaller[i]), threshold*ndims)
for j in idx[1]
if !matched[j]
id_min = j
push!(matches, order ? [l_key[id_min], s_key[i]] : [s_key[i], l_key[id_min]])
matched[j] = true
break
end
end
end
end

matches
end

Expand Down