/
pyramid_constraint.py
63 lines (51 loc) · 1.8 KB
/
pyramid_constraint.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
import cv2
import numpy
class Pyramid:
"gaussian pyramid de- and re-construction"
def __init__(self, level=7, size=(1920,1200)):
self.level = level
self.size = size
def down_size(self):
divisor = float(pow(2, self.level))
w = int(numpy.ceil(self.size[0] / divisor))
h = int(numpy.ceil(self.size[1] / divisor))
return (w,h)
def down(self, im):
"reduce `im' to self.level"
for iteration in range(self.level):
im = cv2.pyrDown(im)
return im
def up(self, im):
"assume `im' is at self.level & restore"
for level in reversed(range(self.level)):
# make sure we get back to the original size exactly
divisor = float(pow(2, level))
w = int(numpy.ceil(self.size[0] / divisor))
h = int(numpy.ceil(self.size[1] / divisor))
if len(im.shape)>2:
shape = (h,w,im.shape[2])
else:
shape = (h,w)
z = numpy.zeros(shape, dtype=numpy.uint8)
cv2.pyrUp(im, z, (w,h))
im = z
return im
class Constraint:
"constraint generation & iteration s.t. no pixel has two ``on'' edges"
def __init__(self, size=(15,10)):
self.size = size
self._modidx = 0
def last(self):
self._modidx = (self._modidx - 1) % 5
return self.next()
def prev(self):
self._modidx = (self._modidx - 2) % 5
return self.next()
def next(self):
"return a constraint that should limit deltaCorrection"
c = numpy.zeros((self.size[1], self.size[0]), dtype=numpy.bool)
for idx in range(self.size[1]):
i = (2*idx + self._modidx) % 5
c[idx,i::5] = True
self._modidx = (self._modidx + 1) % 5
return c