-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnoborder.py
183 lines (150 loc) · 5.46 KB
/
noborder.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
from array import array
class NoBorderImage(object):
"An image class for people who dont care about border effects"
def __init__(self, w, h, typecode='d', fromfile=None):
self.width = w
self.height = h
if fromfile is not None:
self.data = array(typecode)
self.data.fromfile(fromfile, w*h)
else:
self.data = array(typecode, [0]) * (w*h)
self.typecode = typecode
def _idx(self, p):
if isinstance(p, Pixel):
assert p.image.__class__ is self.__class__
assert p.image.width == self.width
idx = p.idx
else:
idx = p[1] * self.width + p[0]
return min(max(idx, 0), len(self.data)-1)
def __getitem__(self, p):
return self.data[self._idx(p)]
def __setitem__(self, p, val):
self.data[self._idx(p)] = val
def pixels(self):
for i in self.pixelrange():
yield Pixel(i, self)
def pixeliter(self):
return PixelIter(self)
def pixelrange(self):
return xrange(self.width * self.height)
def setup(self, data):
for y in xrange(self.height):
for x in xrange(self.width):
self[x, y] = data[y][x]
return self
def clone(self, **kwargs):
return self.__class__(self.width, self.height, **kwargs)
def tofile(self, f):
self.data.tofile(f)
class NoBorderImagePadded(NoBorderImage):
def __init__(self, w, h, typecode='d', fromfile=None):
self.width = w
self.height = h
self.typecode = typecode
if fromfile is None:
self.data = array(typecode, [0]) * (w*(h+2)+2)
else:
self.data = array(typecode, [0]) * (w + 1)
self.data.fromfile(fromfile, w*h)
self.data += array(typecode, [0]) * (w + 1)
def _idx(self, p):
if isinstance(p, Pixel):
assert p.image.__class__ is self.__class__
assert p.image.width == self.width
idx = p.idx
else:
idx = (p[1]+1) * self.width + p[0] + 1
return min(max(idx, 0), len(self.data)-1)
def pixelrange(self):
return xrange(self.width + 1, (self.width+1) * self.height + 1)
def tofile(self, f):
self.data[(self.width+1):(-self.width-1)].tofile(f)
class NoBorderImagePadded640x480(NoBorderImagePadded):
def _idx(self, p):
assert self.width == 640
assert self.height == 480
assert len(self.data) == 640*(480+2)+2
return NoBorderImagePadded._idx(self, p)
class Pixel(object):
def __init__(self, idx, image):
self.idx = idx
self.image = image
def __add__(self, other):
return Pixel(self.idx + other[1]*self.image.width + other[0], self.image)
class PixelIter(object):
def __init__(self, image):
self.image = image
self.pixelrange = iter(image.pixelrange())
def __iter__(self):
return self
def next(self):
return Pixel(self.pixelrange.next(), self.image)
def conv3x3(img, k):
assert k.width == k.height == 3
res = img.clone()
for p in img.pixels():
res[p] = k[2,2]*img[p + (-1,-1)] + k[1,2]*img[p + (0,-1)] + k[0,2]*img[p + (1,-1)] + \
k[2,1]*img[p + (-1, 0)] + k[1,1]*img[p + (0, 0)] + k[0,1]*img[p + (1, 0)] + \
k[2,0]*img[p + (-1, 1)] + k[1,0]*img[p + (0, 1)] + k[0,0]*img[p + (1, 1)]
return res
def conv3x3iter(img, k):
assert k.width == k.height == 3
res = img.clone()
for p in img.pixeliter():
res[p] = k[2,2]*img[p + (-1,-1)] + k[1,2]*img[p + (0,-1)] + k[0,2]*img[p + (1,-1)] + \
k[2,1]*img[p + (-1, 0)] + k[1,1]*img[p + (0, 0)] + k[0,1]*img[p + (1, 0)] + \
k[2,0]*img[p + (-1, 1)] + k[1,0]*img[p + (0, 1)] + k[0,0]*img[p + (1, 1)]
return res
def conv3x3range(img, k):
assert k.width == k.height == 3
res = img.clone()
for i in img.pixelrange():
p = Pixel(i, img)
res[p] = k[2,2]*img[p + (-1,-1)] + k[1,2]*img[p + (0,-1)] + k[0,2]*img[p + (1,-1)] + \
k[2,1]*img[p + (-1, 0)] + k[1,1]*img[p + (0, 0)] + k[0,1]*img[p + (1, 0)] + \
k[2,0]*img[p + (-1, 1)] + k[1,0]*img[p + (0, 1)] + k[0,0]*img[p + (1, 1)]
return res
def main(args):
Image = eval(args[0])
if len(args) == 1:
func = conv3x3
else:
func = eval('conv3x3' + args[1])
n = 1000
for i in range(10):
func(Image(n, n), Image(3, 3))
if len(args) > 1:
return 'conv3x3%s(%s(%dx%d))' % (args[1], Image.__name__, n, n)
else:
return Image.__name__
if __name__ == '__main__':
import time, sys
sys.setcheckinterval(2**30)
try:
import pypyjit
pypyjit.set_param(trace_limit=200000)
except ImportError:
pass
Image = eval(sys.argv[1])
n = 1000
# Warmup
conv3x3(Image(n, n), Image(3,3))
conv3x3iter(Image(n, n), Image(3,3))
conv3x3range(Image(n, n), Image(3,3))
a = time.time()
for i in range(10):
conv3x3(Image(n, n), Image(3,3))
b = time.time()
print 'conv3x3(%s(%dx%d)):' % (Image.__name__, n, n), b - a
a = time.time()
for i in range(10):
conv3x3iter(Image(n, n), Image(3,3))
b = time.time()
print 'conv3x3iter(%s(%dx%d)):' % (Image.__name__, n, n), b - a
a = time.time()
for i in range(10):
conv3x3range(Image(n, n), Image(3,3))
b = time.time()
print 'conv3x3range(%s(%dx%d)):' % (Image.__name__, n, n), b - a