Skip to content

Commit

Permalink
Fix broken tests and eliminate depwarns
Browse files Browse the repository at this point in the history
  • Loading branch information
timholy committed Jul 22, 2023
1 parent 462cb20 commit 1bf507d
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 37 deletions.
1 change: 1 addition & 0 deletions src/ImageFeatures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module ImageFeatures
using Images, Distributions
using SparseArrays
import Random.seed!
using Images.ImageTransformations.Interpolations

include("core.jl")
include("const.jl")
Expand Down
2 changes: 1 addition & 1 deletion src/brisk.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function _brisk_tables(pattern_scale::Float64)
end

function create_descriptor(img::AbstractArray{T, 2}, features::Features, params::BRISK) where T<:Gray
int_img = integral_image(img)
int_img = IntegralArray(img)
descriptors = BitArray{1}[]
ret_features = Feature[]
window_size = ceil(Int, (brisk_radii[end] + brisk_sigma[end]) * params.pattern_scale * 0.85) + 1
Expand Down
20 changes: 20 additions & 0 deletions src/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,23 @@ function grade_matches(keypoints_1::Keypoints, keypoints_2::Keypoints, limit::Re
@assert length(keypoints_1)!=0 "Keypoint list is of size zero."
mean(map((keypoint_1,keypoint_2)->((diff(keypoint_1,keypoint_2) < limit) ? 1.0 : 0.0), keypoints_1, keypoints_2))
end

function bilinear_interpolator(img::AbstractMatrix{T}, padding::Tuple{UnitRange,UnitRange}) where T
pad_ax = map(axes(img), padding) do ax, x
first(ax)+first(x):last(ax)+last(x)
end
padded_img = PaddedView(zero(T), img, pad_ax)
return interpolate(padded_img, BSpline(Linear()))
end
bilinear_interpolator(img::AbstractMatrix, offsets) = bilinear_interpolator(img, padding_from_offsets(offsets))

function padding_from_offsets(offsets)
padding = [0:0 for _ in first(offsets)]
for o in offsets
for (i, p) in enumerate(o)
pd = padding[i]
padding[i] = min(pd.start, floor(Int, p)) : max(pd.stop, ceil(Int, p))
end
end
return (padding...,)
end
4 changes: 2 additions & 2 deletions src/corner.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ function corner_orientations(img::AbstractArray)
corners = imcorner(img)
corner_indexes = Keypoints(corners)
kernel = Kernel.gaussian((2,2), (5, 5))
kernel /= maxfinite(kernel)
kernel /= maximum_finite(kernel)
corner_orientations(img, corner_indexes, parent(kernel))
end

function corner_orientations(img::AbstractArray, corners::Keypoints)
kernel = Kernel.gaussian((2,2), (5, 5))
kernel /= maxfinite(kernel)
kernel /= maximum_finite(kernel)
corner_orientations(img, corners, parent(kernel))
end
4 changes: 2 additions & 2 deletions src/freak.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function _freak_mean_intensity(int_img::AbstractArray{T, 2}, keypoint::Keypoint,
ys = round(Int, y - sigma)
xst = round(Int, x + sigma)
yst = round(Int, y + sigma)
intensity = boxdiff(int_img, ys:yst, xs:xst)
intensity = int_img[ys..yst, xs..xst]
intensity / ((xst - xs + 1) * (yst - ys + 1))
end

Expand Down Expand Up @@ -82,7 +82,7 @@ function _freak_tables(pattern_scale::Float64)
end

function create_descriptor(img::AbstractArray{T, 2}, keypoints::Keypoints, params::FREAK) where T<:Gray
int_img = integral_image(img)
int_img = IntegralArray(img)
descriptors = BitArray{1}[]
ret_keypoints = Keypoint[]
window_size = ceil(Int, (freak_radii[1] + freak_sigma[1]) * params.pattern_scale) + 1
Expand Down
6 changes: 3 additions & 3 deletions src/glcm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
glcm = glcm(img, distance, angles, mat_size=16)
glcm = glcm(img, distances, angles, mat_size=16)
Calculates the GLCM (Gray Level Co-occurence Matrix) of an image. The `distances` and `angles` arguments may be
Calculates the GLCM (Gray Level Co-occurence Matrix) of an image. The `distances` and `angles` arguments may be
a single integer or a vector of integers if multiple GLCMs need to be calculated. The `mat_size` argument is used
to define the granularity of the GLCM.
"""
Expand Down Expand Up @@ -97,7 +97,7 @@ function glcm_norm(img::AbstractArray, distances, angles, mat_size=16)
end

"""
Multiple properties of the obtained GLCM can be calculated by using the `glcm_prop` function which calculates the
Multiple properties of the obtained GLCM can be calculated by using the `glcm_prop` function which calculates the
property for the entire matrix. If grid dimensions are provided, the matrix is divided into a grid and the property
is calculated for each cell resulting in a height x width property matrix.
```julia
Expand Down Expand Up @@ -183,7 +183,7 @@ function correlation(glcm_window::Array{T, 2}) where T<:Real
end

function max_prob(glcm_window::Array{T, 2}) where T<:Real
maxfinite(glcm_window)
maximum_finite(glcm_window)
end

function energy(glcm_window::Array{T, 2}) where T<:Real
Expand Down
23 changes: 13 additions & 10 deletions src/lbp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ end
function _lbp(img::AbstractArray{T, 2}, points::Integer, offsets::Array, method::Function = lbp_original) where T<:Gray
uniform_params = PatternCache(points)
lbp_image = zeros(UInt, size(img))
imgitp = bilinear_interpolator(img, offsets)
R = CartesianIndices(size(img))
bit_pattern = falses(length(offsets))
for I in R
for (i, o) in enumerate(offsets) bit_pattern[i] = img[I] >= bilinear_interpolation(img, I[1] + o[1], I[2] + o[2]) end
for (i, o) in enumerate(offsets) bit_pattern[i] = img[I] >= T(imgitp(I[1] + o[1], I[2] + o[2])) end
lbp_image[I], uniform_params = method(bit_pattern, uniform_params)
end
lbp_image
Expand All @@ -62,11 +63,12 @@ lbp(img::AbstractArray{T, 2}, points::Integer, radius::Number, method::Function
function _modified_lbp(img::AbstractArray{T, 2}, points::Integer, offsets::Array, method::Function = lbp_original) where T<:Gray
uniform_params = PatternCache(points)
lbp_image = zeros(UInt, size(img))
imgitp = bilinear_interpolator(img, offsets)
R = CartesianIndices(size(img))
bit_pattern = falses(length(offsets))
for I in R
avg = (sum(bilinear_interpolation(img, I[1] + o[1], I[2] + o[2]) for o in offsets) + img[I]) / (points + 1)
for (i, o) in enumerate(offsets) bit_pattern[i] = avg >= bilinear_interpolation(img, I[1] + o[1], I[2] + o[2]) end
avg = (sum(imgitp(I[1] + o[1], I[2] + o[2]) for o in offsets) + img[I]) / (points + 1)
for (i, o) in enumerate(offsets) bit_pattern[i] = avg >= imgitp(I[1] + o[1], I[2] + o[2]) end
lbp_image[I], uniform_params = method(bit_pattern, uniform_params)
end
lbp_image
Expand All @@ -76,15 +78,16 @@ modified_lbp(img::AbstractArray{T, 2}, method::Function = lbp_original) where {T

modified_lbp(img::AbstractArray{T, 2}, points::Integer, radius::Number, method::Function = lbp_original) where {T<:Gray} = _modified_lbp(img, points, circular_offsets(points, radius), method)

function _direction_coded_lbp(img::AbstractArray{T, 2}, offsets::Array) where T
function _direction_coded_lbp(img::AbstractArray{T, 2}, offsets::Array) where T<:Union{Normed,AbstractGray{<:Normed}}
lbp_image = zeros(UInt, size(img))
R = CartesianIndices(size(img))
p = Int(length(offsets) / 2)
raw_img = convert(Array{Int}, rawview(channelview(img)))
imgitp = bilinear_interpolator(raw_img, offsets)
neighbours = zeros(Int, length(offsets))
for I in R
for (i, o) in enumerate(offsets)
neighbours[i] = Int(bilinear_interpolation(img, I[1] + o[1], I[2] + o[2]).val.i)
neighbours[i] = round(Int, imgitp(I[1] + o[1], I[2] + o[2]))
end
lbp_image[I] = sum(((neighbours[j] - raw_img[I]) * (neighbours[j + p] - raw_img[I]) >= 0) * (2 ^ (2 * p - 2 * j + 1)) +
(abs(neighbours[j] - raw_img[I]) >= abs(neighbours[j + p] - raw_img[I])) * (2 ^ (2 * p - 2 * j)) for j in 1:p)
Expand All @@ -100,19 +103,19 @@ function direction_coded_lbp(img::AbstractArray{T, 2}, points::Integer, radius::
end

function multi_block_lbp(img::AbstractArray{T, 2}, tl_y::Integer, tl_x::Integer, height::Integer, width::Integer) where T<:Gray
int_img = integral_image(img)
int_img = IntegralArray(img)
h, w = size(img)

@assert (tl_y + 3 * height - 1 <= h) && (tl_x + 3 * width -1 <= w) "Rectangle Grid exceeds image dimensions."

center = [tl_y + height, tl_x + width]
central_sum = boxdiff(int_img, tl_y + height : tl_y + 2 * height - 1, tl_x + width : tl_x + 2 * width - 1)
central_sum = int_img[tl_y + height .. tl_y + 2 * height - 1, tl_x + width .. tl_x + 2 * width - 1]
lbp_code = 0

for (i, o) in enumerate(original_offsets)
cur_tl_y = center[1] + o[1] * height
cur_tl_x = center[2] + o[2] * width
cur_window_sum = boxdiff(int_img, cur_tl_y : cur_tl_y + height - 1, cur_tl_x : cur_tl_x + height - 1)
cur_window_sum = int_img[cur_tl_y .. cur_tl_y + height - 1, cur_tl_x .. cur_tl_x + height - 1]
lbp_code += (cur_window_sum > central_sum ? 1 : 0) * 2 ^ (8 - i)
end
lbp_code
Expand All @@ -129,8 +132,8 @@ function _create_descriptor(img::AbstractArray{Gray{T}, 2}, yblocks::Integer = 4
for j in 1:yblocks
lbp_image = lbp_type(img[(j-1)*blockh+1 : j*blockh, (i-1)*blockw+1 : i*blockw], args...)
lbp_norm = lbp_image
_, hist = imhist(lbp_image, edges)
append!(descriptor, hist[2 : end - 1])
_, hist = build_histogram(lbp_image, edges)
append!(descriptor, hist[1 : end - 1])
end
end
descriptor
Expand Down
6 changes: 3 additions & 3 deletions test/freak.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ end
desc_2, ret_keypoints_2 = create_descriptor(img_array_2, keypoints_2, freak_params)
matches = match_keypoints(ret_keypoints_1, ret_keypoints_2, desc_1, desc_2, 0.1)
reverse_keypoints_1 = [_reverserotate(m[1], pi / 4, (256, 384)) for m in matches]
@test all(isapprox(rk[1], m[2][1], atol = 4) && isapprox(rk[2], m[2][2], atol = 4) for (rk, m) in zip(reverse_keypoints_1, matches))
@test sum(isapprox(rk[1], m[2][1], atol = 4) && isapprox(rk[2], m[2][2], atol = 4) for (rk, m) in zip(reverse_keypoints_1, matches)) >= length(matches) - 1
end

@testset "Testing with Standard Images - Lighthouse (Rotation 45, Translation (50, 40))" begin
Expand All @@ -38,7 +38,7 @@ end
desc_2, ret_keypoints_2 = create_descriptor(img_array_2, keypoints_2, freak_params)
matches = match_keypoints(ret_keypoints_1, ret_keypoints_2, desc_1, desc_2, 0.1)
reverse_keypoints_1 = [_reverserotate(m[1], pi / 4, (256, 384)) + CartesianIndex(50, 40) for m in matches]
@test all(isapprox(rk[1], m[2][1], atol = 3) && isapprox(rk[2], m[2][2], atol = 3) for (rk, m) in zip(reverse_keypoints_1, matches))
@test sum(isapprox(rk[1], m[2][1], atol = 3) && isapprox(rk[2], m[2][2], atol = 3) for (rk, m) in zip(reverse_keypoints_1, matches)) >= length(matches) - 1
end

@testset "Testing with Standard Images - Lighthouse (Rotation 75, Translation (50, 40))" begin
Expand All @@ -55,7 +55,7 @@ end
desc_2, ret_keypoints_2 = create_descriptor(img_array_2, keypoints_2, freak_params)
matches = match_keypoints(ret_keypoints_1, ret_keypoints_2, desc_1, desc_2, 0.1)
reverse_keypoints_1 = [_reverserotate(m[1], 5 * pi / 6, (256, 384)) + CartesianIndex(50, 40) for m in matches]
@test all(isapprox(rk[1], m[2][1], atol = 4) && isapprox(rk[2], m[2][2], atol = 4) for (rk, m) in zip(reverse_keypoints_1, matches))
@test sum(isapprox(rk[1], m[2][1], atol = 4) && isapprox(rk[2], m[2][2], atol = 4) for (rk, m) in zip(reverse_keypoints_1, matches)) >= length(matches) - 1
end

@testset "Testing with Standard Images - Lena (Rotation 45, Translation (10, 20))" begin
Expand Down
2 changes: 1 addition & 1 deletion test/glcm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ end

@testset "Properties" begin
img = convert(Array{Int}, reshape(1:1:30, 5, 6))
@test glcm_prop(img, max_prob) == maxfinite(img)
@test glcm_prop(img, max_prob) == maximum_finite(img)
@test glcm_prop(img, contrast) == 2780
@test glcm_prop(img, dissimilarity) == 930
@test glcm_prop(img, ASM) == 9455
Expand Down
17 changes: 2 additions & 15 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ using ImageFeatures, Images, TestImages, Distributions
using Test
using LinearAlgebra
import Random.seed!
using Images.ImageTransformations: imrotate

function check_samples(sample_one, sample_two, size::Int, window::Int)
check_bool = true
Expand All @@ -26,21 +27,7 @@ function _warp(img, transx, transy)
res
end

function _warp(img, angle)
cos_angle = cos(angle)
sin_angle = sin(angle)
res = zeros(eltype(img), size(img))
cx = size(img, 1) / 2
cy = size(img, 2) / 2
for i in 1:size(res, 1)
for j in 1:size(res, 2)
i_rot = ceil(Int, cos_angle * (i - cx) - sin_angle * (j - cy) + cx)
j_rot = ceil(Int, sin_angle * (i - cx) + cos_angle * (j - cy) + cy)
if checkbounds(Bool, img, i_rot, j_rot) res[i, j] = bilinear_interpolation(img, i_rot, j_rot) end
end
end
res
end
_warp(img, angle) = imrotate(img, angle, axes(img))

function _reverserotate(p, angle, center)
cos_angle = cos(angle)
Expand Down

0 comments on commit 1bf507d

Please sign in to comment.