alignfaces 1.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- alignfaces/__init__.py +15 -0
- alignfaces/aperture_tools.py +213 -0
- alignfaces/contrast_tools.py +106 -0
- alignfaces/contrast_tools_.py +106 -0
- alignfaces/data/shape_predictor_68_face_landmarks.dat +0 -0
- alignfaces/face_landmarks.py +233 -0
- alignfaces/make_aligned_faces.py +1217 -0
- alignfaces/make_aligned_faces_.py +1209 -0
- alignfaces/make_files.py +42 -0
- alignfaces/make_files_.py +42 -0
- alignfaces/make_files_OLD.py +86 -0
- alignfaces/phase_cong_3.py +524 -0
- alignfaces/plot_tools.py +170 -0
- alignfaces/procrustes_tools.py +217 -0
- alignfaces/tests/R/align_reference.csv +1 -0
- alignfaces/tests/R/align_shapes.csv +40 -0
- alignfaces/tests/R/input_shapes.csv +40 -0
- alignfaces/tests/__init__.py +0 -0
- alignfaces/tests/_test_pawarp.py +267 -0
- alignfaces/tests/test_procrustes_tools.py +569 -0
- alignfaces/tests/test_warp_tools.py +316 -0
- alignfaces/warp_tools.py +279 -0
- alignfaces-1.0.1.dist-info/METADATA +135 -0
- alignfaces-1.0.1.dist-info/RECORD +27 -0
- alignfaces-1.0.1.dist-info/WHEEL +5 -0
- alignfaces-1.0.1.dist-info/licenses/LICENSE.txt +13 -0
- alignfaces-1.0.1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,524 @@
|
|
|
1
|
+
import math
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
import cv2 # Faster Fourier transforms than NumPy and Scikit-Image
|
|
5
|
+
|
|
6
|
+
# References:
|
|
7
|
+
#
|
|
8
|
+
# Peter Kovesi, "Image Features From Phase Congruency". Videre: A
|
|
9
|
+
# Journal of Computer Vision Research. MIT Press. Volume 1, Number 3,
|
|
10
|
+
# Summer 1999 http://mitpress.mit.edu/e-journals/Videre/001/v13.html
|
|
11
|
+
#
|
|
12
|
+
# Peter Kovesi, "Phase Congruency Detects Corners and
|
|
13
|
+
# Edges". Proceedings DICTA 2003, Sydney Dec 10-12
|
|
14
|
+
|
|
15
|
+
# Copyright (c) 1996-2017 Peter Kovesi
|
|
16
|
+
# Centre for Exploration Targeting
|
|
17
|
+
# The University of Western Australia
|
|
18
|
+
# peter.kovesi at uwa edu au
|
|
19
|
+
#
|
|
20
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
21
|
+
# of this software and associated documentation files (the "Software"), to deal
|
|
22
|
+
# in the Software without restriction, subject to the following conditions:
|
|
23
|
+
#
|
|
24
|
+
# The above copyright notice and this permission notice shall be included in
|
|
25
|
+
# all copies or substantial portions of the Software.
|
|
26
|
+
#
|
|
27
|
+
# The Software is provided "as is", without warranty of any kind.
|
|
28
|
+
|
|
29
|
+
# Usage in UpToBrowFit.py
|
|
30
|
+
#
|
|
31
|
+
# from phasecong3 import filtered_images_centered_on_critical_band_and_wavelet_bank
|
|
32
|
+
#
|
|
33
|
+
# output = filtered_images_centered_on_critical_band_and_wavelet_bank(face, foCpf, facesPerImage, NumberScales, NumberAngles)
|
|
34
|
+
#
|
|
35
|
+
# NumberScales should be odd (choose 3 to match wavelet features used for correlation experiments)
|
|
36
|
+
# NumberAngles (choose 6 to match ...)
|
|
37
|
+
|
|
38
|
+
def filtered_images_centered_on_critical_band_and_wavelet_bank(InputImage, CriticalBandCyclesPerObject, ObjectsPerImage, NumberScales, NumberAngles):
|
|
39
|
+
nrows, ncols = InputImage.shape
|
|
40
|
+
|
|
41
|
+
# Matrix of radii
|
|
42
|
+
y, x = np.mgrid[0:nrows, 0:ncols]
|
|
43
|
+
#cy = int(math.ceil((nrows-1)/2))
|
|
44
|
+
#cx = int(math.ceil((ncols-1)/2))
|
|
45
|
+
cy = math.floor(nrows/2)
|
|
46
|
+
cx = math.floor(ncols/2)
|
|
47
|
+
y = y - cy
|
|
48
|
+
x = x - cx
|
|
49
|
+
radius = np.sqrt(x**2 + y**2)
|
|
50
|
+
radius[cy, cx] = 1
|
|
51
|
+
|
|
52
|
+
# Matrix values contain polar angle.
|
|
53
|
+
# (note -ve y is used to give +ve anti-clockwise angles)
|
|
54
|
+
theta = np.arctan2(-y, x)
|
|
55
|
+
sintheta = np.sin(theta)
|
|
56
|
+
costheta = np.cos(theta)
|
|
57
|
+
|
|
58
|
+
# Initialise set of annular bandpass filters
|
|
59
|
+
# Here I use the method of scale selection from the code I used to generate
|
|
60
|
+
# stimuli for my latest experiments (spatial feature scaling):
|
|
61
|
+
# /Users/carl/Studies/Face_Projects/features_wavelet
|
|
62
|
+
#NumberScales = 3 # should be odd
|
|
63
|
+
mult = 1.66 # see Peter Kovesi's plotgaborfilters.m
|
|
64
|
+
annularBandpassFilters = np.empty((nrows,ncols,NumberScales))
|
|
65
|
+
p = np.arange(NumberScales) - math.floor(NumberScales/2)
|
|
66
|
+
fSetCpo = CriticalBandCyclesPerObject*mult**p
|
|
67
|
+
fSetCpi = fSetCpo * ObjectsPerImage
|
|
68
|
+
|
|
69
|
+
# Number of filter orientations.
|
|
70
|
+
#NumberAngles = 6
|
|
71
|
+
""" Ratio of angular interval between filter orientations and the standard deviation
|
|
72
|
+
of the angular Gaussian function used to construct filters in the freq. plane.
|
|
73
|
+
"""
|
|
74
|
+
dThetaOnSigma = 1.3
|
|
75
|
+
filterOrient = np.arange(start=0, stop=math.pi - math.pi / NumberAngles, step = math.pi / NumberAngles)
|
|
76
|
+
|
|
77
|
+
# The standard deviation of the angular Gaussian function used to construct filters in the frequency plane.
|
|
78
|
+
thetaSigma = math.pi / NumberAngles / dThetaOnSigma;
|
|
79
|
+
|
|
80
|
+
BandpassFilters = np.empty((nrows,ncols,NumberScales,NumberAngles))
|
|
81
|
+
evenWavelets = np.empty((nrows,ncols,NumberScales,NumberAngles))
|
|
82
|
+
oddWavelets = np.empty((nrows,ncols,NumberScales,NumberAngles))
|
|
83
|
+
|
|
84
|
+
# The following implements the log-gabor transfer function
|
|
85
|
+
""" From http://www.peterkovesi.com/matlabfns/phase_congruency/Docs/convexpl.html
|
|
86
|
+
The filter bandwidth is set by specifying the ratio of the standard deviation
|
|
87
|
+
of the Gaussian describing the log Gabor filter's transfer function in the
|
|
88
|
+
log-frequency domain to the filter center frequency. This is set by the parameter
|
|
89
|
+
sigmaOnf . The smaller sigmaOnf is the larger the bandwidth of the filter.
|
|
90
|
+
I have not worked out an expression relating sigmaOnf to bandwidth, but
|
|
91
|
+
empirically a sigmaOnf value of 0.75 will result in a filter with a bandwidth
|
|
92
|
+
of approximately 1 octave and a value of 0.55 will result in a bandwidth of
|
|
93
|
+
roughly 2 octaves.
|
|
94
|
+
"""
|
|
95
|
+
# sigmaOnf = 0.74 # approximately 1 octave
|
|
96
|
+
sigmaOnf = 0.55 # approximately 2 octaves
|
|
97
|
+
""" From Wilson, Loffler and Wilkinson (2002 Vision Research):
|
|
98
|
+
The bandpass filtering alluded to above was used because of ubiquitous evidence
|
|
99
|
+
that face discrimination is optimal within a 2.0 octave (at half amplitude)
|
|
100
|
+
bandwidth centered upon 8–13 cycles per face width (Costen et al., 1996;
|
|
101
|
+
Fiorentini et al., 1983; Gold et al., 1999; Hayes et al., 1986; Näsänen, 1999).
|
|
102
|
+
We therefore chose a radially symmetric filter with a peak frequency of 10.0
|
|
103
|
+
cycles per mean face width and a 2.0 octave bandwidth described by a difference
|
|
104
|
+
of Gaussians (DOG):"""
|
|
105
|
+
|
|
106
|
+
plotColors = ['b','r','k']
|
|
107
|
+
for i in np.arange(3):
|
|
108
|
+
logGabor = np.exp( (-(np.log(radius/fSetCpi[i]))**2) / (2 * math.log(sigmaOnf)**2) )
|
|
109
|
+
logGabor[cy, cx] = 0 # Set the value at the 0 frequency point
|
|
110
|
+
# of the filter back to zero
|
|
111
|
+
# (undo the radius fudge).
|
|
112
|
+
|
|
113
|
+
# Lowpass filter to remove high frequency 'garbage'
|
|
114
|
+
cutoff = 0.4
|
|
115
|
+
filterorder = 10 # filter 'sharpness'
|
|
116
|
+
normradius = radius / (abs(x).max()*2)
|
|
117
|
+
lowpassbutterworth = 1.0 / (1.0 + (normradius / cutoff)**(2*filterorder))
|
|
118
|
+
annularBandpassFilters[:,:,i] = logGabor * lowpassbutterworth
|
|
119
|
+
|
|
120
|
+
fgain = annularBandpassFilters[cy,cx+1:,i]
|
|
121
|
+
for ai in np.arange(NumberAngles):
|
|
122
|
+
angl = filterOrient[ai]
|
|
123
|
+
""" For each point in the filter matrix calculate the angular distance from the
|
|
124
|
+
specified filter orientation. To overcome the angular wrap-around problem
|
|
125
|
+
sine difference and cosine difference values are first computed and then
|
|
126
|
+
the atan2 function is used to determine angular distance.
|
|
127
|
+
"""
|
|
128
|
+
ds = sintheta * math.cos(angl) - costheta * math.sin(angl); # Difference in sine.
|
|
129
|
+
dc = costheta * math.cos(angl) + sintheta * math.sin(angl); # Difference in cosine.
|
|
130
|
+
dtheta = np.abs(np.arctan2(ds,dc)); # Absolute angular distance.
|
|
131
|
+
spread = np.exp((-dtheta**2) / (2 * thetaSigma**2)); # Calculate the angular
|
|
132
|
+
# filter component.
|
|
133
|
+
|
|
134
|
+
BandpassFilters[:,:,i,ai] = spread * annularBandpassFilters[:,:,i]; # Product of the two components.
|
|
135
|
+
temp = np.empty((nrows, ncols, 2))
|
|
136
|
+
for i in range(2):
|
|
137
|
+
temp[:,:,i] = BandpassFilters[:,:,i,ai]
|
|
138
|
+
complexImage = cv2.idft(np.fft.ifftshift(temp))
|
|
139
|
+
evenWavelets[:,:,i,ai] = complexImage[:,:,0]
|
|
140
|
+
oddWavelets[:,:,i,ai] = complexImage[:,:,1]
|
|
141
|
+
|
|
142
|
+
"""
|
|
143
|
+
Output so far:
|
|
144
|
+
annularBandpassFilters.
|
|
145
|
+
|
|
146
|
+
Use annularBandpassFilters[:,:,1] for contrast islands of eyes, nose and mouth.
|
|
147
|
+
Use annularBandpassFilters[:,:,0] for eyebrow detection.
|
|
148
|
+
|
|
149
|
+
BandpassFilters. Use shifted versions for Fourier plane filtering to select high-amplitude points.
|
|
150
|
+
evenWavelets & oddWavelets for reconstruction!
|
|
151
|
+
"""
|
|
152
|
+
|
|
153
|
+
# Images filtered at center frequencies critical for recognition.
|
|
154
|
+
# To be thresholded for determination of local contrast islands.
|
|
155
|
+
# In conjunction with facial landmarks.
|
|
156
|
+
filteredFaces = np.empty((nrows,ncols,NumberScales))
|
|
157
|
+
f_cv = cv2.dft(np.float32(InputImage),flags=cv2.DFT_COMPLEX_OUTPUT)
|
|
158
|
+
for fi in range(NumberScales):
|
|
159
|
+
criticalfiltershift = np.fft.ifftshift( annularBandpassFilters[:,:,fi] )
|
|
160
|
+
criticalfiltershift_cv = np.empty((nrows, ncols, 2))
|
|
161
|
+
for i in range(2):
|
|
162
|
+
criticalfiltershift_cv[:,:,i] = criticalfiltershift
|
|
163
|
+
|
|
164
|
+
# TEST - according to my notes (fourier_demo.py) should be [:,:,1]
|
|
165
|
+
filteredFaces[:,:,fi] = cv2.idft( criticalfiltershift_cv * f_cv)[:,:,0]
|
|
166
|
+
|
|
167
|
+
return filteredFaces
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
# based on phasecong3.m
|
|
171
|
+
def phase_congruency(InputImage, NumberScales, NumberAngles):
|
|
172
|
+
|
|
173
|
+
# nscale 4 - Number of wavelet scales, try values 3-6
|
|
174
|
+
# norient 6 - Number of filter orientations.
|
|
175
|
+
# minWaveLength 3 - Wavelength of smallest scale filter.
|
|
176
|
+
# mult 2.1 - Scaling factor between successive filters.
|
|
177
|
+
# sigmaOnf 0.55 - Ratio of the standard deviation of the Gaussian
|
|
178
|
+
# describing the log Gabor filter's transfer function
|
|
179
|
+
# in the frequency domain to the filter center frequency.
|
|
180
|
+
# k 2.0 - No of standard deviations of the noise energy beyond
|
|
181
|
+
# the mean at which we set the noise threshold point.
|
|
182
|
+
# You may want to vary this up to a value of 10 or
|
|
183
|
+
# 20 for noisy images
|
|
184
|
+
# cutOff 0.5 - The fractional measure of frequency spread
|
|
185
|
+
# below which phase congruency values get penalized.
|
|
186
|
+
# g 10 - Controls the sharpness of the transition in
|
|
187
|
+
# the sigmoid function used to weight phase
|
|
188
|
+
# congruency for frequency spread.
|
|
189
|
+
# noiseMethod -1 - Parameter specifies method used to determine
|
|
190
|
+
# noise statistics.
|
|
191
|
+
# -1 use median of smallest scale filter responses
|
|
192
|
+
# -2 use mode of smallest scale filter responses
|
|
193
|
+
# 0+ use noiseMethod value as the fixed noise threshold
|
|
194
|
+
minWaveLength = 3
|
|
195
|
+
mult = 2.1
|
|
196
|
+
sigmaOnf = 0.55
|
|
197
|
+
k = 2.0
|
|
198
|
+
cutOff = 0.5
|
|
199
|
+
g = 10
|
|
200
|
+
noiseMethod = -1
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
epsilon = .0001 # Used to prevent division by zero.
|
|
204
|
+
|
|
205
|
+
# [rows,cols] = size(im);
|
|
206
|
+
# imagefft = fft2(im); % Fourier transform of image
|
|
207
|
+
#
|
|
208
|
+
# zero = zeros(rows,cols);
|
|
209
|
+
# EO = cell(nscale, norient); % Array of convolution results.
|
|
210
|
+
# PC = cell(norient,1);
|
|
211
|
+
# covx2 = zero; % Matrices for covariance data
|
|
212
|
+
# covy2 = zero;
|
|
213
|
+
# covxy = zero;
|
|
214
|
+
#
|
|
215
|
+
# EnergyV = zeros(rows,cols,3); % Matrix for accumulating total energy
|
|
216
|
+
# % vector, used for feature orientation
|
|
217
|
+
# % and type calculation
|
|
218
|
+
#
|
|
219
|
+
# pcSum = zeros(rows,cols);
|
|
220
|
+
|
|
221
|
+
f_cv = cv2.dft(np.float32(InputImage),flags=cv2.DFT_COMPLEX_OUTPUT)
|
|
222
|
+
|
|
223
|
+
#------------------------------
|
|
224
|
+
nrows, ncols = InputImage.shape
|
|
225
|
+
zero = np.zeros((nrows,ncols))
|
|
226
|
+
EO = np.zeros((nrows,ncols,NumberScales,NumberAngles),dtype=complex)
|
|
227
|
+
PC = np.zeros((nrows,ncols,NumberAngles))
|
|
228
|
+
covx2 = np.zeros((nrows,ncols))
|
|
229
|
+
covy2 = np.zeros((nrows,ncols))
|
|
230
|
+
covxy = np.zeros((nrows,ncols))
|
|
231
|
+
EnergyV = np.zeros((nrows,ncols,3))
|
|
232
|
+
pcSum = np.zeros((nrows,ncols))
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
# Matrix of radii
|
|
237
|
+
cy = math.floor(nrows/2)
|
|
238
|
+
cx = math.floor(ncols/2)
|
|
239
|
+
y, x = np.mgrid[0:nrows, 0:ncols]
|
|
240
|
+
y = (y-cy)/nrows
|
|
241
|
+
x = (x-cx)/ncols
|
|
242
|
+
|
|
243
|
+
# y = y - cy
|
|
244
|
+
# x = x - cx
|
|
245
|
+
radius = np.sqrt(x**2 + y**2)
|
|
246
|
+
radius[cy, cx] = 1
|
|
247
|
+
|
|
248
|
+
# Matrix values contain polar angle.
|
|
249
|
+
# (note -ve y is used to give +ve anti-clockwise angles)
|
|
250
|
+
theta = np.arctan2(-y, x)
|
|
251
|
+
sintheta = np.sin(theta)
|
|
252
|
+
costheta = np.cos(theta)
|
|
253
|
+
|
|
254
|
+
# Initialise set of annular bandpass filters
|
|
255
|
+
# Here I use the method of scale selection from the code I used to generate
|
|
256
|
+
# stimuli for my latest experiments (spatial feature scaling):
|
|
257
|
+
# /Users/carl/Studies/Face_Projects/features_wavelet
|
|
258
|
+
#NumberScales = 3 # should be odd
|
|
259
|
+
annularBandpassFilters = np.empty((nrows,ncols,NumberScales))
|
|
260
|
+
#p = np.arange(NumberScales) - math.floor(NumberScales/2)
|
|
261
|
+
#fSetCpo = CriticalBandCyclesPerObject*mult**p
|
|
262
|
+
#fSetCpi = fSetCpo * ObjectsPerImage
|
|
263
|
+
|
|
264
|
+
# Number of filter orientations.
|
|
265
|
+
#NumberAngles = 6
|
|
266
|
+
""" Ratio of angular interval between filter orientations and the standard deviation
|
|
267
|
+
of the angular Gaussian function used to construct filters in the freq. plane.
|
|
268
|
+
"""
|
|
269
|
+
dThetaOnSigma = 1.3
|
|
270
|
+
filterOrient = np.arange(start=0, stop=math.pi - math.pi / NumberAngles, step = math.pi / NumberAngles)
|
|
271
|
+
|
|
272
|
+
# The standard deviation of the angular Gaussian function used to construct filters in the frequency plane.
|
|
273
|
+
thetaSigma = math.pi / NumberAngles / dThetaOnSigma;
|
|
274
|
+
|
|
275
|
+
BandpassFilters = np.empty((nrows,ncols,NumberScales,NumberAngles))
|
|
276
|
+
evenWavelets = np.empty((nrows,ncols,NumberScales,NumberAngles))
|
|
277
|
+
oddWavelets = np.empty((nrows,ncols,NumberScales,NumberAngles))
|
|
278
|
+
|
|
279
|
+
# The following implements the log-gabor transfer function
|
|
280
|
+
""" From http://www.peterkovesi.com/matlabfns/phase_congruency/Docs/convexpl.html
|
|
281
|
+
The filter bandwidth is set by specifying the ratio of the standard deviation
|
|
282
|
+
of the Gaussian describing the log Gabor filter's transfer function in the
|
|
283
|
+
log-frequency domain to the filter center frequency. This is set by the parameter
|
|
284
|
+
sigmaOnf . The smaller sigmaOnf is the larger the bandwidth of the filter.
|
|
285
|
+
I have not worked out an expression relating sigmaOnf to bandwidth, but
|
|
286
|
+
empirically a sigmaOnf value of 0.75 will result in a filter with a bandwidth
|
|
287
|
+
of approximately 1 octave and a value of 0.55 will result in a bandwidth of
|
|
288
|
+
roughly 2 octaves.
|
|
289
|
+
"""
|
|
290
|
+
# sigmaOnf = 0.74 # approximately 1 octave
|
|
291
|
+
# sigmaOnf = 0.55 # approximately 2 octaves
|
|
292
|
+
""" From Wilson, Loffler and Wilkinson (2002 Vision Research):
|
|
293
|
+
The bandpass filtering alluded to above was used because of ubiquitous evidence
|
|
294
|
+
that face discrimination is optimal within a 2.0 octave (at half amplitude)
|
|
295
|
+
bandwidth centered upon 8–13 cycles per face width (Costen et al., 1996;
|
|
296
|
+
Fiorentini et al., 1983; Gold et al., 1999; Hayes et al., 1986; Näsänen, 1999).
|
|
297
|
+
We therefore chose a radially symmetric filter with a peak frequency of 10.0
|
|
298
|
+
cycles per mean face width and a 2.0 octave bandwidth described by a difference
|
|
299
|
+
of Gaussians (DOG):"""
|
|
300
|
+
|
|
301
|
+
# Lowpass filter to remove high frequency 'garbage'
|
|
302
|
+
filterorder = 15 # filter 'sharpness'
|
|
303
|
+
cutoff = .45
|
|
304
|
+
normradius = radius / (abs(x).max()*2)
|
|
305
|
+
lowpassbutterworth = 1.0 / (1.0 + (normradius / cutoff)**(2*filterorder))
|
|
306
|
+
#
|
|
307
|
+
# Note: lowpassbutterworth is currently DC centered.
|
|
308
|
+
#
|
|
309
|
+
#
|
|
310
|
+
#annularBandpassFilters[:,:,i] = logGabor * lowpassbutterworth
|
|
311
|
+
#logGabor = np.empty((nrows,ncols,NumberScales)) --> same as annularBandpassFilters
|
|
312
|
+
for s in np.arange(NumberScales):
|
|
313
|
+
wavelength = minWaveLength*mult**s
|
|
314
|
+
fo = 1.0/wavelength # Centre frequency of filter.
|
|
315
|
+
logGabor = np.exp((-(np.log(radius/fo))**2) / (2 * math.log(sigmaOnf)**2))
|
|
316
|
+
annularBandpassFilters[:,:,s] = logGabor*lowpassbutterworth # Apply low-pass filter
|
|
317
|
+
annularBandpassFilters[cy,cx,s] = 0 # Set the value at the 0 frequency point of the filter
|
|
318
|
+
# back to zero (undo the radius fudge).
|
|
319
|
+
|
|
320
|
+
# main loop
|
|
321
|
+
for o in np.arange(NumberAngles):
|
|
322
|
+
# Construct the angular filter spread function
|
|
323
|
+
angl = o*math.pi/NumberAngles # Filter angle.
|
|
324
|
+
# For each point in the filter matrix calculate the angular distance from
|
|
325
|
+
# the specified filter orientation. To overcome the angular wrap-around
|
|
326
|
+
# problem sine difference and cosine difference values are first computed
|
|
327
|
+
# and then the atan2 function is used to determine angular distance.
|
|
328
|
+
# ds = sintheta * cos(angl) - costheta * sin(angl); % Difference in sine.
|
|
329
|
+
# dc = costheta * cos(angl) + sintheta * sin(angl); % Difference in cosine.
|
|
330
|
+
# dtheta = abs(atan2(ds,dc)); % Absolute angular distance.
|
|
331
|
+
|
|
332
|
+
# % Scale theta so that cosine spread function has the right wavelength and clamp to pi
|
|
333
|
+
# dtheta = min(dtheta*norient/2,pi);
|
|
334
|
+
# % The spread function is cos(dtheta) between -pi and pi. We add 1,
|
|
335
|
+
# % and then divide by 2 so that the value ranges 0-1
|
|
336
|
+
# spread = (cos(dtheta)+1)/2;
|
|
337
|
+
#
|
|
338
|
+
# sumE_ThisOrient = zero; % Initialize accumulator matrices.
|
|
339
|
+
# sumO_ThisOrient = zero;
|
|
340
|
+
# sumAn_ThisOrient = zero;
|
|
341
|
+
# Energy = zero;
|
|
342
|
+
#angl = filterOrient[o]
|
|
343
|
+
""" For each point in the filter matrix calculate the angular distance from the
|
|
344
|
+
specified filter orientation. To overcome the angular wrap-around problem
|
|
345
|
+
sine difference and cosine difference values are first computed and then
|
|
346
|
+
the atan2 function is used to determine angular distance.
|
|
347
|
+
"""
|
|
348
|
+
ds = sintheta * math.cos(angl) - costheta * math.sin(angl) # Difference in sine.
|
|
349
|
+
dc = costheta * math.cos(angl) + sintheta * math.sin(angl) # Difference in cosine.
|
|
350
|
+
dtheta = np.abs(np.arctan2(ds,dc)) # Absolute angular distance.
|
|
351
|
+
|
|
352
|
+
# Scale theta so that cosine spread function has the right wavelength
|
|
353
|
+
# and clamp to pi
|
|
354
|
+
dtheta = np.minimum(dtheta*NumberAngles/2, math.pi)
|
|
355
|
+
|
|
356
|
+
#spread = np.exp((-dtheta**2) / (2 * thetaSigma**2)); # Calculate the angular
|
|
357
|
+
# filter component.
|
|
358
|
+
# The spread function is cos(dtheta) between -pi and pi. We add 1,
|
|
359
|
+
# and then divide by 2 so that the value ranges 0-1
|
|
360
|
+
spread = (np.cos(dtheta)+1)/2
|
|
361
|
+
|
|
362
|
+
sumE_ThisOrient = np.zeros((nrows,ncols)) # Initialize accumulator matrices.
|
|
363
|
+
sumO_ThisOrient = np.zeros((nrows,ncols))
|
|
364
|
+
sumAn_ThisOrient = np.zeros((nrows,ncols))
|
|
365
|
+
Energy = np.zeros((nrows,ncols))
|
|
366
|
+
|
|
367
|
+
maxAn = []
|
|
368
|
+
for s in np.arange(NumberScales):
|
|
369
|
+
filter = annularBandpassFilters[:,:,s] * spread # Multiply radial and angular
|
|
370
|
+
# components to get the filter.
|
|
371
|
+
|
|
372
|
+
criticalfiltershift = np.fft.ifftshift( filter )
|
|
373
|
+
criticalfiltershift_cv = np.empty((nrows, ncols, 2))
|
|
374
|
+
for ip in range(2):
|
|
375
|
+
criticalfiltershift_cv[:,:,ip] = criticalfiltershift
|
|
376
|
+
|
|
377
|
+
# Convolve image with even and odd filters returning the result in EO
|
|
378
|
+
MatrixEO = cv2.idft( criticalfiltershift_cv * f_cv )
|
|
379
|
+
EO[:,:,s,o] = MatrixEO[:,:,1] + 1j*MatrixEO[:,:,0]
|
|
380
|
+
|
|
381
|
+
An = cv2.magnitude(MatrixEO[:,:,0], MatrixEO[:,:,1]) # Amplitude of even & odd filter response.
|
|
382
|
+
|
|
383
|
+
sumAn_ThisOrient = sumAn_ThisOrient + An # Sum of amplitude responses.
|
|
384
|
+
sumE_ThisOrient = sumE_ThisOrient + MatrixEO[:,:,1] # Sum of even filter convolution results.
|
|
385
|
+
sumO_ThisOrient = sumO_ThisOrient + MatrixEO[:,:,0] # Sum of odd filter convolution results.
|
|
386
|
+
|
|
387
|
+
# At the smallest scale estimate noise characteristics from the
|
|
388
|
+
# distribution of the filter amplitude responses stored in sumAn.
|
|
389
|
+
# tau is the Rayleigh parameter that is used to describe the
|
|
390
|
+
# distribution.
|
|
391
|
+
if s == 0:
|
|
392
|
+
# if noiseMethod == -1 # Use median to estimate noise statistics
|
|
393
|
+
tau = np.median(sumAn_ThisOrient) / math.sqrt(math.log(4))
|
|
394
|
+
# elseif noiseMethod == -2 # Use mode to estimate noise statistics
|
|
395
|
+
# tau = rayleighmode(sumAn_ThisOrient(:));
|
|
396
|
+
# end
|
|
397
|
+
maxAn = An
|
|
398
|
+
else:
|
|
399
|
+
# Record maximum amplitude of components across scales. This is needed
|
|
400
|
+
# to determine the frequency spread weighting.
|
|
401
|
+
maxAn = np.maximum(maxAn,An)
|
|
402
|
+
# end
|
|
403
|
+
|
|
404
|
+
# complete scale loop
|
|
405
|
+
# next section within mother (orientation) loop
|
|
406
|
+
#
|
|
407
|
+
# Accumulate total 3D energy vector data, this will be used to
|
|
408
|
+
# determine overall feature orientation and feature phase/type
|
|
409
|
+
EnergyV[:,:,0] = EnergyV[:,:,0] + sumE_ThisOrient
|
|
410
|
+
EnergyV[:,:,1] = EnergyV[:,:,1] + math.cos(angl)*sumO_ThisOrient
|
|
411
|
+
EnergyV[:,:,2] = EnergyV[:,:,2] + math.sin(angl)*sumO_ThisOrient
|
|
412
|
+
|
|
413
|
+
# Get weighted mean filter response vector, this gives the weighted mean
|
|
414
|
+
# phase angle.
|
|
415
|
+
XEnergy = np.sqrt(sumE_ThisOrient**2 + sumO_ThisOrient**2) + epsilon
|
|
416
|
+
MeanE = sumE_ThisOrient / XEnergy
|
|
417
|
+
MeanO = sumO_ThisOrient / XEnergy
|
|
418
|
+
|
|
419
|
+
# Now calculate An(cos(phase_deviation) - | sin(phase_deviation)) | by
|
|
420
|
+
# using dot and cross products between the weighted mean filter response
|
|
421
|
+
# vector and the individual filter response vectors at each scale. This
|
|
422
|
+
# quantity is phase congruency multiplied by An, which we call energy.
|
|
423
|
+
|
|
424
|
+
for s in np.arange(NumberScales):
|
|
425
|
+
# Extract even and odd convolution results.
|
|
426
|
+
E = EO[:,:,s,o].real
|
|
427
|
+
O = EO[:,:,s,o].imag
|
|
428
|
+
|
|
429
|
+
Energy = Energy + E*MeanE + O*MeanO - np.abs(E*MeanO - O*MeanE)
|
|
430
|
+
|
|
431
|
+
## Automatically determine noise threshold
|
|
432
|
+
#
|
|
433
|
+
# Assuming the noise is Gaussian the response of the filters to noise will
|
|
434
|
+
# form Rayleigh distribution. We use the filter responses at the smallest
|
|
435
|
+
# scale as a guide to the underlying noise level because the smallest scale
|
|
436
|
+
# filters spend most of their time responding to noise, and only
|
|
437
|
+
# occasionally responding to features. Either the median, or the mode, of
|
|
438
|
+
# the distribution of filter responses can be used as a robust statistic to
|
|
439
|
+
# estimate the distribution mean and standard deviation as these are related
|
|
440
|
+
# to the median or mode by fixed constants. The response of the larger
|
|
441
|
+
# scale filters to noise can then be estimated from the smallest scale
|
|
442
|
+
# filter response according to their relative bandwidths.
|
|
443
|
+
#
|
|
444
|
+
# This code assumes that the expected reponse to noise on the phase congruency
|
|
445
|
+
# calculation is simply the sum of the expected noise responses of each of
|
|
446
|
+
# the filters. This is a simplistic overestimate, however these two
|
|
447
|
+
# quantities should be related by some constant that will depend on the
|
|
448
|
+
# filter bank being used. Appropriate tuning of the parameter 'k' will
|
|
449
|
+
# allow you to produce the desired output.
|
|
450
|
+
|
|
451
|
+
# if noiseMethod >= 0: % We are using a fixed noise threshold
|
|
452
|
+
# T = noiseMethod; % use supplied noiseMethod value as the threshold
|
|
453
|
+
# else:
|
|
454
|
+
# Estimate the effect of noise on the sum of the filter responses as
|
|
455
|
+
# the sum of estimated individual responses (this is a simplistic
|
|
456
|
+
# overestimate). As the estimated noise response at succesive scales
|
|
457
|
+
# is scaled inversely proportional to bandwidth we have a simple
|
|
458
|
+
# geometric sum.
|
|
459
|
+
totalTau = tau * (1 - (1/mult)**NumberScales)/(1-(1/mult))
|
|
460
|
+
|
|
461
|
+
# Calculate mean and std dev from tau using fixed relationship
|
|
462
|
+
# between these parameters and tau. See
|
|
463
|
+
# http://mathworld.wolfram.com/RayleighDistribution.html
|
|
464
|
+
EstNoiseEnergyMean = totalTau*math.sqrt(math.pi/2) # Expected mean and std
|
|
465
|
+
EstNoiseEnergySigma = totalTau*math.sqrt((4-math.pi)/2) # values of noise energy
|
|
466
|
+
|
|
467
|
+
T = EstNoiseEnergyMean + k*EstNoiseEnergySigma # Noise threshold
|
|
468
|
+
# end
|
|
469
|
+
|
|
470
|
+
# Apply noise threshold, this is effectively wavelet denoising via
|
|
471
|
+
# soft thresholding.
|
|
472
|
+
Energy = np.maximum(Energy - T, 0)
|
|
473
|
+
|
|
474
|
+
# Form weighting that penalizes frequency distributions that are
|
|
475
|
+
# particularly narrow. Calculate fractional 'width' of the frequencies
|
|
476
|
+
# present by taking the sum of the filter response amplitudes and dividing
|
|
477
|
+
# by the maximum amplitude at each point on the image. If
|
|
478
|
+
# there is only one non-zero component width takes on a value of 0, if
|
|
479
|
+
# all components are equal width is 1.
|
|
480
|
+
width = (sumAn_ThisOrient/(maxAn + epsilon) - 1) / (NumberScales-1)
|
|
481
|
+
|
|
482
|
+
# Now calculate the sigmoidal weighting function for this orientation.
|
|
483
|
+
weight = 1.0 / (1 + np.exp( (cutOff - width)*g))
|
|
484
|
+
|
|
485
|
+
# Apply weighting to energy and then calculate phase congruency
|
|
486
|
+
PC[:,:,o] = weight*Energy/sumAn_ThisOrient # Phase congruency for this orientatio
|
|
487
|
+
|
|
488
|
+
pcSum = pcSum + PC[:,:,o]
|
|
489
|
+
|
|
490
|
+
# Build up covariance data for every point
|
|
491
|
+
covx = PC[:,:,o]*math.cos(angl)
|
|
492
|
+
covy = PC[:,:,o]*math.sin(angl)
|
|
493
|
+
covx2 = covx2 + covx**2
|
|
494
|
+
covy2 = covy2 + covy**2
|
|
495
|
+
covxy = covxy + covx*covy
|
|
496
|
+
# above everyting within orientaiton loop
|
|
497
|
+
# ------------------------------------------------------------------------
|
|
498
|
+
# current work
|
|
499
|
+
# Edge and Corner calculations
|
|
500
|
+
# The following is optimised code to calculate principal vector
|
|
501
|
+
# of the phase congruency covariance data and to calculate
|
|
502
|
+
# the minimumum and maximum moments - these correspond to
|
|
503
|
+
# the singular values.
|
|
504
|
+
|
|
505
|
+
# First normalise covariance values by the number of orientations/2
|
|
506
|
+
covx2 = covx2/(NumberAngles/2)
|
|
507
|
+
covy2 = covy2/(NumberAngles/2)
|
|
508
|
+
covxy = 4*covxy/NumberAngles # This gives us 2*covxy/(norient/2)
|
|
509
|
+
denom = np.sqrt(covxy**2 + (covx2-covy2)**2)+epsilon
|
|
510
|
+
M = (covy2+covx2 + denom)/2 # Maximum moment
|
|
511
|
+
m = (covy2+covx2 - denom)/2 # ... and minimum moment
|
|
512
|
+
|
|
513
|
+
# Orientation and feature phase/type computation
|
|
514
|
+
ORM = np.arctan2(EnergyV[:,:,2], EnergyV[:,:,1])
|
|
515
|
+
ORM[ORM<0] = ORM[ORM<0]+math.pi # Wrap angles -pi..0 to 0..pi
|
|
516
|
+
ORM = np.round(ORM*180/math.pi) # Orientation in degrees between 0 and 180
|
|
517
|
+
|
|
518
|
+
OddV = np.sqrt(EnergyV[:,:,1]**2 + EnergyV[:,:,2]**2)
|
|
519
|
+
featType = np.arctan2(EnergyV[:,:,0], OddV) # Feature phase pi/2 <-> white line,
|
|
520
|
+
# 0 <-> step, -pi/2 <-> black line
|
|
521
|
+
# ------------------------------------------------------------------------
|
|
522
|
+
|
|
523
|
+
#return M, m, ORM, EO, T, annularBandpassFilters, lowpassbutterworth
|
|
524
|
+
return M, m, EO
|