|
| 1 | +""" |
| 2 | + localmap(f, A, B=3; eltype=eltype(A), null=zero(eltype), order=FORWARD_FILTER) |
| 3 | +
|
| 4 | +for each position in `A`, applies the function `f` to the values of `A` extracted from the |
| 5 | +neighborhood defined by `B`. |
| 6 | +
|
| 7 | +Keyword `eltype` may be used to specify the the element type `T` of the result. |
| 8 | +By default, it is the same as that of `A`. |
| 9 | +
|
| 10 | +The function `f` is never called with an empty vector of values. Keyword `null` may be |
| 11 | +used to specify the value of the result where the neighborhood is empty. By default, `null |
| 12 | += zero(T)` with `T` the element type of the result. |
| 13 | +
|
| 14 | +Keyword `order` specifies the filter direction, `FORWARD_FILTER` by default. |
| 15 | +
|
| 16 | +For example, applying a median filter of the 2-dimensional image `img` in a sliding `5×5` |
| 17 | +window can be done by: |
| 18 | +
|
| 19 | +``` julia |
| 20 | +using Statistics |
| 21 | +med = localmap(median!, img, 5; eltype=float(Base.eltype(A)), null=NaN) |
| 22 | +``` |
| 23 | +
|
| 24 | +As another example, with argument `f` set to `minimum` or `maximum` respectively yield the |
| 25 | +erosion and the dilation of the input array. However [`erode`](@ref) and [`dilate`](@) are |
| 26 | +much faster. |
| 27 | +
|
| 28 | +""" |
| 29 | +function localmap(f::Function, A::AbstractArray{T,N}, B::Window{N}; |
| 30 | + eltype::Type=T, kwds...) where {T,N} |
| 31 | + return localmap!(f, similar(A, eltype), A, B; kwds...) |
| 32 | +end |
| 33 | + |
| 34 | +""" |
| 35 | + localmap!(f, dst, A, B=3; null=zero(eltype(dst)), order=FORWARD_FILTER) |
| 36 | +
|
| 37 | +set each entry of `dst`, to the result of applying the function `f` to the values of `A` |
| 38 | +extracted from the neighborhood defined by `B`. |
| 39 | +
|
| 40 | +The function `f` is never called with an empty vector of values. Keyword `null` may be |
| 41 | +used to specify the value of the result where the neighborhood is empty. By default, `null |
| 42 | += zero(T)` with `T` the element type of the result. |
| 43 | +
|
| 44 | +Keyword `order` specifies the filter direction, `FORWARD_FILTER` by default. |
| 45 | +
|
| 46 | +""" |
| 47 | +function localmap!(f::Function, dst::AbstractArray{<:Any,N}, |
| 48 | + A::AbstractArray{<:Any,N}, B::Window{N}; kwds...) where {N} |
| 49 | + return localmap!(f, dst, A, kernel(Dims{N}, B); kwds...) |
| 50 | +end |
| 51 | + |
| 52 | +function localmap!(f::Function, dst::AbstractArray{<:Any,N}, |
| 53 | + A::AbstractArray{<:Any,N}, B::AbstractArray{Bool,N}; |
| 54 | + work::AbstractVector = Vector{eltype(A)}(undef, count(B)), |
| 55 | + null = zero(eltype(dst)), |
| 56 | + order::FilterOrdering = FORWARD_FILTER) where {N} |
| 57 | + maxlen = count(B) |
| 58 | + if maxlen == 0 |
| 59 | + fill!(dst, null) |
| 60 | + else |
| 61 | + sizehint!(work, maxlen; shrink=false) |
| 62 | + indices = Indices(dst, A, B) |
| 63 | + if maxlen == length(B) |
| 64 | + # `B` is a simple box. |
| 65 | + indices = Indices(dst, A, B) |
| 66 | + @inbounds for i in indices(dst) |
| 67 | + J = localindices(indices(A), order, indices(B), i) |
| 68 | + len = length(J) |
| 69 | + if len > 0 |
| 70 | + length(work) == len || resize!(work, len) |
| 71 | + k = 0 |
| 72 | + for j in J |
| 73 | + work[k += 1] = A[j] |
| 74 | + end |
| 75 | + k == len || throw(AssertionError("unexpected count")) |
| 76 | + dst[i] = f(work) |
| 77 | + else |
| 78 | + dst[i] = null |
| 79 | + end |
| 80 | + end |
| 81 | + else |
| 82 | + # `B` has "holes". |
| 83 | + indices = Indices(dst, A, B) |
| 84 | + @inbounds for i in indices(dst) |
| 85 | + J = localindices(indices(A), order, indices(B), i) |
| 86 | + len = min(length(J), maxlen) |
| 87 | + if len > 0 |
| 88 | + length(work) ≥ len || resize!(work, len) |
| 89 | + k = 0 |
| 90 | + for j in J |
| 91 | + if B[order(i,j)] |
| 92 | + work[k += 1] = A[j] |
| 93 | + end |
| 94 | + end |
| 95 | + k ≤ len || throw(AssertionError("unexpected count")) |
| 96 | + len = k |
| 97 | + if len > 0 |
| 98 | + length(work) == len || resize!(work, len) |
| 99 | + dst[i] = f(work) |
| 100 | + end |
| 101 | + end |
| 102 | + if len == 0 |
| 103 | + dst[i] = null |
| 104 | + end |
| 105 | + end |
| 106 | + end |
| 107 | + end |
| 108 | + return dst |
| 109 | +end |
0 commit comments