This repository has been archived by the owner on May 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhw6.py
71 lines (56 loc) · 2.04 KB
/
hw6.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
from PIL import Image
from helpers.image import Pixels2D
from hw2 import thresholding
def downsampling(img, size):
width, height = size
ratio_x, ratio_y = img.width / width, img.height / height
pixels = Pixels2D(img)
result = Image.new(img.mode, size)
result.putdata([pixels[x * ratio_x, y * ratio_y] for y in xrange(height) for x in xrange(width)])
return result
class SymbolicOperator(object):
@classmethod
def _x(cls, pixels, size, center):
width, height = size
center_x, center_y = center
return map(lambda (_x, _y): pixels[_x, _y] if 0 <= _x < width and 0 <= _y < height else 0,
map(lambda (_x, _y): (center_x + _x, center_y + _y), [
(0, 0), (1, 0), (0, -1), (-1, 0), (0, 1), (1, 1), (1, -1), (-1, -1), (-1, 1),
])
)
class YokoiConnNumber(SymbolicOperator):
def __new__(cls, img):
return [
str(cls._f(img, (x, y))) if img.getpixel((x, y)) != 0 else ' '
for y in xrange(img.height) for x in xrange(img.width)
]
@classmethod
def _f(cls, img, (center_x, center_y)):
pixels = Pixels2D(img)
x = cls._x(pixels, img.size, (center_x, center_y))
a = map(cls._h, [
(x[0], x[1], x[6], x[2]),
(x[0], x[2], x[7], x[3]),
(x[0], x[3], x[8], x[4]),
(x[0], x[4], x[5], x[1]),
])
return 5 if all(ai == 'r' for ai in a) else a.count('q')
@classmethod
def _h(cls, (b, c, d, e)):
if b != c:
return 's'
if b == c == d == e:
return 'r'
return 'q'
if __name__ == '__main__':
img = Image.open('benchmarks/lena.bmp')
bin_img = thresholding(img, 128)
small_img = downsampling(bin_img, (64, 64))
yokoi = YokoiConnNumber(small_img)
with open('results/yokoi.txt', 'w') as f:
f.write(
'\n'.join(
''.join(yokoi[i : i + small_img.width])
for i in xrange(0, len(yokoi), small_img.width)
)
)