4 from intrsc import intersections_from_angl_dist
6 import ransac as ransac
7 import manual as manual
8 from geometry import l2ad
10 # TODO comments, refactoring, move methods to appropriate modules
12 class GridFittingFailedError(Exception):
15 class BadGenError(Exception):
18 def plot_line(line, c, size):
19 points = linef.line_from_angl_dist(line, size)
20 pyplot.plot(*zip(*points), color=c)
24 def __init__(self, data):
25 self.data = [p for p in sum(data, []) if p]
27 self.gen = self.initial_g()
30 l1, l2 = random.sample(self.lines, 2)
31 for i in xrange(len(l1)):
32 for j in xrange(len(l2)):
38 def remove(self, data):
39 self.data = list(set(self.data) - set(data))
45 self.gen = self.initial_g()
49 def get(self, sample):
51 return ransac.points_to_line(*sample)
53 return ransac.least_squares(sample)
55 def score(self, est, dist):
59 dst = lambda (x, y): abs(a * x + b * y + c) / sqrt(a*a+b*b)
66 if p.l1 == l1 or p.l2 == l2:
67 return float("inf"), []
70 else: # TODO delete this or refactor
75 def intersection((a1, b1, c1), (a2, b2, c2)):
76 delim = float(a1 * b2 - b1 * a2)
79 x = (b1 * c2 - c1 * b2) / delim
80 y = (c1 * a2 - a1 * c2) / delim
84 def __init__(self, (x, y)):
88 def __getitem__(self, key):
102 return (self.x, self.y)
105 def __init__(self, (a, b, c)):
106 self.a, self.b, self.c = (a, b, c)
110 def from_ad(cls, (a, d), size):
111 p = linef.line_from_angl_dist((a, d), size)
112 return cls(ransac.points_to_line(*p))
122 def __getitem__(self, key):
130 def gen_corners(d1, d2):
136 c2 = [p for p in d2.points if p in c1.l1.points][0]
137 c3 = [p for p in d1.points if p in c2.l2.points][0]
138 c4 = [p for p in d2.points if p in c3.l1.points][0]
141 # there is not a corresponding intersection
142 # TODO create an intersection?
144 yield manual.lines(map(lambda p: p.to_tuple(), [c2, c1, c3, c4]))
147 # the square was too small to fit 17 lines inside
148 # TODO define SquareTooSmallError or something
151 (x, y), (a, b, c) = p, ransac.points_to_line(*l)
152 return abs(a * x + b * y + c) / sqrt(a*a+b*b)
154 def score(lines, points):
157 s = min(map(lambda l: dst(p, l), lines))
163 def find(lines, size, l1, l2, bounds, hough, show_all, do_something, logger):
164 logger("finding the grid")
165 new_lines1 = map(lambda l: Line.from_ad(l, size), lines[0])
166 new_lines2 = map(lambda l: Line.from_ad(l, size), lines[1])
167 for l1 in new_lines1:
168 for l2 in new_lines2:
169 p = Point(intersection(l1, l2))
175 points = [l.points for l in new_lines1]
180 return sqrt(x * x + y * y)
182 for n_tries in xrange(3):
183 model = Diagonal_model(points)
184 diag_lines = ransac.ransac_multi(6, points, 2, 800, model=model)
185 diag_lines = [l[0] for l in diag_lines]
188 for i in xrange(len(diag_lines)):
189 line1 = diag_lines[i]
190 for line2 in diag_lines[i+1:]:
191 c = intersection(line1, line2)
192 if c and dst_p(*c) < min(size) / 2:
193 cen_lin.append((line1, line2, c))
197 import matplotlib.pyplot as pyplot
200 def plot_line_g((a, b, c), max_x):
201 find_y = lambda x: - (c + a * x) / b
202 pyplot.plot([0, max_x], [find_y(0), find_y(max_x)], color='b')
204 fig = pyplot.figure(figsize=(8, 6))
206 plot_line_g(l, size[0])
207 pyplot.scatter(*zip(*sum(points, [])))
208 if len(centers) >= 1:
209 pyplot.scatter([c[0] for c in centers], [c[1] for c in centers], color='r')
210 pyplot.xlim(0, size[0])
211 pyplot.ylim(0, size[1])
212 pyplot.gca().invert_yaxis()
214 size_f = fig.canvas.get_width_height()
215 buff = fig.canvas.tostring_rgb()
216 image_p = Image.fromstring('RGB', size_f, buff, 'raw')
217 do_something(image_p, "finding diagonals")
219 data = sum(points, [])
220 # TODO what if lines are missing?
223 for (line1, line2, c) in cen_lin:
225 diag1.points = ransac.filter_near(data, diag1, 2)
227 diag2.points = ransac.filter_near(data, diag2, 2)
230 grids = list(gen_corners(diag1, diag2))
233 new_sc, new_grid = min(map(lambda g: (score(sum(g, []), data), g), grids))
235 sc, grid = new_sc, new_grid
241 raise GridFittingFailedError
243 grid_lines = [[l2ad(l, size) for l in grid[0]],
244 [l2ad(l, size) for l in grid[1]]]
245 grid_lines[0].sort(key=lambda l: l[1])
246 grid_lines[1].sort(key=lambda l: l[1])
247 if grid_lines[0][0][0] > grid_lines[1][0][0]:
248 grid_lines = grid_lines[1], grid_lines[0]
250 return grid, grid_lines