Skip to content

Commit 5aea2bf

Browse files
committed
Box{N}, Window{N}, and Kernel{N} aliases
1 parent 80e5649 commit 5aea2bf

File tree

6 files changed

+62
-49
lines changed

6 files changed

+62
-49
lines changed

NEWS.md

+15-4
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,21 @@
2727
* The algorithm to infer the result type is now based on Julia's arithmetic rules and can
2828
cope with arguments that have units.
2929

30-
* To represent hyper-rectangular neighborhoods, instances of non-exported
31-
`LocalFilters.Box` have been replaced by fast uniform arrays with offset axes from the
32-
[`StructuredArrays`](https://github.com/emmt/StructuredArrays.jl) package.
33-
`LocalFilters.Box{N,I}` is now an alias to `FastUniformArray{Boll,N,true,I}`.
30+
* Non-exported *public* aliases `LocalFilters.Kernel{N}` and `LocalFilters.Window{N}`for
31+
union of types suitable to define `N`-dimensional kernels or windows in `LocalFilters`.
32+
A **kernel** is an array of weights implementing a local filter or an array of Booleans
33+
representing a local neighborhood. A **window** is an array of Booleans representing a
34+
local neighborhood. As an optimization, a **box** is an hyper-rectangular neighborhood
35+
with axes aligned with the Cartesian axes. Thus, a window whose values are all true is
36+
also a box and a kernel with Boolean values is also a window. The function
37+
`kernel(Dims{N},B)` yields an `N`-dimensional array for any `B::LocalFilters.Kernel{N}`,
38+
with Boolean values for any `B::LocalFilters.Window{N}`.
39+
40+
* Non-exported *public* type `LocalFilters.Box{N}` is now an alias to an efficient type to
41+
represent `N`-dimensional **boxes**, that is uniformly true windows or hyper-rectangular
42+
neighborhoods. Currently, instances of this type are fast uniformly true arrays with
43+
offset axes from the [`StructuredArrays`](https://github.com/emmt/StructuredArrays.jl)
44+
package and `LocalFilters.Box{N}` is just `FastUniformArray{Bool,N,true}`.
3445

3546
* In local filtering operations, small integers arguments are automatically promoted to a
3647
wider integer type to avoid overflows. This is similar to what is done by base reduction

docs/src/reference.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,18 @@ FORWARD_FILTER
6363
REVERSE_FILTER
6464
```
6565

66-
## Neighborhoods and kernels
66+
## Neighborhoods, boxes, kernels, and windows
6767

6868
```@docs
6969
kernel
70-
reverse_kernel
70+
LocalFilters.Kernel
71+
LocalFilters.Window
7172
strel
7273
LocalFilters.ball
7374
LocalFilters.box
75+
LocalFilters.Box
7476
LocalFilters.centered
77+
reverse_kernel
7578
```
7679

7780
## Utilities

src/generic.jl

+8-18
Original file line numberDiff line numberDiff line change
@@ -83,20 +83,14 @@ As another example, implementing a convolution by `B` writes:
8383
""" localfilter!
8484

8585
# This version builds a kernel.
86-
function localfilter!(dst::AbstractArray{<:Any,N},
87-
A::AbstractArray{<:Any,N},
88-
B::Window{N},
89-
initial,
90-
update::Function,
86+
function localfilter!(dst::AbstractArray{<:Any,N}, A::AbstractArray{<:Any,N},
87+
B::Kernel{N}, initial, update::Function,
9188
final::Function = identity; kwds...) where {N}
9289
return localfilter!(dst, A, kernel(Dims{N}, B), initial, update, final; kwds...)
9390
end
9491

95-
@inline function localfilter!(dst::AbstractArray{<:Any,N},
96-
A::AbstractArray{<:Any,N},
97-
B::AbstractArray{<:Any,N},
98-
initial,
99-
update::Function,
92+
@inline function localfilter!(dst::AbstractArray{<:Any,N}, A::AbstractArray{<:Any,N},
93+
B::AbstractArray{<:Any,N}, initial, update::Function,
10094
final::Function = identity;
10195
order::FilterOrdering = FORWARD_FILTER) where {N}
10296
!(initial isa Function) || axes(dst) == axes(A) || throw(DimensionMismatch(
@@ -141,18 +135,14 @@ automatically yield either `B[j-i]` or `B[i-j]` depending on whether `order` is
141135
`FORWARD_FILTER` or `REVERSE_FILTER`.
142136
143137
"""
144-
function localfilter!(dst::AbstractArray{<:Any,N},
145-
A::AbstractArray{<:Any,N},
146-
B::Window{N},
147-
filter!::Function; kwds...) where {N}
138+
function localfilter!(dst::AbstractArray{<:Any,N}, A::AbstractArray{<:Any,N},
139+
B::Kernel{N}, filter!::Function; kwds...) where {N}
148140
# Build kernel.
149141
return localfilter!(dst, A, kernel(Dims{N}, B), filter!; kwds...)
150142
end
151143

152-
function localfilter!(dst::AbstractArray{<:Any,N},
153-
A::AbstractArray{<:Any,N},
154-
B::AbstractArray{<:Any,N},
155-
filter!::Function;
144+
function localfilter!(dst::AbstractArray{<:Any,N}, A::AbstractArray{<:Any,N},
145+
B::AbstractArray{<:Any,N}, filter!::Function;
156146
order::FilterOrdering = FORWARD_FILTER) where {N}
157147
indices = Indices(dst, A, B)
158148
@inbounds for i in indices(dst)

src/linear.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Keyword `order` specifies the filter direction, `FORWARD_FILTER` by default.
3030
See also [`localmean!`](@ref) and [`localfilter!`](@ref).
3131
3232
"""
33-
localmean(A::AbstractArray{<:Any,N}, B::Window{N} = 3; kwds...) where {N} =
33+
localmean(A::AbstractArray{<:Any,N}, B::Kernel{N} = 3; kwds...) where {N} =
3434
localmean(A, kernel(Dims{N}, B); kwds...)
3535

3636
function localmean(A::AbstractArray{<:Any,N}, B::AbstractArray{<:Any,N}; kwds...) where {N}
@@ -53,7 +53,7 @@ See also [`localmean`](@ref) and [`localfilter!`](@ref).
5353
5454
"""
5555
function localmean!(dst::AbstractArray{<:Any,N}, A::AbstractArray{<:Any,N},
56-
B::Window{N} = 3; kwds...) where {N}
56+
B::Kernel{N} = 3; kwds...) where {N}
5757
return localmean!(dst, A, kernel(Dims{N}, B); kwds...)
5858
end
5959

src/morphology.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ for (f, op) in ((:erode, :min), (:dilate, :max))
104104

105105
# Build structuring element.
106106
function $f!(dst::AbstractArray{<:Any,N}, A::AbstractArray{<:Any,N},
107-
B::Window{N} = 3; kwds...) where {N}
107+
B::Kernel{N} = 3; kwds...) where {N}
108108
return $f!(dst, A, kernel(Dims{N}, B); kwds...)
109109
end
110110

@@ -122,7 +122,7 @@ for (f, op) in ((:erode, :min), (:dilate, :max))
122122
end
123123

124124
# Fast separable filter (in-place).
125-
function $f!(A::AbstractArray{<:Any,N}, B::Window{N} = 3; kwds...) where {N}
125+
function $f!(A::AbstractArray{<:Any,N}, B::Kernel{N} = 3; kwds...) where {N}
126126
_B = kernel(Dims{N}, B)
127127
is_morpho_math_box(_B) || throw(ArgumentError(
128128
"for in-place operation kernel must be a simple box"))
@@ -239,7 +239,7 @@ See [`localextrema`](@ref) for an out-of-place version for more information.
239239
function localextrema!(Amin::AbstractArray{<:Any,N},
240240
Amax::AbstractArray{<:Any,N},
241241
A::AbstractArray{<:Any,N},
242-
B::Window{N} = 3; kwds...) where {N}
242+
B::Kernel{N} = 3; kwds...) where {N}
243243
return localextrema!(Amin, Amax, A, kernel(Dims{N}, B); kwds...)
244244
end
245245

@@ -365,7 +365,7 @@ for f in (:closing, :opening)
365365

366366
# Provide default structuring element and build kernel.
367367
function $f!(dst::AbstractArray{<:Any,N}, wrk::AbstractArray{<:Any,N},
368-
A::AbstractArray{<:Any,N}, B::Window{N} = 3; kwds...) where {N}
368+
A::AbstractArray{<:Any,N}, B::Kernel{N} = 3; kwds...) where {N}
369369
return $f!(dst, wrk, A, kernel(Dims{N}, B); kwds...)
370370
end
371371
end

src/types.jl

+28-19
Original file line numberDiff line numberDiff line change
@@ -27,42 +27,51 @@ const ArrayAxes{N} = NTuple{N,AbstractUnitRange{<:Integer}}
2727
struct Indices{S<:IndexStyle} <: Function end
2828

2929
"""
30-
LocalFilters.Window{N}
30+
const LocalFilters.Box{N} = FastUniformArray{Bool,N,true}
31+
32+
is an alias to the type of uniformly `true` `N`-dimensional arrays. Instances of this kind
33+
are used to represent hyper-rectangular sliding windows in `LocalFilters`.
34+
35+
Method [`LocalFilters.box`](@ref) can be used to build an object of this type.
3136
32-
is the union of types for an argument `B` to be suitable to define a simple
33-
`N`-dimensional hyper-rectangular sliding window and that can be converted into a
34-
`Box{N}`, that is a `N`-dimensional uniformly true kernel array by [`kernel(Dims{N},
35-
B)`](@ref LocalFilters.kernel).
37+
"""
38+
const Box{N} = FastUniformArray{Bool,N,true}
39+
@public Box
3640

41+
"""
42+
LocalFilters.BoxLike{N}
3743
38-
a kernel
39-
by the [`LocalFilters.kernel`](@ref) method.
44+
is the union of types of arguments suitable to define a simple `N`-dimensional *box*, that
45+
is an hyper-rectangular sliding window. Any argument `B` of this type can be converted
46+
into a `Box{N}` instance which is a `N`-dimensional uniformly true array by
47+
[`kernel(Dims{N}, B)`](@ref LocalFilters.kernel).
4048
4149
"""
42-
const Window{N} = Union{Axis,NTuple{N,Axis},NTuple{2,CartesianIndex{N}},
43-
CartesianIndices{N}}
50+
const BoxLike{N} = Union{Axis,NTuple{N,Axis},NTuple{2,CartesianIndex{N}},
51+
CartesianIndices{N}}
4452

4553
"""
4654
LocalFilters.Kernel{N}
4755
48-
is the union of types for an argument `B` to be suitable to define a `N`-dimensional
49-
kernel or filter that can be converted into a `N`-dimensional kernel array by
56+
is the union of types of arguments suitable to define a `N`-dimensional kernel or filter.
57+
Any argument `B` of this type can be converted into a `N`-dimensional kernel array by
5058
[`kernel(Dims{N}, B)`](@ref LocalFilters.kernel).
5159
5260
"""
53-
const Kernel{N} = Union{Window{N},AbstractArray{<:Any,N}}
61+
const Kernel{N} = Union{BoxLike{N},AbstractArray{<:Any,N}}
62+
@public Kernel
5463

5564
"""
56-
const LocalFilters.Box{N} = FastUniformArray{Bool,N,true}
57-
58-
is an alias to the type of `N`-dimensional arrays whose elements are all `true`. Instances
59-
of this kind are used to represent hyper-rectangular sliding windows in `LocalFilters`.
65+
LocalFilters.Window{N}
6066
61-
Method [`LocalFilters.box`](@ref) can be used to build an object of this type.
67+
is the union of types of arguments suitable to define a `N`-dimensional sliding *window*
68+
that is a `N`-dimensional array of Booleans indicating whether a position belongs to the
69+
window of not. Any argument `B` of this type can be converted into a `N`-dimensional
70+
array of Booleans by [`kernel(Dims{N}, B)`](@ref LocalFilters.kernel).
6271
6372
"""
64-
const Box{N} = FastUniformArray{Bool,N,true}
65-
@public Box
73+
const Window{N} = Union{BoxLike{N},AbstractArray{Bool,N}}
74+
@public Window
6675

6776
"""
6877
LocalFilters.FilterOrdering

0 commit comments

Comments
 (0)