1 """Imago intersections module."""
3 from math import cos, tan, pi
4 from operator import itemgetter
14 """Return normalized line."""
16 line = line[0] + pi, - line[1]
20 """Return lines sorted by distance."""
21 l_max = max(l[0] for l in lines)
22 l_min = min(l[0] for l in lines)
23 if l_max - l_min > (3. / 4) * pi:
24 lines = [dst(l) for l in lines]
25 lines.sort(key=itemgetter(1))
28 def board(image, lines, show_all, do_something):
29 """Compute intersections, find stone colors and return board situation."""
30 # TODO refactor show_all, do_something
31 # TODO refactor this into smaller functions
32 lines = [dst_sort(l) for l in lines]
33 an0 = (sum([l[0] for l in lines[0]]) / len(lines[0]) - pi / 2)
34 an1 = (sum([l[0] for l in lines[1]]) / len(lines[1]) - pi / 2)
36 lines = [lines[1], lines[0]]
38 intersections = intersections_from_angl_dist(lines, image.size)
41 image_g = image.copy()
42 draw = ImageDraw.Draw(image_g)
43 for line in intersections:
45 draw.point((x , y), fill=(120, 255, 120))
46 do_something(image_g, "intersections")
48 image_c = filters.color_enhance(image)
50 do_something(image_c, "white balance")
54 for line in intersections:
55 board_raw.append([stone_color_raw(image_c, intersection) for intersection in
57 board_raw = sum(board_raw, [])
59 ### Show color distribution
60 luma = [s[0] for s in board_raw]
61 saturation = [s[1] for s in board_raw]
64 import matplotlib.pyplot as pyplot
66 fig = pyplot.figure(figsize=(8, 6))
67 pyplot.scatter(luma, saturation,
75 size = fig.canvas.get_width_height()
76 buff = fig.canvas.tostring_rgb()
77 image_p = Image.fromstring('RGB', size, buff, 'raw')
78 do_something(image_p, "color distribution")
80 clusters = k_means.cluster(3, 2,zip(zip(luma, saturation), range(len(luma))),
81 [[0., 0.5], [0.5, 0.5], [1., 0.5]])
84 fig = pyplot.figure(figsize=(8, 6))
85 pyplot.scatter([d[0][0] for d in clusters[0]], [d[0][1] for d in clusters[0]],
87 pyplot.scatter([d[0][0] for d in clusters[1]], [d[0][1] for d in clusters[1]],
89 pyplot.scatter([d[0][0] for d in clusters[2]], [d[0][1] for d in clusters[2]],
94 size = fig.canvas.get_width_height()
95 buff = fig.canvas.tostring_rgb()
96 image_p = Image.fromstring('RGB', size, buff, 'raw')
97 do_something(image_p, "color clustering")
99 clusters[0] = [(p[1], 'B') for p in clusters[0]]
100 clusters[1] = [(p[1], '.') for p in clusters[1]]
101 clusters[2] = [(p[1], 'W') for p in clusters[2]]
103 board_rl = sum(clusters, [])
105 board_rg = (p[1] for p in board_rl)
109 #TODO 19 should be a size parameter
113 board_r.append(board_rg.next())
114 except StopIteration:
118 return output.Board(19, board_r)
120 def mean_luma(cluster):
121 """Return mean luma of the *cluster* of points."""
122 return sum(c[0][0] for c in cluster) / float(len(cluster))
124 def to_general(line, size):
126 (x1, y1), (x2, y2) = linef.line_from_angl_dist(line, size)
127 return (y2 - y1, x1 - x2, x2 * y1 - x1 * y2)
129 def intersection(l1, l2):
132 delim = float(a1 * b2 - b1 * a2)
133 x = (b1 * c2 - c1 * b2) / delim
134 y = (c1 * a2 - a1 * c2) / delim
137 # TODO remove the parameter get_all
138 def intersections_from_angl_dist(lines, size, get_all=True):
139 """Take grid-lines and size of the image. Return intersections."""
140 lines0 = map(lambda l: to_general(l, size), lines[0])
141 lines1 = map(lambda l: to_general(l, size), lines[1])
146 line.append(intersection(l1, l2))
147 intersections.append(line)
150 def rgb2lumsat(color):
151 """Convert RGB to luma and HSI model saturation."""
153 luma = (0.30 * r + 0.59 * g + 0.11 * b) / 255.0
154 max_diff = max(color) - min(color)
158 saturation = 1. - ((3. * min(color)) / sum(color))
159 return luma, saturation
162 #TODO comment (or delete maybe?)
165 return (lst[len_lst / 2] + lst[len_lst / 2 + 1]) / 2.0
167 return lst[len_lst / 2]
169 def stone_color_raw(image, (x, y)):
170 """Given image and coordinates, return stone color."""
173 for i in range(-size, size + 1):
174 for j in range(-size, size + 1):
176 points.append(image.getpixel((x + i, y + j)))
179 norm = float(len(points))
181 return 0, 0, (0, 0, 0) #TODO trow exception here
182 color = (sum(p[0] for p in points) / norm,
183 sum(p[1] for p in points) / norm,
184 sum(p[2] for p in points) / norm)
185 luma, saturation = rgb2lumsat(color)
186 return luma, saturation, color