-
Notifications
You must be signed in to change notification settings - Fork 0
/
kernel.py
71 lines (61 loc) · 1.89 KB
/
kernel.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
import numpy
import math
import linear
import util
import scipy.ndimage.filters
def kernel(shape, f):
ar = numpy.empty(shape, dtype=numpy.complex)
for i in numpy.ndindex(shape):
ar[i] = f(*i)
return ar
def map(f, kern):
newkern = numpy.empty(kern.shape, dtype=numpy.complex)
for i in numpy.ndindex(kern.shape):
newkern[i] = f(kern[i])
return newkern
def concatmap(fs, kern):
fshape = fs.shape
kshape = kern.shape
newshape = util.tupleop(lambda x, y: x * y, fshape, kshape)
newkern = numpy.zeros(newshape)
for i in numpy.ndindex(kshape):
x = kern[i]
result = map(lambda f: f(x), fs)
for j in numpy.ndindex(fshape):
newi = util.tupleop(lambda ii, ij, fs: ij + ii * fs, i, j, fshape)
newkern[newi] = result
return newkern
def gaussian(mu, sigma, *v):
A = 1 / (sigma * math.sqrt(2 * math.pi))
tau = (2 * (sigma ** 2))
shift = util.tupleop(lambda x, m: x - m, v, mu)
t = sum(util.tupleop(lambda s: s * s.conjugate(), shift))
return A * math.exp(- t / tau)
def transform(mat, *t):
tex = (1,) + t
vector = numpy.array([tex]).T
return tuple(util.join((mat * vector).tolist()))
def convolve(image, kern):
imgreal = reals(image)
kernreal = reals(kern)
imgimag = imags(image)
kernimag = imags(kern)
f = scipy.ndimage.filters.convolve
outputreal = complexes(f(imgreal, kernreal) - f(imgimag, kernimag))
outputimag = complexes(f(imgreal, kernimag) + f(imgimag, kernreal))*1j
return outputreal + outputimag
#return scipy.ndimage.filters.convolve(image, kern)
def correlate(image, kern):
return scipy.ndimage.filters.correlate(image, kern)
def reals(kern):
newkern = numpy.empty(kern.shape, dtype=float)
for i in numpy.ndindex(kern.shape):
newkern[i] = kern[i].real
return newkern
def imags(kern):
newkern = numpy.empty(kern.shape, dtype=float)
for i in numpy.ndindex(kern.shape):
newkern[i] = kern[i].imag
return newkern
def complexes(kern):
return numpy.array(kern, dtype=complex)