11"""
2- MaskMulti(a, [classes])
3-
4- An `N`-dimensional multilabel mask with labels `classes`.
2+ MaskMulti(a, [classes]; interpolate=BSpline(Constant()), extrapolate=Flat())
3+
4+ An `N`-dimensional multilabel mask with labels `classes`. Optionally, the
5+ interpolation and extrapolation method can be provided. Interpolation here
6+ refers to how the values of projected pixels that fall into the transformed
7+ content bounds are calculated. Extrapolation refers to how to assign values
8+ that fall outside the projected content bounds. The default is nearest neighbor
9+ interpolation and flat extrapolation of the edges into new regions.
10+
11+ !!! info
12+ The `Interpolations` package provides numerous methods for use with
13+ the `interpolate` and `extrapolate` keyword arguments. For instance,
14+ `BSpline(Linear())` and `BSpline(Constant())` provide linear and nearest
15+ neighbor interpolation, respectively. In addition `Flat()`, `Reflect()` and
16+ `Periodic()` boundary conditions are available for extrapolation.
517
618## Examples
719
8- {cell=MaskMulti}
920```julia
1021using DataAugmentation
1122
1223mask = MaskMulti(rand(1:3, 100, 100))
1324```
14- {cell=MaskMulti}
1525```julia
1626showitems(mask)
1727```
@@ -20,19 +30,30 @@ struct MaskMulti{N, T<:Integer, U} <: AbstractArrayItem{N, T}
2030 data:: AbstractArray{T, N}
2131 classes:: AbstractVector{U}
2232 bounds:: Bounds{N}
33+ interpolate:: Interpolations.InterpolationType
34+ extrapolate:: ImageTransformations.FillType
2335end
2436
37+ function MaskMulti (
38+ data:: AbstractArray{T,N} ,
39+ classes:: AbstractVector{U} ,
40+ bounds:: Bounds{N} ;
41+ interpolate:: Interpolations.InterpolationType = BSpline (Constant ()),
42+ extrapolate:: ImageTransformations.FillType = Flat (),
43+ ) where {N, T<: Integer , U}
44+ return MaskMulti (data, classes, bounds, interpolate, extrapolate)
45+ end
2546
26- function MaskMulti (a:: AbstractArray , classes = unique (a))
47+ function MaskMulti (a:: AbstractArray , classes = unique (a); kwargs ... )
2748 bounds = Bounds (size (a))
2849 minimum (a) >= 1 || error (" Class values must start at 1" )
29- return MaskMulti (a, classes, bounds)
50+ return MaskMulti (a, classes, bounds; kwargs ... )
3051end
3152
32- MaskMulti (a:: AbstractArray{<:Gray{T}} , args... ) where T = MaskMulti (reinterpret (T, a), args... )
33- MaskMulti (a:: AbstractArray{<:Normed{T}} , args... ) where T = MaskMulti (reinterpret (T, a), args... )
34- MaskMulti (a:: IndirectArray , classes = a. values, bounds = Bounds (size (a))) =
35- MaskMulti (a. index, classes, bounds)
53+ MaskMulti (a:: AbstractArray{<:Gray{T}} , args... ; kwargs ... ) where T = MaskMulti (reinterpret (T, a), args... ; kwargs ... )
54+ MaskMulti (a:: AbstractArray{<:Normed{T}} , args... ; kwargs ... ) where T = MaskMulti (reinterpret (T, a), args... ; kwargs ... )
55+ MaskMulti (a:: IndirectArray , classes = a. values, bounds = Bounds (size (a)); kwargs ... ) =
56+ MaskMulti (a. index, classes, bounds; kwargs ... )
3657
3758Base. show (io:: IO , mask:: MaskMulti{N, T} ) where {N, T} =
3859 print (io, " MaskMulti{$N , $T }() with size $(size (itemdata (mask))) and $(length (mask. classes)) classes" )
@@ -41,12 +62,15 @@ Base.show(io::IO, mask::MaskMulti{N, T}) where {N, T} =
4162getbounds (mask:: MaskMulti ) = mask. bounds
4263
4364
44- function project (P, mask:: MaskMulti , bounds:: Bounds )
45- a = itemdata (mask)
46- etp = mask_extrapolation (a)
47- res = warp (etp, inv (P), bounds. rs)
65+ function project (P, mask:: MaskMulti{N, T, U} , bounds:: Bounds{N} ) where {N, T, U}
66+ res = warp (
67+ itemdata (mask),
68+ inv (P),
69+ bounds. rs;
70+ method= mask. interpolate,
71+ fillvalue= mask. extrapolate)
4872 return MaskMulti (
49- res,
73+ convert .(T, res) ,
5074 mask. classes,
5175 bounds
5276 )
@@ -57,7 +81,7 @@ function project!(bufmask::MaskMulti, P, mask::MaskMulti, bounds)
5781 a = OffsetArray (parent (itemdata (bufmask)), bounds. rs)
5882 warp! (
5983 a,
60- mask_extrapolation (itemdata (mask)),
84+ box_extrapolation (itemdata (mask); method = mask . interpolate, fillvalue = mask . extrapolate ),
6185 inv (P),
6286 )
6387 return MaskMulti (
80104# ## Binary masks
81105
82106"""
83- MaskBinary(a)
84-
85- An `N`-dimensional binary mask.
107+ MaskBinary(a; interpolate=BSpline(Constant()), extrapolate=Flat())
108+
109+ An `N`-dimensional binary mask. Optionally, the interpolation and extrapolation
110+ method can be provided. Interpolation here refers to how the values of
111+ projected pixels that fall into the transformed content bounds are calculated.
112+ Extrapolation refers to how to assign values that fall outside the projected
113+ content bounds. The default is nearest neighbor interpolation and flat
114+ extrapolation of the edges into new regions.
115+
116+ !!! info
117+ The `Interpolations` package provides numerous methods for use with
118+ the `interpolate` and `extrapolate` keyword arguments. For instance,
119+ `BSpline(Linear())` and `BSpline(Constant())` provide linear and nearest
120+ neighbor interpolation, respectively. In addition `Flat()`, `Reflect()` and
121+ `Periodic()` boundary conditions are available for extrapolation.
86122
87123## Examples
88124
89- {cell=MaskMulti}
90125```julia
91126using DataAugmentation
92127
93128mask = MaskBinary(rand(Bool, 100, 100))
94129```
95- {cell=MaskMulti}
96130```julia
97131showitems(mask)
98132```
99133"""
100134struct MaskBinary{N} <: AbstractArrayItem{N, Bool}
101135 data:: AbstractArray{Bool, N}
102136 bounds:: Bounds{N}
137+ interpolate:: Interpolations.InterpolationType
138+ extrapolate:: ImageTransformations.FillType
103139end
104140
105- function MaskBinary (a:: AbstractArray{Bool, N} , bounds = Bounds (size (a))) where N
106- return MaskBinary (a, bounds)
141+ function MaskBinary (
142+ a:: AbstractArray ,
143+ bounds = Bounds (size (a));
144+ interpolate:: Interpolations.InterpolationType = BSpline (Constant ()),
145+ extrapolate:: ImageTransformations.FillType = Flat (),
146+ )
147+ return MaskBinary (a, bounds, interpolate, extrapolate)
107148end
108149
109150Base. show (io:: IO , mask:: MaskBinary{N} ) where {N} =
@@ -112,19 +153,21 @@ Base.show(io::IO, mask::MaskBinary{N}) where {N} =
112153getbounds (mask:: MaskBinary ) = mask. bounds
113154
114155function project (P, mask:: MaskBinary , bounds:: Bounds )
115- etp = mask_extrapolation (itemdata (mask))
116- return MaskBinary (
117- warp (etp, inv (P), bounds. rs),
118- bounds,
119- )
156+ res = warp (
157+ itemdata (mask),
158+ inv (P),
159+ bounds. rs;
160+ method= mask. interpolate,
161+ fillvalue= mask. extrapolate)
162+ return MaskBinary (convert .(Bool, res), bounds)
120163end
121164
122165
123166function project! (bufmask:: MaskBinary , P, mask:: MaskBinary , bounds)
124167 a = OffsetArray (parent (itemdata (bufmask)), bounds. rs)
125- res = warp! (
168+ warp! (
126169 a,
127- mask_extrapolation (itemdata (mask)),
170+ box_extrapolation (itemdata (mask); method = mask . interpolate, fillvalue = mask . extrapolate ),
128171 inv (P),
129172 )
130173 return MaskBinary (
136179function showitem! (img, mask:: MaskBinary )
137180 showimage! (img, colorview (Gray, itemdata (mask)))
138181end
139- # ## Helpers
140-
141-
142- function mask_extrapolation (
143- mask:: AbstractArray{T} ;
144- t = T,
145- degree = Constant (),
146- boundary = Flat ()) where T
147- itp = interpolate (t, t, mask, BSpline (degree))
148- etp = extrapolate (itp, Flat ())
149- return etp
150- end
0 commit comments