- Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathconvolve.py
More file actions
Latest commit
269 lines (196 loc) · 6.59 KB
/
Copy pathconvolve.py
File metadata and controls
269 lines (196 loc) · 6.59 KB
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
from __future__ importprint_function
importmath, numpyasnp
fromPILimportImage
#Attempt to use scipy to speed up convolution
useScipyConvolution=False
try:
importscipy.ndimage
useScipyConvolution=True
except:
print("Warning: Failed to import scipy.ndimage")
classConvolutionKernel:
def__init__(self, maxKernelWidth=71):
self.width=None
self.data= [0.foriinrange(maxKernelWidth)]
#*********************************************************************
#* _computeKernels
#*
cachegauss=None
cachegaussderiv=None
cached_sigma_last=None
def_computeKernels(sigma):
maxKernelWidth=71
gauss=ConvolutionKernel(maxKernelWidth)
gaussderiv=ConvolutionKernel(maxKernelWidth)
factor=0.01# for truncating tail
assertmaxKernelWidth%2==1
assertsigma>=0.0
# Compute kernels, and automatically determine widths */
hw=int(maxKernelWidth/2)
max_gauss=1.0
max_gaussderiv=float(sigma*math.exp(-0.5))
# Compute gauss and deriv
foriinrange(-hw,hw+1):
gauss.data[i+hw] =float (math.exp(-i*i/ (2*sigma*sigma)))
gaussderiv.data[i+hw] =-i*gauss.data[i+hw]
# Compute widths
gauss.width=maxKernelWidth;
i=-hw
while(abs(gauss.data[i+hw] /max_gauss) <factor):
i=i+1
gauss.width-=2
gaussderiv.width=maxKernelWidth
i=-hw
while(abs(gaussderiv.data[i+hw] /max_gaussderiv) <factor) :
gaussderiv.width-=2
i=i+1
ifgauss.width==maxKernelWidthorgaussderiv.width==maxKernelWidth:
KLTError("(_computeKernels) maxKernelWidth {0} is too small for a sigma of {1}".format(maxKernelWidth, sigma))
# Shift if width less than maxKernelWidth
foriinrange(gauss.width):
gauss.data[i] =gauss.data[int(i+(maxKernelWidth-gauss.width)/2)]
foriinrange(gaussderiv.width):
gaussderiv.data[i] =gaussderiv.data[int(i+(maxKernelWidth-gaussderiv.width)/2)]
# Normalize gauss and deriv
hw=int(gaussderiv.width/2)
den=0.0;
foriinrange(gauss.width):
den+=gauss.data[i]
foriinrange(gauss.width):
gauss.data[i] /=den
den=0.0
foriinrange(-hw,hw+1):
den-=i*gaussderiv.data[i+hw]
foriinrange(-hw,hw+1):
gaussderiv.data[i+hw] /=den
#Extract the valid portion of the kernel
gauss.data=gauss.data[:gauss.width]
gaussderiv.data=gaussderiv.data[:gaussderiv.width]
globalcachegauss, cachegaussderiv, cached_sigma_last
cachegauss=gauss.data
cachegaussderiv=gaussderiv.data
cached_sigma_last=sigma
returngauss.data, gaussderiv.data
#*********************************************************************
#* KLTGetKernelWidths
#*
#*
defKLTGetKernelWidths(sigma):
gauss_kernel, gaussderiv_kernel=_computeKernels(sigma)
returnlen(gauss_kernel), len(gaussderiv_kernel)
#*********************************************************************
#* _convolveImageHoriz
#*
def_convolveImageHoriz(imgin,kernel):
imgin=Image.fromarray(imgin)
radius=len(kernel) /2
imgout=Image.new("F", imgin.size)
imgoutl=imgout.load()
imginl=imgin.load()
ncols, nrows=imgin.size
# Kernel width must be odd
assertlen(kernel) %2==1
# Must read from and write to different images
#assert(imgin != imgout);
# Output image must be large enough to hold result
#assert imgout->ncols >= imgin->ncols
#assert imgout->nrows >= imgin->nrows
# For each row, do ...
forjinrange(nrows):
# Zero leftmost columns
foriinrange(radius):
imgoutl[i,j] =0.
# Convolve middle columns with kernel
foriinrange(radius,ncols-radius):
sumv=0.0
ind=0
forkinrange(len(kernel)-1,-1,-1):
sumv+=imginl[i+ind-radius,j] *kernel[k]
ind+=1
imgoutl[i,j] =sumv
# Zero rightmost columns
foriinrange(ncols-radius, ncols):
imgoutl[i,j] =0.
returnnp.array(imgout)
#*********************************************************************
#* _convolveImageVert
#*
def_convolveImageVert(imgin, kernel):
imgin=Image.fromarray(imgin)
radius=len(kernel) /2;
imgout=Image.new("F", imgin.size)
imgoutl=imgout.load()
imginl=imgin.load()
ncols, nrows=imgin.size
# Kernel width must be odd
assertlen(kernel) %2==1
# Must read from and write to different images
#assert(imgin != imgout);
# Output image must be large enough to hold result
#assert(imgout->ncols >= imgin->ncols);
#assert(imgout->nrows >= imgin->nrows);
# For each column, do ...
foriinrange(ncols):
# Zero topmost rows
forjinrange(radius):
imgoutl[i,j] =0.
# Convolve middle rows with kernel
forjinrange(radius,nrows-radius):
sumv=0.
ind=0
forkinrange(len(kernel)-1,-1,-1):
sumv+=imginl[i,j+ind-radius] *kernel[k]
ind+=1
imgoutl[i,j] =sumv
# Zero bottommost rows
forjinrange(nrows-radius,nrows):
imgoutl[i,j] =0.
#ptrcol++;
#ptrout -= nrows * ncols - 1;
returnnp.array(imgout)
#*********************************************************************
#* _convolveSeparate
#*
def_convolveSeparate(imgin,horiz_kernel,vert_kernel):
ifuseScipyConvolution:
#Do convolution using scipy (faster)
tmpimg=scipy.ndimage.filters.convolve1d(imgin, horiz_kernel, axis=1)
imgout=scipy.ndimage.filters.convolve1d(tmpimg, vert_kernel, axis=0)
returnimgout
# Do convolution in native code (slower)
tmpimg=_convolveImageHoriz(imgin, horiz_kernel)
imgout=_convolveImageVert(tmpimg, vert_kernel)
returnimgout
#*********************************************************************
#* KLTComputeGradients
#*
defKLTComputeGradients(img, sigma):
# Output images must be large enough to hold result
#assert(gradx->ncols >= img->ncols);
#assert(gradx->nrows >= img->nrows);
#assert(grady->ncols >= img->ncols);
#assert(grady->nrows >= img->nrows);
# Compute kernels, if necessary
globalcachegauss, cachegaussderiv, cached_sigma_last
ifabs(sigma-cached_sigma_last) >0.05:
gauss_kernel, gaussderiv_kernel=_computeKernels(sigma)
else:
gauss_kernel, gaussderiv_kernel=cachegauss, cachegaussderiv
#print(gauss_kernel)
#plt.plot(gauss_kernel)
#plt.show()
gradx=_convolveSeparate(img, gaussderiv_kernel, gauss_kernel)
grady=_convolveSeparate(img, gauss_kernel, gaussderiv_kernel)
returngradx, grady
#*********************************************************************
#* KLTComputeSmoothedImage
#*
defKLTComputeSmoothedImage(img,sigma):
# Compute kernel, if necessary; gauss_deriv is not used
globalcachegauss, cachegaussderiv, cached_sigma_last
ifcached_sigma_lastisNoneorabs(sigma-cached_sigma_last) >0.05:
gauss, gaussderiv=_computeKernels(sigma)
else:
gauss, gaussderiv=cachegauss, cachegaussderiv
smooth=_convolveSeparate(img, gauss, gauss)
returnsmooth