1
1
"""
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.
5
17
6
18
## Examples
7
19
8
- {cell=MaskMulti}
9
20
```julia
10
21
using DataAugmentation
11
22
12
23
mask = MaskMulti(rand(1:3, 100, 100))
13
24
```
14
- {cell=MaskMulti}
15
25
```julia
16
26
showitems(mask)
17
27
```
@@ -20,19 +30,30 @@ struct MaskMulti{N, T<:Integer, U} <: AbstractArrayItem{N, T}
20
30
data:: AbstractArray{T, N}
21
31
classes:: AbstractVector{U}
22
32
bounds:: Bounds{N}
33
+ interpolate:: Interpolations.InterpolationType
34
+ extrapolate:: ImageTransformations.FillType
23
35
end
24
36
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
25
46
26
- function MaskMulti (a:: AbstractArray , classes = unique (a))
47
+ function MaskMulti (a:: AbstractArray , classes = unique (a); kwargs ... )
27
48
bounds = Bounds (size (a))
28
49
minimum (a) >= 1 || error (" Class values must start at 1" )
29
- return MaskMulti (a, classes, bounds)
50
+ return MaskMulti (a, classes, bounds; kwargs ... )
30
51
end
31
52
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 ... )
36
57
37
58
Base. show (io:: IO , mask:: MaskMulti{N, T} ) where {N, T} =
38
59
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} =
41
62
getbounds (mask:: MaskMulti ) = mask. bounds
42
63
43
64
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)
48
72
return MaskMulti (
49
- res,
73
+ convert .(T, res) ,
50
74
mask. classes,
51
75
bounds
52
76
)
@@ -57,7 +81,7 @@ function project!(bufmask::MaskMulti, P, mask::MaskMulti, bounds)
57
81
a = OffsetArray (parent (itemdata (bufmask)), bounds. rs)
58
82
warp! (
59
83
a,
60
- mask_extrapolation (itemdata (mask)),
84
+ box_extrapolation (itemdata (mask); method = mask . interpolate, fillvalue = mask . extrapolate ),
61
85
inv (P),
62
86
)
63
87
return MaskMulti (
80
104
# ## Binary masks
81
105
82
106
"""
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.
86
122
87
123
## Examples
88
124
89
- {cell=MaskMulti}
90
125
```julia
91
126
using DataAugmentation
92
127
93
128
mask = MaskBinary(rand(Bool, 100, 100))
94
129
```
95
- {cell=MaskMulti}
96
130
```julia
97
131
showitems(mask)
98
132
```
99
133
"""
100
134
struct MaskBinary{N} <: AbstractArrayItem{N, Bool}
101
135
data:: AbstractArray{Bool, N}
102
136
bounds:: Bounds{N}
137
+ interpolate:: Interpolations.InterpolationType
138
+ extrapolate:: ImageTransformations.FillType
103
139
end
104
140
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)
107
148
end
108
149
109
150
Base. show (io:: IO , mask:: MaskBinary{N} ) where {N} =
@@ -112,19 +153,21 @@ Base.show(io::IO, mask::MaskBinary{N}) where {N} =
112
153
getbounds (mask:: MaskBinary ) = mask. bounds
113
154
114
155
function 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)
120
163
end
121
164
122
165
123
166
function project! (bufmask:: MaskBinary , P, mask:: MaskBinary , bounds)
124
167
a = OffsetArray (parent (itemdata (bufmask)), bounds. rs)
125
- res = warp! (
168
+ warp! (
126
169
a,
127
- mask_extrapolation (itemdata (mask)),
170
+ box_extrapolation (itemdata (mask); method = mask . interpolate, fillvalue = mask . extrapolate ),
128
171
inv (P),
129
172
)
130
173
return MaskBinary (
136
179
function showitem! (img, mask:: MaskBinary )
137
180
showimage! (img, colorview (Gray, itemdata (mask)))
138
181
end
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