Skip to content

Commit 405b034

Browse files
committed
Update doc. and test localfilter! with single filter function
1 parent 919e48e commit 405b034

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

src/generic.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,12 @@ end
120120
overwrites `dst` with the result of filtering the source `A` by the kernel specified by
121121
`B`, with ordering `ord`, and the function `filter!` which is called as:
122122
123-
filter!(dst, A, ord, B′, i, J)
123+
filter!(dst, A, ord, ker, i, J)
124124
125125
for every index `i` of `dst` and with `J` the subset of indices in the local neighborhood
126126
of `i` and:
127127
128-
B′ = kernel(Dims{N}, B)
128+
ker = kernel(Dims{ndims(A)}, B)
129129
130130
the array representing the filter kernel. The function `filter!` shall compute the result
131131
of the local filtering operation and store it in the destination `dst` at position `i`.

test/runtests.jl

+56-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,48 @@ using LocalFilters:
1212
top_hat!, bottom_hat!,
1313
Returns, reverse, reverse!
1414

15+
zerofill!(A::AbstractArray) = fill!(A, zero(eltype(A)))
16+
17+
function unsafe_erode_filter!(dst::AbstractArray{<:Any,N},
18+
A::AbstractArray{<:Any,N},
19+
ord::FilterOrdering,
20+
B::AbstractArray{<:Any,N},
21+
i, J) where {N}
22+
v = typemax(eltype(B) <: Bool ? eltype(A) : promote_type(eltype(A), eltype(B)))
23+
@inbounds begin
24+
if eltype(B) <: Bool
25+
@simd for j in J
26+
v = ifelse(B[ord(i,j)], min(v, A[j]), v)
27+
end
28+
else
29+
@simd for j in J
30+
v = min(v, A[j] - B[ord(i,j)])
31+
end
32+
end
33+
dst[i] = v
34+
end
35+
end
36+
37+
function unsafe_dilate_filter!(dst::AbstractArray{<:Any,N},
38+
A::AbstractArray{<:Any,N},
39+
ord::FilterOrdering,
40+
B::AbstractArray{<:Any,N},
41+
i, J) where {N}
42+
v = typemin(eltype(B) <: Bool ? eltype(A) : promote_type(eltype(A), eltype(B)))
43+
@inbounds begin
44+
if eltype(B) <: Bool
45+
@simd for j in J
46+
v = ifelse(B[ord(i,j)], max(v, A[j]), v)
47+
end
48+
else
49+
@simd for j in J
50+
v = max(v, A[j] + B[ord(i,j)])
51+
end
52+
end
53+
dst[i] = v
54+
end
55+
end
56+
1557
#=
1658
# Selector for reference methods.
1759
const REF = Val(:Base)
@@ -589,14 +631,17 @@ f2(x) = x > 0.5
589631
wrk = similar(A); # workspace
590632
B2 = similar(A); # for in-place operation
591633
C = copy(A); # to check that the source is left unchanged
592-
@testset "$name" for (name, func, func!) in (("Erosion", erode, erode!),
593-
("Dilation", dilate, dilate!))
634+
@testset "$name" for (name, func, func!, filter!) in (("Erosion", erode, erode!, unsafe_erode_filter!),
635+
("Dilation", dilate, dilate!, unsafe_dilate_filter!))
594636
# ... with a simple rectangular structuring element
595637
B1 = @inferred func(A, R; slow=true);
596638
@test C == A # check that A is left unchanged
597639
@test B2 === @inferred func!(B2, A, R; slow=true)
598640
@test C == A # check that A is left unchanged
599641
@test B2 == B1 # check if in-place and out-of-place yield the same result
642+
@test B2 === @inferred localfilter!(zerofill!(B2), A, R, filter!)
643+
@test C == A # check that A is left unchanged
644+
@test B2 == B1 # check result
600645
@test B1 == @inferred func(A, R; slow=false);
601646
@test C == A # check that A is left unchanged
602647
@test B2 === @inferred func!(B2, A, R; slow=false)
@@ -611,20 +656,29 @@ f2(x) = x > 0.5
611656
@test B2 === @inferred func!(B2, A, F)
612657
@test C == A # check that A is left unchanged
613658
@test B2 == B1 # check if in-place and out-of-place yield the same result
659+
@test B2 === @inferred localfilter!(zerofill!(B2), A, F, filter!)
660+
@test C == A # check that A is left unchanged
661+
@test B2 == B1 # check result
614662
end
615663
# ... with a shaped structuring element
616664
B1 = @inferred func(A, S);
617665
@test C == A # check that A is left unchanged
618666
@test B2 === @inferred func!(B2, A, S)
619667
@test C == A # check that A is left unchanged
620668
@test B2 == B1 # check if in-place and out-of-place yield the same result
669+
@test B2 === @inferred localfilter!(zerofill!(B2), A, S, filter!)
670+
@test C == A # check that A is left unchanged
671+
@test B2 == B1 # check result
621672
if T <: AbstractFloat
622673
F = @inferred strel(T, S) # flat structuring element like S
623674
@test B1 == @inferred func(A, F)
624675
@test C == A # check that A is left unchanged
625676
@test B2 === @inferred func!(B2, A, F)
626677
@test C == A # check that A is left unchanged
627678
@test B2 == B1 # check if in-place and out-of-place yield the same result
679+
@test B2 === @inferred localfilter!(zerofill!(B2), A, F, filter!)
680+
@test C == A # check that A is left unchanged
681+
@test B2 == B1 # check result
628682
end
629683
end
630684
@testset "Local min. and max." begin

0 commit comments

Comments
 (0)