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 def plot_line(line, c, size):
13 points = linef.line_from_angl_dist(line, size)
14 pyplot.plot(*zip(*points), color=c)
18 def __init__(self, data):
19 self.data = [p for p in sum(data, []) if p]
21 self.gen = self.initial_g()
24 l1, l2 = random.sample(self.lines, 2)
25 for i in xrange(len(l1)):
26 for j in xrange(len(l2)):
34 return self.gen.next()
36 self.gen = self.initial_g()
37 return self.gen.next()
39 def get(self, sample):
41 return ransac.points_to_line(*sample)
43 return ransac.least_squares(sample)
45 def score(self, est, dist):
49 dst = lambda (x, y): abs(a * x + b * y + c) / sqrt(a*a+b*b)
57 def intersection((a1, b1, c1), (a2, b2, c2)):
58 delim = float(a1 * b2 - b1 * a2)
59 x = (b1 * c2 - c1 * b2) / delim
60 y = (c1 * a2 - a1 * c2) / delim
64 def __init__(self, (x, y)):
68 def __getitem__(self, key):
82 return (self.x, self.y)
85 def __init__(self, (a, b, c)):
86 self.a, self.b, self.c = (a, b, c)
90 def from_ad(cls, (a, d), size):
91 p = linef.line_from_angl_dist((a, d), size)
92 return cls(ransac.points_to_line(*p))
102 def __getitem__(self, key):
110 def gen_corners(d1, d2):
116 c2 = [p for p in d2.points if p in c1.l1.points][0]
117 c3 = [p for p in d1.points if p in c2.l2.points][0]
118 c4 = [p for p in d2.points if p in c3.l1.points][0]
121 # there is not a corresponding intersection
122 # TODO create an intersection?
124 yield manual.lines(map(lambda p: p.to_tuple(), [c2, c1, c3, c4]))
127 # the square was too small to fit 17 lines inside
128 # TODO define SquareTooSmallError or something
131 (x, y), (a, b, c) = p, ransac.points_to_line(*l)
132 return abs(a * x + b * y + c) / sqrt(a*a+b*b)
134 def score(lines, points):
137 s = min(map(lambda l: dst(p, l), lines))
143 def find(lines, size, l1, l2, bounds, hough, show_all, do_something, logger):
144 logger("finding the grid")
145 new_lines1 = map(lambda l: Line.from_ad(l, size), lines[0])
146 new_lines2 = map(lambda l: Line.from_ad(l, size), lines[1])
147 for l1 in new_lines1:
148 for l2 in new_lines2:
149 p = Point(intersection(l1, l2))
155 points = [l.points for l in new_lines1]
157 line1, cons = ransac.estimate(points, 2, 800, Diagonal_model)
158 points2 = map(lambda l: [(p if not p in cons else None) for p in l], points)
159 line2, cons2 = ransac.estimate(points2, 2, 800, Diagonal_model)
160 center = intersection(line1, line2)
161 data = sum(points, [])
163 diag1.points = ransac.filter_near(data, diag1, 2)
165 diag2.points = ransac.filter_near(data, diag2, 2)
168 import matplotlib.pyplot as pyplot
171 def plot_line_g((a, b, c), max_x):
172 find_y = lambda x: - (c + a * x) / b
173 pyplot.plot([0, max_x], [find_y(0), find_y(max_x)], color='b')
175 fig = pyplot.figure(figsize=(8, 6))
176 plot_line_g(diag1, size[0])
177 plot_line_g(diag2, size[0])
178 pyplot.scatter(*zip(*sum(points, [])))
179 pyplot.scatter([center[0]], [center[1]], color='r')
180 pyplot.xlim(0, size[0])
181 pyplot.ylim(0, size[1])
182 pyplot.gca().invert_yaxis()
184 size_f = fig.canvas.get_width_height()
185 buff = fig.canvas.tostring_rgb()
186 image_p = Image.fromstring('RGB', size_f, buff, 'raw')
187 do_something(image_p, "finding diagonal")
190 grids = list(gen_corners(diag1, diag2))
192 sc, grid = min(map(lambda g: (score(sum(g, []), data), g), grids))
194 grid_lines = [[l2ad(l, size) for l in grid[0]],
195 [l2ad(l, size) for l in grid[1]]]
196 grid_lines[0].sort(key=lambda l: l[1])
197 grid_lines[1].sort(key=lambda l: l[1])
198 if grid_lines[0][0][0] > grid_lines[1][0][0]:
199 grid_lines = grid_lines[1], grid_lines[0]
201 return grid, grid_lines
205 import matplotlib.pyplot as pyplot
207 lines = pickle.load(open('lines.pickle'))
210 new_lines1 = map(lambda l: Line.from_ad(l, size), lines[0])
211 new_lines2 = map(lambda l: Line.from_ad(l, size), lines[1])
212 for l1 in new_lines1:
213 for l2 in new_lines2:
214 p = Point(intersection(l1, l2))
220 points = [l.points for l in new_lines1]
222 line1, cons = ransac.estimate(points, 2, 800, Diagonal_model)
223 points2 = map(lambda l: [(p if not p in cons else None) for p in l], points)
224 line2, cons2 = ransac.estimate(points2, 2, 800, Diagonal_model)
225 center = intersection(line1, line2)
226 data = sum(points, [])
228 diag1.points = ransac.filter_near(data, diag1, 2)
230 diag2.points = ransac.filter_near(data, diag2, 2)
232 plot_line_g(diag1, 520)
233 plot_line_g(diag2, 520)
234 pyplot.scatter(*zip(*sum(points, [])))
235 pyplot.scatter([center[0]], [center[1]], color='r')
240 grids = map(manual.lines, list(gen_corners(diag1, diag2)))
241 plot_grid = lambda g: map(lambda l: pyplot.plot(*zip(*l), color='g'), sum(g, []))
242 map(plot_grid, grids)
245 sc, grid = min(map(lambda g: (score(sum(g, []), data), g), grids))
247 map(lambda l: pyplot.plot(*zip(*l), color='g'), sum(grid, []))
248 pyplot.scatter(*zip(*sum(points, [])))