-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
ARTSCENE.jl
364 lines (304 loc) · 11.9 KB
/
ARTSCENE.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
"""
ARTSCENE.jl
Description:
All of the visual filter functions for the ARTSCENE algorithm.
"""
using Distributed
using SharedArrays
"""
color_to_gray(image::RealArray)
ARTSCENE Stage 1: Color-to-gray image transformation.
"""
function color_to_gray(image::RealArray)
# Treat the image as a column-major array, cast to grayscale
dim, n_row, n_column = size(image)
return [sum(image[:,i,j])/3 for i=1:n_row, j=1:n_column]
end # color_to_gray(image::RealArray)
"""
surround_kernel(i::Integer, j::Integer, p::Integer, q::Integer, scale::Integer)
Surround kernel S function for ARTSCENE Stage 2
"""
function surround_kernel(i::Integer, j::Integer, p::Integer, q::Integer, scale::Integer)
return 1/(2*pi*scale^2)*MathConstants.e^(-((i-p)^2 + (j-q)^2)/(2*scale^2))
end # surround_kernel(i::Integer, j::Integer, p::Integer, q::Integer, scale::Integer)
"""
ddt_x(x::RealArray, image::RealArray, sigma_s::RealArray, distributed::Bool)
Time rate of change of LGN network (ARTSCENE Stage 2).
"""
function ddt_x(x::RealArray, image::RealArray, sigma_s::RealArray, distributed::Bool)
n_row, n_column = size(x)
n_g = length(sigma_s)
kernel_r = 5
# dx = zeros(n_row, n_column, 4)
# for g = 1:n_g
if distributed
dx = SharedArray{Float64, 3}((n_row, n_column, n_g))
@sync @distributed for g = 1:n_g
for i = 1:n_row
for j = 1:n_column
# Compute the surround kernel
kernel_h = max(1, i-kernel_r):min(n_row, i + kernel_r)
kernel_w = max(1, j-kernel_r):min(n_row, j + kernel_r)
S_ijg_I = sum([surround_kernel(i, j, p, q, sigma_s[g])*image[p, q]
for p in kernel_h, q in kernel_w])
# Compute the enhanced contrast
dx[i,j,g] = - x[i,j,g] + (1 - x[i,j,g])*image[i,j] - (1 + x[i,j,g])*S_ijg_I
end
end
end
end
return dx
end # ddt_x(x::RealArray, image::RealArray, sigma_s::RealArray, distributed::Bool)
"""
contrast_normalization(image::RealArray ; distributed::Bool=true)
ARTSCENE Stage 2: Constrast normalization.
"""
function contrast_normalization(image::RealArray ; distributed::Bool=true)
# All scale parameters
sigma_s = [1, 4, 8, 12]
n_g = length(sigma_s)
# Number if iterations to settle on contrast result
n_iterations = 4
# Get the shape of the image
n_row, n_column = size(image)
x = zeros(n_row, n_column, n_g)
for g = 1:n_g
x[:,:, g] = deepcopy(image)
end
for i = 1:n_iterations
x += ddt_x(x, image, sigma_s, distributed)
end
return x
end # contrast_normalization(image::RealArray ; distributed::Bool=true)
"""
oriented_kernel(i::Integer, j::Integer, p::Integer, q::Integer, k::Integer, sigma_h::Real, sigma_v::Real ; sign::String="plus")
Oriented, elongated, spatially offset kernel G for ARTSCENE Stage 3.
"""
function oriented_kernel(i::Integer, j::Integer, p::Integer, q::Integer, k::Integer, sigma_h::Real, sigma_v::Real ; sign::String="plus")
m = sin(pi*k/4)
n = cos(pi*k/4)
if sign == "plus"
G = (1/(2*pi*sigma_h*sigma_v)*
MathConstants.e^(-0.5*((((p-i+m)*n-(q-j+n)*m)/sigma_h)^2
+(((p-i+m)*m+(q-j+n)*n)/sigma_v)^2)))
elseif sign == "minus"
G = (1/(2*pi*sigma_h*sigma_v)*
MathConstants.e^(-0.5*((((p-i-m)*n-(q-j-n)*m)/sigma_h)^2
+(((p-i-m)*m+(q-j-n)*n)/sigma_v)^2)))
else
throw("Incorrect sign option for oriented kernel function")
end
return G
end # oriented_kernel(i::Integer, j::Integer, p::Integer, q::Integer, k::Integer, sigma_h::Real, sigma_v::Real ; sign::String="plus")
"""
ddt_y(y::RealArray, X_plus::RealArray, X_minus::RealArray, alpha::Real, distributed::Bool)
Shunting equation for ARTSCENE Stage 3.
"""
function ddt_y(y::RealArray, X_plus::RealArray, X_minus::RealArray, alpha::Real, distributed::Bool)
# n_row, n_column = size(x) # TODO: SOURCE OF WRONGNESS
n_row, n_column = size(y)
n_k = 4
sigma_v = [0.25, 1, 2, 3]
sigma_h = [0.75, 3, 6, 9]
n_g = length(sigma_v)
kernel_r = 5
# dy = zeros(n_row, n_column, n_k, n_g)
# for k = 1:n_k
if distributed
dy = SharedArray{Float64, 4}((n_row, n_column, n_k, n_g))
@sync @distributed for k = 1:n_k
for g = 1:n_g
for i = 1:n_row
for j = 1:n_column
# Compute the surround kernel
kernel_h = max(1, i-kernel_r):min(n_row, i + kernel_r)
kernel_w = max(1, j-kernel_r):min(n_row, j + kernel_r)
Gp = [oriented_kernel(i, j, p, q, k-1, sigma_h[g], sigma_v[g], sign="plus")
for p in kernel_h, q in kernel_w]
Gm = [oriented_kernel(i, j, p, q, k-1, sigma_h[g], sigma_v[g], sign="minus")
for p in kernel_h, q in kernel_w]
dy[i,j,g,k] = (-alpha*y[i,j,g,k]
+ (1-y[i,j,g,k])*sum(X_plus[kernel_h, kernel_w, g].*Gp
+ X_minus[kernel_h, kernel_w, g].*Gm)
- (1+y[i,j,g,k])*sum(X_plus[kernel_h, kernel_w, g].*Gm
+ X_minus[kernel_h, kernel_w, g].*Gp))
end
end
end
end
end
return dy
end # ddt_y(y::RealArray, X_plus::RealArray, X_minus::RealArray, alpha::Real, distributed::Bool)
"""
contrast_sensitive_oriented_filtering(image::RealArray, x::RealArray ; distributed::Bool=true)
ARTSCENE Stage 3: Contrast-sensitive oriented filtering.
"""
function contrast_sensitive_oriented_filtering(image::RealArray, x::RealArray ; distributed::Bool=true)
# Get the size of the field
n_row, n_column = size(x)
# Parameters
n_g = 4 # Number of scales
n_k = 4 # Number of orientations
alpha = 1 # Passive decay rate
n_iterations = 4 # Number if iterations to settle on contrast result
# Compute the LGN ON-cell and OFF-cell output signals
X_plus = [max(0, x[i,j,g]) for i=1:n_row, j=1:n_column, g=1:n_g]
X_minus = [max(0, -x[i,j,g]) for i=1:n_row, j=1:n_column, g=1:n_g]
# Get the shape of the image
n_row, n_column = size(x)
y = zeros(n_row, n_column, n_g, n_k)
for k = 1:n_k
y[:,:,:,k] = deepcopy(x)
end
for _ = 1:n_iterations
y += ddt_y(y, X_plus, X_minus, alpha, distributed)
end
return y
end # contrast_sensitive_oriented_filtering(image::RealArray, x::RealArray ; distributed::Bool=true)
"""
contrast_insensitive_oriented_filtering(y::RealArray)
ARTSCENE Stage 4: Contrast-insensitive oriented filtering.
"""
function contrast_insensitive_oriented_filtering(y::RealArray)
n_row, n_column, n_g, n_k = size(y)
# Compute the LGN ON-cell and OFF-cell output signals
Y_plus = [max(0, y[i,j,g,k]) for i=1:n_row, j=1:n_column, g=1:n_g, k=1:n_k]
Y_minus = [max(0, -y[i,j,g,k]) for i=1:n_row, j=1:n_column, g=1:n_g, k=1:n_k]
return Y_plus + Y_minus
end # contrast_insensitive_oriented_filtering(y::RealArray)
"""
competition_kernel(l::Integer, k::Integer ; sign::String="plus")
Competition kernel for ARTSCENE: Stage 5.
"""
function competition_kernel(l::Integer, k::Integer ; sign::String="plus")
if sign == "plus"
g = ( 1/(0.5*sqrt(2*pi))*MathConstants.e^(-0.5*((l-k)/0.5)^2) )
elseif sign == "minus"
g = ( 1/(sqrt(2*pi))*MathConstants.e^(-0.5*(l-k)^2) )
else
throw("Incorrect sign option for oriented kernel function")
end
return g
end # competition_kernel(l::Integer, k::Integer ; sign::String="plus")
"""
ddt_z(z::RealArray ; distributed=true)
Time rate of change for ARTSCENE: Stage 5.
"""
function ddt_z(z::RealArray ; distributed::Bool=true)
n_row, n_column, n_g, n_k = size(z)
kernel_r = 5
# dz = zeros(n_row, n_column, n_k, n_g)
# for k = 1:n_k
if distributed
dz = SharedArray{Float64, 4}((n_row, n_column, n_k, n_g))
@sync @distributed for k = 1:n_k
for g = 1:n_g
for i = 1:n_row
for j = 1:n_column
zgp = sum([z[i,j,g,l]*competition_kernel(l,k,sign="plus") for l = 1:n_g])
zgm = sum([z[i,j,g,l]*competition_kernel(l,k,sign="minus") for l = 1:n_g])
dz[i,j,g,k] = (- z[i,j,g,k]
+ (1 - z[i,j,g,k]*zgp)
- (1 + z[i,j,g,k]*zgm))
end
end
end
end
end
return dz
end # ddt_z(z::RealArray ; distributed=true)
"""
orientation_competition(z::RealArray)
ARTSCENE Stage 5: Orientation competition at the same position.
"""
function orientation_competition(z::RealArray)
# Parameters
n_iterations = 4 # Number if iterations to settle on contrast result
# Get the shape of the image
# n_row, n_column, n_g, n_k = size(z)
# Z = zeros(n_row, n_column, n_g, n_k)
# for k = 1:n_k
# Z[:,:,:,k] = deepcopy(z)
# end
for _ = 1:n_iterations
z += ddt_z(z)
end
return z
end
"""
patch_orientation_color(z::RealArray, image::RealArray)
ARTSCENE Stage 6: Create patch feature vectors.
"""
function patch_orientation_color(z::RealArray, image::RealArray)
n_i, n_j, n_g, n_k = size(z)
patch_i = 4
patch_j = 4
n_colors = 3
n_patches = patch_i * patch_j
size_i = n_i / patch_i
size_j = n_j / patch_j
size_patch = size_i * size_j
O = zeros(patch_i, patch_j, n_g, n_k)
C = zeros(patch_i, patch_j, n_colors)
for p_i = 1:patch_i
for p_j = 1:patch_j
# Get the correct range objects for the grid
i_range = Integer(floor(size_i*(p_i-1)+1)):Integer(floor(size_i*p_i))
j_range = Integer(floor(size_j*(p_j-1)+1)):Integer(floor(size_j*p_j))
# Compute the color averages
for c = 1:n_colors
C[p_i,p_j,c] = 1/size_patch*sum(image[c, i_range, j_range])
end
# Compute the orientation averages
for k = 1:4
for g = 1:4
O[p_i,p_j,k,g] = 1/size_patch * sum(z[i_range, j_range, k, g])
end
end
end
end
return O, C
end # patch_orientation_color(z::RealArray, image::RealArray)
"""
artscene_filter(raw_image::Array{T, 3} ; distributed::Bool=true) where {T<:Real}
Process the full artscene filter toolchain on an image.
"""
function artscene_filter(raw_image::Array{T, 3} ; distributed::Bool=true) where {T<:Real}
# Get the number of workers
n_processes = nprocs()
n_workers = nworkers()
@debug "Processes: $n_processes, Workers: $n_workers"
# Random image
image_size = size(raw_image)
image_type = typeof(raw_image)
@debug "Original: Size = $image_size, Type = $image_type"
# Stage 1: Grayscale
image = color_to_gray(raw_image)
image_size = size(image)
image_type = typeof(image)
@debug "Stage 1 Complete: Grayscale: Size = $image_size, Type = $image_type"
# Stage 2: Contrast normalization
x = contrast_normalization(image, distributed=true)
image_size = size(x)
image_type = typeof(x)
@debug "Stage 2 Complete: Contrast: Size = $image_size, Type = $image_type"
# Stage 3: Contrast-sensitive oriented filtering
y = contrast_sensitive_oriented_filtering(image, x)
image_size = size(y)
image_type = typeof(y)
@debug "Stage 3 Complete: Sensitive Oriented: Size = $image_size, Type = $image_type"
# Stage 4: Contrast-insensitive oriented filtering
z = contrast_insensitive_oriented_filtering(y)
image_size = size(z)
image_type = typeof(z)
@debug "Stage 4 Complete: Insensitive Oriented: Size = $image_size, Type = $image_type"
# Stage 5: Orientation competition
z = orientation_competition(z)
image_size = size(z)
image_type = typeof(z)
@debug "Stage 5 Complete: Orientation Competition: Size = $image_size, Type = $image_type"
# *Stage 6*: Compute patch vectors (orientation and color)
O, C = patch_orientation_color(z, raw_image)
@debug "Stage 6 Complete"
return O, C
end # artscene_filter(raw_image::Array{T, 3} ; distributed::Bool=true) where {T<:Real}