/
down_sampler.pyx
439 lines (393 loc) · 14.2 KB
/
down_sampler.pyx
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
# -*- coding: utf-8 -*-
#
# Project: Image downsampler
# https://github.com/kif/imagizer
#
# Copyright (C) 2014
#
# Principal author:
# Jérôme Kieffer (Jerome.Kieffer@ESRF.eu)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""Implementation of a separable 2D convolution"""
__authors__ = ["Jerome Kieffer"]
__contact__ = "Jerome.kieffer@terre-adelie.org"
__date__ = "06/12/2014"
__status__ = "stable"
__license__ = "GPLv3+"
import cython
import numpy
cimport numpy
from cython.parallel import prange
from cython cimport view
from libc.math cimport round, fabs, floor
import time
def timeit(func):
def wrapper(*arg, **kw):
'''This is the docstring of timeit:
a decorator that logs the execution time'''
t1 = time.time()
res = func(*arg, **kw)
t2 = time.time()
if "func_name" in dir(func):
name = func.func_name
else:
name = str(func)
print("%s took %.3fs" % (name, t2 - t1))
return res
wrapper.__name__ = func.__name__
wrapper.__doc__ = func.__doc__
return wrapper
@cython.cdivision(True)
@cython.boundscheck(False)
@cython.wraparound(False)
def horizontal_convolution(float[:, :] img, float[:] filter):
"""
Implements a 1D horizontal convolution with a filter.
The only implemented mode is "reflect" (default in scipy.ndimage.filter)
@param img: input image
@param filter: 1D array with the coefficients of the array
@return: array of the same shape as image with
"""
cdef:
int FILTER_SIZE, HALF_FILTER_SIZE
int IMAGE_H, IMAGE_W
int x, y, pos, fIndex, newpos, c
float sum, err, val, tmp
numpy.ndarray[numpy.float32_t, ndim = 2] output
FILTER_SIZE = filter.shape[0]
if FILTER_SIZE % 2 == 1:
HALF_FILTER_SIZE = (FILTER_SIZE) // 2
else:
HALF_FILTER_SIZE = (FILTER_SIZE + 1) // 2
IMAGE_H = img.shape[0]
IMAGE_W = img.shape[1]
output = numpy.zeros((IMAGE_H, IMAGE_W), dtype=numpy.float32)
for y in prange(IMAGE_H, nogil=True):
for x in range(IMAGE_W):
sum = 0.0
err = 0.0
for fIndex in range(FILTER_SIZE):
newpos = x + fIndex - HALF_FILTER_SIZE
if newpos < 0:
newpos = - newpos - 1
elif newpos >= IMAGE_W:
newpos = 2 * IMAGE_W - newpos - 1
# sum += img[y,newpos] * filter[fIndex]
# implement Kahan summation
val = img[y, newpos] * filter[fIndex] - err
tmp = sum + val
err = (tmp - sum) - val
sum = tmp
output[y, x] += sum
return output
@cython.cdivision(True)
@cython.boundscheck(False)
@cython.wraparound(False)
def vertical_convolution(float[:, :] img, float[:] filter):
"""
Implements a 1D vertical convolution with a filter.
The only implemented mode is "reflect" (default in scipy.ndimage.filter)
@param img: input image
@param filter: 1D array with the coefficients of the array
@return: array of the same shape as image with
"""
cdef:
int FILTER_SIZE, HALF_FILTER_SIZE
int IMAGE_H, IMAGE_W
int x, y, pos, fIndex, newpos, c
float sum, err, val, tmp
numpy.ndarray[numpy.float32_t, ndim=2] output
FILTER_SIZE = filter.shape[0]
if FILTER_SIZE % 2 == 1:
HALF_FILTER_SIZE = (FILTER_SIZE) // 2
else:
HALF_FILTER_SIZE = (FILTER_SIZE + 1) // 2
IMAGE_H = img.shape[0]
IMAGE_W = img.shape[1]
output = numpy.zeros((IMAGE_H, IMAGE_W), dtype=numpy.float32)
for y in prange(IMAGE_H, nogil=True):
for x in range(IMAGE_W):
sum = 0.0
err = 0.0
for fIndex in range(FILTER_SIZE):
newpos = y + fIndex - HALF_FILTER_SIZE
if newpos < 0:
newpos = - newpos - 1
elif newpos >= IMAGE_H:
newpos = 2 * IMAGE_H - newpos - 1
# sum += img[y,newpos] * filter[fIndex]
# implement Kahan summation
val = img[newpos, x] * filter[fIndex] - err
tmp = sum + val
err = (tmp - sum) - val
sum = tmp
output[y, x] += sum
return output
def gaussian(sigma, width=None, half=True):
"""
Return a Gaussian window of length "width" with standard-deviation "sigma".
@param sigma: standard deviation sigma
@param width: length of the windows (int) By default 6*sigma+1,
Width should be odd for half==False.
The FWHM is 2*sqrt(2 * pi)*sigma
"""
if width is None:
if half:
width = int(3 * sigma)
else:
width = int(6 * sigma + 1)
if width % 2 == 0:
width += 1
sigma = float(sigma)
if half:
x = numpy.linspace(0, 3 * sigma, width)
else:
x = numpy.linspace(-3 * sigma, 3 * sigma, width)
g = numpy.exp(-(x / sigma) ** 2 / 2.0)
return g
def lanczos(order=3, width=32, half=True):
"""
Returns the lanczos function
"""
if half:
x = numpy.linspace(0, order, width)
else:
x = numpy.linspace(-order, order, width)
l = numpy.sinc(x)*numpy.sinc(x/order)
return l
def gaussian_filter(img, sigma):
"""
Performs a gaussian bluring using a gaussian kernel.
@param img: input image
@param sigma:
"""
raw = numpy.ascontiguousarray(img, dtype=numpy.float32)
gauss = gaussian(sigma).astype(numpy.float32)
res = vertical_convolution(horizontal_convolution(raw, gauss), gauss)
return res
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
cdef inline void _point_horizontal(int x, int y, numpy.uint8_t[:,:,:] img, numpy.uint8_t[:,:,:] output, float factor, float order, float[:] data) nogil:
cdef:
float[4] sum
int c = 0
float norm = 0.0
int fIndex = 0
int newpos = 0
int pos_coef = 0
float coef = 0.0
int HALF_FILTER_SIZE = <int> round(factor * order)
int IMAGE_H = img.shape[0]
int IMAGE_W = img.shape[1]
int COLORS = img.shape[2]
int OUTPUT_H = output.shape[0]
int OUTPUT_W = output.shape[1]
int SIZE = data.shape[0]
for c in range(COLORS):
sum[c] = 0.0
for fIndex in range(-HALF_FILTER_SIZE + 1, HALF_FILTER_SIZE):
newpos = <int> round(factor * x + fIndex)
pos_coef = <int> round(fabs(fIndex) / factor / order * SIZE)
if pos_coef>=SIZE:
continue
else:
coef = data[pos_coef]
#mirror
if newpos < 0:
newpos = - newpos - 1
elif newpos >= IMAGE_W:
newpos = 2 * IMAGE_W - newpos - 1
norm += coef
for c in range(COLORS):
sum[c] += img[y,newpos,c] * coef
for c in range(COLORS):
coef = round(sum[c]/norm)
if coef >= 255.0:
output[y, x, c] = 255
else:
output[y, x, c] = <numpy.uint8_t> coef
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
cdef inline void _point_vertical(int x, int y, numpy.uint8_t[:,:,:] img, numpy.uint8_t[:,:,:] output, float factor, float order, float[:] data) nogil:
cdef:
float[4] sum
int c = 0
float norm = 0.0
int fIndex = 0
int newpos = 0
int pos_coef = 0
float coef = 0.0
int HALF_FILTER_SIZE = <int> round(factor * order)
int IMAGE_H = img.shape[0]
int IMAGE_W = img.shape[1]
int COLORS = img.shape[2]
int OUTPUT_H = output.shape[0]
int OUTPUT_W = output.shape[1]
int SIZE = data.shape[0]
for c in range(COLORS):
sum[c] = 0.0
for fIndex in range(-HALF_FILTER_SIZE + 1,HALF_FILTER_SIZE):
newpos = <int> round(factor * y + fIndex)
pos_coef = <int> round(fabs(fIndex) / factor / order * SIZE)
if pos_coef >= SIZE:
continue
else:
coef = data[pos_coef]
if newpos < 0:
continue
#newpos = - newpos - 1
elif newpos >= IMAGE_H:
continue
#newpos = 2 * IMAGE_H - newpos - 1
norm += coef
for c in range(COLORS):
sum[c] = sum[c] + img[newpos, x, c] * coef
for c in range(COLORS):
coef = round(sum[c]/norm)
if coef >= 255.0:
output[y, x, c] = 255
else:
output[y, x, c] = <numpy.uint8_t> coef
class DownScaler(object):
"""
Anti-aliased down-sampler
"""
def __init__(self, func="lanczos", order=3, size=32 ):
self.order = order
self.size = size
self.func = func
if func == "lanczos":
self.data = lanczos(order, size, half=True).astype("float32")
else:
self.data = gaussian(order/3.0, size, half=True).astype("float32")
@timeit
def scale(self, raw not None, float factor=3):
"""
Scale should be greater than 1 for downscaling
"""
if raw.ndim==3:
colors = raw.shape[2]
in_shape = raw.shape[:2]
else:
in_shape = raw.shape
colors = 1
raw = raw.reshape(in_shape[0], in_shape[1], 1)
out_shape = tuple([int(floor(i//factor)) for i in in_shape])
tmp = numpy.zeros(shape=(in_shape[0],out_shape[1],colors), dtype=numpy.uint8)
out = numpy.zeros(shape=(out_shape[0],out_shape[1],colors), dtype=numpy.uint8)
self._horizontal_scale(raw, tmp, factor)
self._vertical_scale(tmp, out, factor)
if colors==1:
return out.reshape(out_shape[0], out_shape[1])
else:
return out
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
def _horizontal_scale(self, numpy.uint8_t[:,:,:] img, numpy.uint8_t[:,:,:] output, float factor):
"""
scale an image dim0, dim1 -> dim0, dim1/factor
note dim2 is the color channel
"""
cdef:
int x, y
float ORDER = float(self.order)
float[:] data=self.data
int IMAGE_H = img.shape[0]
int OUTPUT_W = output.shape[1]
for y in prange(IMAGE_H, nogil=True):
for x in range(OUTPUT_W):
_point_horizontal(x, y, img, output, factor, ORDER, data)
# for c in range(COLORS):
# sum[c] = 0.0
# norm = 0.0
# for fIndex in range(-HALF_FILTER_SIZE + 1, HALF_FILTER_SIZE):
# newpos = <int> round(factor * x + fIndex)
#
# pos_coef = <int> round(fabs(fIndex) / factor / ORDER * SIZE)
# if pos_coef>=SIZE:
## with gil:
## print("Warning H: idx=%s, hfs=%s, w=%s, order=%s"%(fIndex,HALF_FILTER_SIZE,SIZE,ORDER))
# coef = 0
# else:
# coef = data[pos_coef]
#
# if newpos < 0:
# newpos = - newpos - 1
# elif newpos >= IMAGE_W:
# newpos = 2 * IMAGE_W - newpos - 1
#
# norm = norm + coef
# for c in range(COLORS):
# sum[c] = sum[c] + img[y,newpos,c] * coef
# for c in range(COLORS):
# coef = round(sum[c]/norm)
# if coef>255.0:
## with gil:
## print("Warning V: sum=%s, norm=%s"%(coef,norm))
# coef=255.0
# output[y, x, c] += <numpy.uint8_t> coef
return output
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
def _vertical_scale(self, numpy.uint8_t[:,:,:] img, numpy.uint8_t[:,:,:] output, float factor):
"""
scale an image dim0, dim1 -> dim0/factor
note dim2 is the color channel
"""
cdef:
int x, y
float ORDER = float(self.order)
float[:] data=self.data
int IMAGE_H = img.shape[0]
int IMAGE_W = img.shape[1]
int OUTPUT_H = output.shape[0]
for y in prange(OUTPUT_H, nogil=True):
for x in range(IMAGE_W):
_point_vertical(x, y, img, output, factor, ORDER, data)
# for c in range(COLORS):
# sum[c] = 0.0
# norm = 0.0
# for fIndex in range(-HALF_FILTER_SIZE + 1,HALF_FILTER_SIZE):
# newpos = <int> round(factor * y + fIndex)
#
# pos_coef = <int> round(fabs(fIndex) / factor / ORDER * SIZE)
# if pos_coef>=SIZE:
## with gil:
## print("Warning V: idx=%s, hfs=%s, w=%s, order=%s"%(fIndex,HALF_FILTER_SIZE,SIZE,ORDER))
# coef = 0
# else:
# coef = data[pos_coef]
#
# if newpos < 0:
# newpos = - newpos - 1
# elif newpos >= IMAGE_H:
# newpos = 2 * IMAGE_H - newpos - 1
#
# norm = norm + coef
# for c in range(COLORS):
# sum[c] = sum[c] + img[newpos, x, c] * coef
# for c in range(COLORS):
# coef = round(sum[c]/norm)
# if coef>255.0:
## with gil:
## print("Warning V: sum=%s, norm=%s"%(coef,norm))
# coef=255.0
# output[y, x, c] += <numpy.uint8_t> coef
return output