@@ -68,7 +68,7 @@ macro test_approx_eq_sigma_eps(A, B, sigma, eps)
68
68
kern = KernelFactors. IIRGaussian ($ (esc (sigma)))
69
69
Af = imfilter ($ (esc (A)), kern, NA ())
70
70
Bf = imfilter ($ (esc (B)), kern, NA ())
71
- diffscale = max (_abs (maxabsfinite ( $ (esc (A)))), _abs (maxabsfinite ( $ (esc (B)))))
71
+ diffscale = max (_abs (maximum_finite (abs, $ (esc (A)))), _abs (maximum_finite (abs, $ (esc (B)))))
72
72
d = sad (Af, Bf)
73
73
if d > length (Af)* diffscale* ($ (esc (eps)))
74
74
error (" Arrays A and B differ" )
@@ -104,7 +104,7 @@ function test_approx_eq_sigma_eps(A::AbstractArray, B::AbstractArray,
104
104
kern = KernelFactors. IIRGaussian (sigma)
105
105
Af = imfilter (A, kern, NA ())
106
106
Bf = imfilter (B, kern, NA ())
107
- diffscale = max (_abs (maxabsfinite ( A)), _abs (maxabsfinite ( B)))
107
+ diffscale = max (_abs (maximum_finite (abs, A)), _abs (maximum_finite (abs, B)))
108
108
d = sad (Af, Bf)
109
109
diffpct = d / (length (Af) * diffscale)
110
110
if diffpct > eps
131
131
imgaussiannoise (img:: AbstractArray{T} , variance:: Number ) where {T} = imgaussiannoise (img, variance, 0 )
132
132
imgaussiannoise (img:: AbstractArray{T} ) where {T} = imgaussiannoise (img, 0.01 , 0 )
133
133
134
- # image gradients
135
-
136
- function div (p:: AbstractArray{T,3} ) where T
137
- # Definition from the Chambolle citation below, between Eqs. 5 and 6
138
- # This is the adjoint of -forwarddiff
139
- inds = axes (p)[1 : 2 ]
140
- out = similar (p, inds)
141
- Router = CartesianIndices (inds)
142
- rstp = _oneunit (first (Router))
143
- Rinner = _clippedinds (Router,rstp)
144
- # Since most of the points are in the interior, compute them more quickly by avoiding branches
145
- for I in Rinner
146
- out[I] = p[I,1 ] - p[I[1 ]- 1 , I[2 ], 1 ] +
147
- p[I,2 ] - p[I[1 ], I[2 ]- 1 , 2 ]
148
- end
149
- # Handle the edge points
150
- for I in EdgeIterator (Router, Rinner)
151
- out[I] = 0
152
- if I[1 ] == first (inds[1 ])
153
- out[I] += p[I, 1 ]
154
- elseif I[1 ] == last (inds[1 ])
155
- out[I] -= p[I[1 ]- 1 , I[2 ], 1 ]
156
- else
157
- out[I] += p[I,1 ] - p[I[1 ]- 1 , I[2 ], 1 ]
158
- end
159
- if I[2 ] == first (inds[2 ])
160
- out[I] += p[I, 2 ]
161
- elseif I[2 ] == last (inds[2 ])
162
- out[I] -= p[I[1 ], I[2 ]- 1 , 2 ]
163
- else
164
- out[I] += p[I,2 ] - p[I[1 ], I[2 ]- 1 , 2 ]
165
- end
166
- end
167
- out
168
- end
169
-
170
134
"""
171
135
```
172
136
imgr = imROF(img, λ, iterations)
@@ -188,12 +152,12 @@ function imROF(img::AbstractMatrix{T}, λ::Number, iterations::Integer) where T<
188
152
s1, s2 = size (img)
189
153
p = zeros (T, s1, s2, 2 )
190
154
# This iterates Eq. (9) of the Chambolle citation
191
- local u
155
+ u = similar (img)
192
156
τ = 1 / 4 # see 2nd remark after proof of Theorem 3.1.
193
157
for i = 1 : iterations
194
- div_p = div (p )
195
- u = img - λ* div_p # multiply term inside ∇ by -λ. Thm. 3.1 relates this to u via Eq. 7.
196
- grad_u = cat (fdiff (u, dims= 1 , boundary= :zero ), fdiff (u, dims= 2 , boundary= :zero ), dims= 3 )
158
+ div_p = ImageBase . FiniteDiff . fdiv ( view (p, :, :, 1 ), view (p, :, :, 2 ) )
159
+ u . = img - λ* div_p # multiply term inside ∇ by -λ. Thm. 3.1 relates this to u via Eq. 7.
160
+ grad_u = cat (ImageBase . fdiff (u, dims= 1 , boundary= :zero ), ImageBase . fdiff (u, dims= 2 , boundary= :zero ), dims= 3 )
197
161
grad_u_mag = sqrt .(sum (abs2, grad_u, dims= 3 ))
198
162
p .= (p .- (τ/ λ). * grad_u). / (1 .+ (τ/ λ). * grad_u_mag)
199
163
end
0 commit comments