1 """Imago intersections module"""
3 from math import cos, tan, pi
4 from operator import itemgetter
12 """Return normalized line."""
14 line = line[0] + pi, - line[1]
18 """Return lines sorted by distance."""
19 l_max = max(l[0] for l in lines)
20 l_min = min(l[0] for l in lines)
21 if l_max - l_min > (3. / 4) * pi:
22 lines = [dst(l) for l in lines]
23 lines.sort(key=itemgetter(1))
26 def board(image, lines, show_all, do_something):
27 """Compute intersections, find stone colors and return board situation."""
28 lines = [dst_sort(l) for l in lines]
29 intersections = intersections_from_angl_dist(lines, image.size)
32 image_g = image.copy()
33 draw = ImageDraw.Draw(image_g)
34 for line in intersections:
36 draw.point((x , y), fill=(120, 255, 120))
37 do_something(image_g, "intersections")
41 for line in intersections:
42 board_raw.append([stone_color_raw(image, intersection) for intersection in
44 board_raw = sum(board_raw, [])
46 ### Show color distribution
47 luma = [s[0] for s in board_raw]
48 saturation = [s[1] for s in board_raw]
51 import matplotlib.pyplot as pyplot
52 pyplot.scatter(luma, saturation,
61 clusters = k_means.cluster(3, 2,zip(zip(luma, saturation), range(len(luma))),
62 [[0., 0.5], [0.5, 0.5], [1., 0.5]])
63 #clusters.sort(key=mean_luma)
66 pyplot.scatter([d[0][0] for d in clusters[0]], [d[0][1] for d in clusters[0]],
68 pyplot.scatter([d[0][0] for d in clusters[1]], [d[0][1] for d in clusters[1]],
70 pyplot.scatter([d[0][0] for d in clusters[2]], [d[0][1] for d in clusters[2]],
76 clusters[0] = [(p[1], 'B') for p in clusters[0]]
77 clusters[1] = [(p[1], '.') for p in clusters[1]]
78 clusters[2] = [(p[1], 'W') for p in clusters[2]]
80 board_rl = sum(clusters, [])
82 board_rg = (p[1] for p in board_rl)
86 #TODO 19 should be a size parameter
90 board_r.append(board_rg.next())
95 return output.Board(19, board_r)
97 def mean_luma(cluster):
98 return sum(c[0][0] for c in cluster) / float(len(cluster))
100 def intersections_from_angl_dist(lines, size, get_all=True):
101 """Take grid-lines and size of the image. Return intersections."""
103 for (angl1, dist1) in lines[1]:
105 for (angl2, dist2) in lines[0]:
106 if abs(angl1 - angl2) > 0.4:
107 i_x = (- ((dist2 / cos(angl2)) - (dist1 / cos(angl1)))
108 / (tan(angl1) - tan(angl2)))
109 i_y = (tan(angl1) * i_x) - (dist1 / cos(angl1))
110 if get_all or (-size[0] / 2 < i_x < size[0] / 2 and
111 -size[1] / 2 < i_y < size[1] / 2):
112 line.append((int(i_x + size[0] / 2),
113 int(i_y + size[1] / 2)))
114 intersections.append(line)
118 """Using the HSI color model."""
119 max_diff = max(c) - min(c)
123 return 1. - ((3. * min(c)) / sum(c))
125 def stone_color_raw(image, (x, y)):
126 """Given image and coordinates, return stone color."""
130 for i in range(-size, size + 1):
131 for j in range(-size, size + 1):
133 suma.append(image.getpixel((x + i, y + j)))
137 luma = sum([0.30 * sum(s[0] for s in suma) / t, 0.59 * sum(s[1] for s in suma) / t,
138 0.11 * sum(s[2] for s in suma) / t]) / 255.
139 saturation = sum(RGBtoSat(s) for s in suma) / t
140 color = [sum(s[0] for s in suma) / t, sum(s[1] for s in suma) / t,
141 sum(s[2] for s in suma) / t]
142 return luma, saturation, color