-
Notifications
You must be signed in to change notification settings - Fork 1
/
affscsp.py
564 lines (417 loc) · 19.6 KB
/
affscsp.py
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
"""Affine Scale-Space and Scale-Space Derivative Toolbox for Python
For computing affine Gaussian kernels and affine Gaussian directional
derivative kernels, as well as providing a computationally reasonably
efficient way to compute filter banks of directional derivative responses
over different orders of spatial differentiation, by defining directional
derivative approximation masks of small spatial support, which are to
be applied to image data that has been already smoothed by regular
affine Gaussian kernels.
References:
Lindeberg (1993) Scale-Space Theory in Computer Vision, Springer.
Lindeberg and Garding (1997) "Shape-adapted smoothing in estimation
of 3-D depth cues from affine distortions of local 2-D structure",
Image and Vision Computing 15: 415-434
Lindeberg (2013) "A computational theory of visual receptive fields",
Biological Cybernetics, 107(6): 589-635.
Lindeberg (2021) "Normative theory of visual receptive fields",
Heliyon 7(1): e05897: 1-20.
Relations between the scientific papers and concepts in this code:
Chapter 14 in the book (Lindeberg 1993) and the article
(Lindeberg and Garding 1997) describe the notion of affine Gaussian
scale space, with its closedness property under affine image
transformations, referred to as affine covariance or affine equivariance.
The articles (Lindeberg 2013) and (Lindeberg 2021) demonstrate how
the spatial component of the receptive fields of simple cells in
the primary visual cortex can be well modelled by directional
derivatives of affine Gaussian kernels. In the code below, we
provide functions for generating such kernels, corresponding to
directional derivatives of affine Gaussian kernels and for computing
the effect of convolving images with such kernels.
"""
from math import exp, pi, sqrt, cos, sin
import numpy as np
from scipy.ndimage import correlate
from pyscsp.discscsp import dirdermask
from pyscsp.gaussders import N_from_epsilon_2D
def CxxCxyCyyfromlambda12phi(
lambda1 : float,
lambda2 : float,
phi : float
) -> (float, float, float) :
"""Computes the parameters of spatial covariance matrix Sigma
Sigma = [[Cxx, Cxy],
[Cxy, Cyy]]
given a specification of the eigenvalues lambda1 and lambda2 of the
covariance matrix as well as its orientation.
Reference:
Lindeberg (2013) "A computational theory of visual receptive fields",
Biological Cybernetics, 107(6): 589-635. (See Equation (68).)
"""
Cxx = lambda1 * cos(phi)**2 + lambda2 * sin(phi)**2
Cxy = (lambda1 - lambda2) * cos(phi) * sin(phi)
Cyy = lambda1 * sin(phi)**2 + lambda2 * cos(phi)**2
return Cxx, Cxy, Cyy
def CxxCxyCyyfromsigma12phi(
sigma1 : float,
sigma2 : float,
phi : float
) -> (float, float, float) :
"""Computes the parameters of spatial covariance matrix Sigma
Sigma = [[Cxx, Cxy],
[Cxy, Cyy]]
given a specification of the eigenvalues scale parameter sigma1
and sigma2 (the square roots of the eigenvalues) of the covariance
matrix as well as its orientation.
Reference:
Lindeberg (2013) "A computational theory of visual receptive fields",
Biological Cybernetics, 107(6): 589-635. (See Equation (68).)
"""
lambda1 = sigma1**2
lambda2 = sigma2**2
return CxxCxyCyyfromlambda12phi(lambda1, lambda2, phi)
def sampldirderaffgausskernelfromlambda12phi(
lambda1 : float,
lambda2 : float,
phi : float,
phiorder : int,
orthorder : int,
N : int
) -> np.ndarray :
"""Computes a kernel of size N x N representing the sampled directional
derivative of order phiorder in the direction phi and of order orthorder
in a direction orthogonal to phi.
The kernel is defined as
D_phi^phiorder D_orth^orthorder g(x; Sigma)
for
D_phi = cos phi D_x + sin phi D_y
D_orth = -sin phi D_x + cos phi D_y
where D_phi and D_orth represent the partial derivative operators in the
directions phi and its orthogonal direction orth, respectively.
The Gaussian kernel is, in turn, defined as
g(x; Sigma) = 1/(2 * pi * det Sigma) * exp(-x^T Sigma^(-1) x/2)
with the spatial covariance matrix
Sigma = [[Cxx, Cxy],
[Cxy, Cyy]]
represented by the parameterization
Cxx = lambda1 * cos(phi)**2 + lambda2 * sin(phi)**2
Cxy = (lambda1 - lambda2) * cos(phi) * sin(phi)
Cyy = lambda1 * sin(phi)**2 + lambda2 * cos(phi)**2
Note: You have to determine an appropriate choice of N in a complementary way.
Reference:
Lindeberg (2021) "Normative theory of visual receptive fields",
Heliyon 7(1): e05897: 1-20. (See Equation (23)).
"""
# Generate a grid of spatial coordinates
xbase = np.linspace(-N, N, 2*N + 1)
ybase = np.linspace(-N, N, 2*N + 1)
ybase = - ybase
x, y = np.meshgrid(xbase, ybase, indexing='xy')
# The code below has been autogenerated from Mathematica to C, then first
# ported to Matlab by semi-automatic editing, and then further ported to
# Python by another round of editing.
# Therefore, some of the constructions may seem a bit odd ...
E = exp(1)
if (phiorder == 0) and (orthorder == 0):
return 1 / (2 * np.power(E, \
((lambda2 * x**2 + lambda1 * y**2) * cos(phi)**2 + \
(lambda1 * x**2 + lambda2 * y**2) * sin(phi)**2 - \
(lambda1 - lambda2) * x * y * sin(2*phi)) \
/ (2 * lambda1 * lambda2)) \
* sqrt(lambda1 * lambda2) * pi)
if (phiorder == 1) and (orthorder == 0):
return - (lambda2 * (x * cos(phi) + y * sin(phi))) / \
(2 * np.power(E, \
((lambda2 * x**2 + lambda1 * y**2) * cos(phi)**2 + \
(lambda1 * x**2 + lambda2 * y**2) *sin(phi)**2 - \
(lambda1 - lambda2) * x * y *sin(2*phi)) \
/ (2 * lambda1 * lambda2)) \
* pow(lambda1 *lambda2, 1.5) * pi)
if (phiorder == 0) and (orthorder == 1):
return (-2 * lambda1 * y * cos(phi) + 2 * lambda1 * x * sin(phi)) / \
(4 * np.power(E, \
((lambda2 * x**2 + lambda1 * y**2) * cos(phi)**2 + \
(lambda1 * x**2 + lambda2 * y**2) * sin(phi)**2 - \
(lambda1 - lambda2) * x * y * sin(2*phi)) \
/ (2 * lambda1 * lambda2)) \
* pow(lambda1 * lambda2, 1.5) * pi)
if (phiorder == 2) and (orthorder == 0):
return (-2 * lambda1 + x**2 + y**2 + (x**2 - y**2) * cos(2*phi) + \
2 * x * y * sin(2*phi)) / \
(4 * np.power(E, \
((lambda2 * x**2 + lambda1 * y**2) * cos(phi)**2 + \
(lambda1 * x**2 + lambda2 * y**2) * sin(phi)**2 - \
(lambda1 - lambda2) * x * y * sin(2*phi)) \
/ (2 * lambda1 * lambda2)) \
* pow(lambda1, 2) * sqrt(lambda1 * lambda2) * pi)
if (phiorder == 1) and (orthorder == 1):
return - (lambda2 * (x * cos(phi) + y * sin(phi))) *\
(-2 * lambda1 * y * cos(phi) + 2 * lambda1 * x * sin(phi)) / \
(4 * np.power(E, \
((lambda2 * x**2 + lambda1 * y**2) * cos(phi)**2 + \
(lambda1 * x**2 + lambda2 * y**2) * sin(phi)**2 - \
(lambda1 - lambda2) * x * y * sin(2*phi)) \
/ (2 * lambda1 * lambda2)) \
* pow(lambda1 * lambda2, 1.5) * pi)
if (phiorder == 0) and (orthorder == 2):
return (-2 * lambda2 + x**2 + y**2 + (-x**2 + y**2)*cos(2*phi) \
- 2 * x * y * sin(2*phi)) / \
(4 * np.power(E, \
((lambda2 * x**2 + lambda1 * y**2) * cos(phi)**2 + \
(lambda1 * x**2 + lambda2 * y**2) * sin(phi)**2 - \
(lambda1 - lambda2) * x * y * sin(2*phi)) \
/(2 * lambda1 * lambda2)) \
* pow(lambda2, 2) * sqrt(lambda1 * lambda2) * pi)
raise ValueError(f"Not implemented for phiorder {phiorder} orthorder {orthorder}")
def sampldirderaffgausskernelfromsigma12phi(
sigma1 : float,
sigma2 : float,
phi : float,
phiorder : int,
orthorder : int,
N : int
) -> np.ndarray :
"""Computes a kernel of size N x N representing the sampled directional
derivative of order phiorder in the direction phi and of order orthorder
in a direction orthogonal to phi.
The kernel is defined as
D_phi^phiorder D_orth^orthorder g(x; Sigma)
for .
D_phi = cos phi D_x + sin phi D_y
D_orth = -sin phi D_x + cos phi D_y
where D_phi and D_orth represent the partial derivative operators in the
directions phi and and its orthogonal direction orth, respectively.
The Gaussian kernel is, in turn, defined as
g(x; Sigma) = 1/(2 * pi * det Sigma) * exp(-x^T Sigma^(-1) x/2)
with the spatial covariance matrix
Sigma = [[Cxx, Cxy],
[Cxy, Cyy]]
represented by the parameterization
Cxx = sigma1^2 * cos(phi)**2 + sigma2^2 * sin(phi)**2
Cxy = (sigma1^2 - sigma2^2) * cos(phi) * sin(phi)
Cyy = sigma1^2 * sin(phi)**2 + sigma2^2 * cos(phi)**2
Note: You have to determine an appropriate choice of N in a complementary way.
References:
Lindeberg (2021) "Normative theory of visual receptive fields",
Heliyon 7(1): e05897: 1-20. (See Equation (23)).
"""
lambda1 = sigma1**2
lambda2 = sigma2**2
return sampldirderaffgausskernelfromlambda12phi(lambda1, lambda2, phi, \
phiorder, orthorder, N)
def scnormsampldirderaffgausskernelfromsigma12phi(
sigma1 : float,
sigma2 : float,
phi : float,
phiorder : int,
orthorder : int,
N : int
) -> np.ndarray :
"""Computes a kernel of size N x N representing the sampled directional
derivative of order phiorder in the direction phi and of order orthorder
in a direction orthogonal to phi.
The kernel is defined as
sigma1^phiorder sigma2^orthorder D_phi^phiorder D_orth^orthorder g(x; Sigma)
for
D_phi = cos phi D_x + sin phi D_y
D_orth = -sin phi D_x + cos phi D_y
where D_phi and D_orth represent the partial derivative operators in the
directions phi and and its orthogonal direction orth, respectively.
The Gaussian kernel is, in turn, defined as
g(x; Sigma) = 1/(2 * pi * det Sigma) * exp(-x^T Sigma^(-1) x/2)
with the spatial covariance matrix
Sigma = [[Cxx, Cxy],
[Cxy, Cyy]]
represented by the parameterization
Cxx = sigma1^2 * cos(phi)**2 + sigma2^2 * sin(phi)**2
Cxy = (sigma1^2 - sigma2^2) * cos(phi) * sin(phi)
Cyy = sigma1^2 * sin(phi)**2 + sigma2^2 * cos(phi)**2
Note: You have to determine an appropriate choice of N in a complementary way.
Reference:
Lindeberg (2021) "Normative theory of visual receptive fields",
Heliyon 7(1): e05897: 1-20. (See Equation (31)).
"""
lambda1 = sigma1**2
lambda2 = sigma2**2
scalenormfactor = sigma1**phiorder * sigma2**orthorder
return scalenormfactor * \
sampldirderaffgausskernelfromsigma12phi(lambda1, lambda2, phi, \
phiorder, orthorder, N)
def numdirdersamplaffgausskernel(
sigma1 : float,
sigma2 : float,
phi : float,
phiorder : int,
orthorder : int,
N : int
) -> np.ndarray :
"""Computes a kernel of size N x N representing a numerical approximation
of the directional derivative of order phiorder in the direction phi and
of order orthorder in a direction orthogonal to phi.
The kernel is defined as
D_phi^phiorder D_orth^orthorder g(x; Sigma)
for
D_phi = cos phi D_x + sin phi D_y
D_orth = -sin phi D_x + cos phi D_y
where D_phi and D_orth represent (discrete approximations of) the partial
derivative operators in the directions phi and its orthogonal direction
orth, respectively.
The Gaussian kernel is, in turn, defined as
g(x; Sigma) = 1/(2 * pi * det Sigma) * exp(-x^T Sigma^(-1) x/2)
with the spatial covariance matrix
Sigma = [[Cxx, Cxy],
[Cxy, Cyy]]
represented by the parameterization
Cxx = sigma1^2 * cos(phi)**2 + sigma2^2 * sin(phi)**2
Cxy = (sigma1^2 - sigma2^2) * cos(phi) * sin(phi)
Cyy = sigma1^2 * sin(phi)**2 + sigma2^2 * cos(phi)**2
Reference:
Lindeberg (2021) "Normative theory of visual receptive fields",
Heliyon 7(1): e05897: 1-20. (See Equation (23)).
"""
# ==>> Complement the following code by removal of boundary effects
affgausskernel = samplaffgausskernel(sigma1, sigma2, phi, N)
mask = dirdermask(phi, phiorder, orthorder)
return correlate(affgausskernel, mask)
def samplaffgausskernel(
sigma1 : float,
sigma2 : float,
phi : float,
N : int
) -> np.ndarray :
"""Computes a sampled affine Gaussian kernel of size N x N defined as
g(x; Sigma) = 1/(2 * pi * det Sigma) * exp(-x^T Sigma^(-1) x/2)
with the covariance matrix
Sigma = [[Cxx, Cxy],
[Cxy, Cyy]]
parameterized as
Cxx = sigma1^2 * cos(phi)^2 + sigma2^2 * sin(phi)^2
Cxy = (sigma1^2 - sigma2^2) * cos(phi) * sin(phi)
Cyy = sigma1^2 * sin(phi)^2 + sigma2^2 * cos(phi)^2
References:
Lindeberg (1993b) Scale-Space Theory in Computer Vision, Springer.
Lindeberg and Garding (1997) "Shape-adapted smoothing in estimation
of 3-D depth cues from affine distortions of local 2-D structure",
Image and Vision Computing 15:415-434
"""
return sampldirderaffgausskernelfromsigma12phi(sigma1, sigma2, phi, 0, 0, N)
def samplaffgaussconv(
inpic,
sigma1 : float,
sigma2 : float,
phi : float,
epsilon : int = 0.0001
) -> np.ndarray :
"""Convolves the input image inpic sampled affine Gaussian kernel defined as
g(x; Sigma) = 1/(2 * pi * det Sigma) * exp(-x^T Sigma^(-1) x/2)
with the covariance matrix
Sigma = [[Cxx, Cxy],
[Cxy, Cyy]]
parameterized as
Cxx = sigma1^2 * cos(phi)^2 + sigma2^2 * sin(phi)^2
Cxy = (sigma1^2 - sigma2^2) * cos(phi) * sin(phi)
Cyy = sigma1^2 * sin(phi)^2 + sigma2^2 * cos(phi)^2
and given a bound epsilon on the truncation error at the boundaries,
if the smoothing kernel is truncated at the tails.
"""
# Estimate a size for truncating the Gaussian derivative kernels
N = N_from_epsilon_2D(0, max(sigma1, sigma2), epsilon)
affgaussfilter = samplaffgausskernel(sigma1, sigma2, phi, N)
return correlate(inpic, affgaussfilter)
def applyaffdirder(
smoothpic,
sigma1 : float,
sigma2 : float,
phi : float,
phiorder : int,
orthorder : int,
normdermethod = 'varnorm'
) -> np.ndarray :
"""Applies a directional derivative operator of order phiorder in the direction
phi and of order orthorder in the orthogonal direction, to an image that is
assumed to have been smoothed with an affine Gaussian kernel with standard
deviation sigma1 in the direction phi and standard deviation orthorder
in the orthogonal direction, where the parameter normdermethod specifies
the type of scale normalization to be used for the affine Gaussian derivatives.
"""
if normdermethod == 'varnorm':
mask = scnormaffdirdermask(sigma1, sigma2, phi, phiorder, orthorder)
elif normdermethod == 'nonormalization':
mask = dirdermask(phi, phiorder, orthorder)
else:
raise ValueError(f"Scale normalization method {normdermethod} not implemented")
return correlate(smoothpic, mask)
def scnormaffdirdermask(
sigma1 : float,
sigma2 : float,
phi : float,
phiorder : int,
orthorder : int
) -> np.ndarray :
"""Returns a discrete directional derivative approximation mask, such that
application of this mask to an image smoothed by a zero-order affine Gaussian
kernel (assumed to have been determined using the same values of sigma1,
sigma2 and phi) gives an approximation of the scale-normalized directional
derivative according to
sigma1^phiorder sigma2^orthorder D_phi^phiorder D_orth^orthorder g(x; Sigma)
for
D_phi = cos phi D_x + sin phi D_y
D_orth = -sin phi D_x + cos phi D_y
where D_phi and D_orth represent the partial derivative operators in the
directions phi and its orthogonal direction orth, respectively, where it
is assumed that convolution with g(x; Sigma) is computed outside of this
function, and using the same values of sigma1, sigma2 and phi.
The intention is that the mask returned by this function should be applied
to affine Gaussian smoothed images. Specifically, for an image processing
method that makes use of a filter bank of directional derivatives of
affine Gaussian kernels, the intention is that the computationally heavy
affine Gaussian smoothing operation should be performed only once, and
that different directional derivative approximation masks should then
be applied to the same affine Gaussian smoothed image, thus saving
a substantial amount of work, compared to applying full size affine
Gaussian directional derivative masks for different choices of orders
of the directional derivatives.
Reference:
Lindeberg (2021) "Normative theory of visual receptive fields",
Heliyon 7(1): e05897: 1-20. (See Equation (31)).
"""
scalenormfactor = sigma1**phiorder * sigma2**orthorder
rawmask = dirdermask(phi, phiorder, orthorder)
return scalenormfactor * rawmask
def scnormnumdirdersamplaffgausskernel(
sigma1 : float,
sigma2 : float,
phi : float,
phiorder : int,
orthorder : int,
N : int
) -> np.ndarray :
"""Computes a kernel of size N x N representing the sampled directional
derivative of order phiorder in the direction phi and of order orthorder
in a direction orthogonal to phi.
The kernel is defined as
sigma1^phiorder sigma2^orthorder D_phi^phiorder D_orth^orthorder g(x; Sigma)
for
D_phi = cos phi D_x + sin phi D_y
D_orth = -sin phi D_x + cos phi D_y
where D_phi and D_orth represent (discrete approximations of) the partial
derivative operators in the directions phi and its orthogonal direction orth,
respectively, and with the Gaussian kernel is, in turn, defined as
g(x; Sigma) = 1/(2 * pi * det Sigma) * exp(-x^T Sigma^(-1) x/2)
with the spatial covariance matrix
Sigma = [[Cxx, Cxy],
[Cxy, Cyy]]
represented by the parameterization
Cxx = sigma1^2 * cos(phi)**2 + sigma2^2 * sin(phi)**2
Cxy = (sigma1^2 - sigma2^2) * cos(phi) * sin(phi)
Cyy = sigma1^2 * sin(phi)**2 + sigma2^2 * cos(phi)**2
Note: The intention is not that this function should be used for computing
output from receptive field responses. It is mererly intended for purposes
of graphical illustration of receptive fields.
Reference:
Lindeberg (2021) "Normative theory of visual receptive fields",
Heliyon 7(1): e05897: 1-20. (See Equation (31)).
"""
# ==>> Complement the following code by removal of boundary effects
affgausskernel = samplaffgausskernel(sigma1, sigma2, phi, N)
scnormmask = scnormaffdirdermask(sigma1, sigma2, phi, phiorder, orthorder)
return correlate(affgausskernel, scnormmask)