1 """Imago intersections module"""
3 from math import cos, tan, pi
4 from operator import itemgetter
11 """Return normalized line."""
13 line = line[0] + pi, - line[1]
17 """Return lines sorted by distance."""
18 l_max = max(l[0] for l in lines)
19 l_min = min(l[0] for l in lines)
20 if l_max - l_min > (3. / 4) * pi:
21 lines = [dst(l) for l in lines]
22 lines.sort(key=itemgetter(1))
25 def board(image, lines, show_all, do_something):
26 """Compute intersections, find stone colors and return board situation."""
27 lines = [dst_sort(l) for l in lines]
28 intersections = intersections_from_angl_dist(lines, image.size)
31 image_g = image.copy()
32 draw = ImageDraw.Draw(image_g)
33 for line in intersections:
35 draw.point((x , y), fill=(120, 255, 120))
36 do_something(image_g, "intersections")
40 for line in intersections:
41 board_raw.append([stone_color_raw(image, intersection) for intersection in
43 board_raw = sum(board_raw, [])
45 ### Show color distribution
46 luma = [s[0] for s in board_raw]
47 saturation = [s[1] for s in board_raw]
50 import matplotlib.pyplot as pyplot
51 pyplot.scatter(luma, saturation,
52 color=[(s[2][0]/255., s[2][1]/255., s[2][2]/255., 1.)
56 clusters = k_means.cluster(3, 2,zip(zip(luma, saturation), range(len(luma))),
57 [[0., 0.], [0.5, 0.5], [1., 1.]])
58 #clusters.sort(key=mean_luma)
61 pyplot.scatter([d[0][0] for d in clusters[0]], [d[0][1] for d in clusters[0]],
63 pyplot.scatter([d[0][0] for d in clusters[1]], [d[0][1] for d in clusters[1]],
65 pyplot.scatter([d[0][0] for d in clusters[2]], [d[0][1] for d in clusters[2]],
69 clusters[0] = [(p[1], 'B') for p in clusters[0]]
70 clusters[1] = [(p[1], '.') for p in clusters[1]]
71 clusters[2] = [(p[1], 'W') for p in clusters[2]]
73 board_rl = sum(clusters, [])
75 board_rg = (p[1] for p in board_rl)
83 board_r[i].append(board_rg.next())
89 def mean_luma(cluster):
90 return sum(c[0][0] for c in cluster) / float(len(cluster))
92 def intersections_from_angl_dist(lines, size, get_all=True):
93 """Take grid-lines and size of the image. Return intersections."""
95 for (angl1, dist1) in lines[1]:
97 for (angl2, dist2) in lines[0]:
98 if abs(angl1 - angl2) > 0.4:
99 i_x = (- ((dist2 / cos(angl2)) - (dist1 / cos(angl1)))
100 / (tan(angl1) - tan(angl2)))
101 i_y = (tan(angl1) * i_x) - (dist1 / cos(angl1))
102 if get_all or (-size[0] / 2 < i_x < size[0] / 2 and
103 -size[1] / 2 < i_y < size[1] / 2):
104 line.append((int(i_x + size[0] / 2),
105 int(i_y + size[1] / 2)))
106 intersections.append(line)
109 def stone_color_raw(image, (x, y)):
110 """Given image and coordinates, return stone color."""
112 for i in range(-2, 3):
113 for j in range(-2, 3):
115 suma.append(image.getpixel((x + i, y + j)))
118 luma = sum([0.30 * sum(s[0] for s in suma) / 25., 0.59 * sum(s[1] for s in suma) / 25.,
119 0.11 * sum(s[2] for s in suma) / 25.]) / 255.
120 saturation = sum(max(s) - min(s) / float(255. - abs(max(s) + min(s) - 255))
121 for s in suma) / (25. * 255.)
122 color = [sum(s[0] for s in suma) / 25., sum(s[1] for s in suma) / 25.,
123 sum(s[2] for s in suma) / 25.]
124 return luma, saturation, color