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 intersection((a1, b1, c1), (a2, b2, c2)):
46 delim = float(a1 * b2 - b1 * a2)
47 x = (b1 * c2 - c1 * b2) / delim
48 y = (c1 * a2 - a1 * c2) / delim
52 def __init__(self, (x, y)):
56 def __getitem__(self, key):
70 return (self.x, self.y)
73 def __init__(self, (a, b, c)):
74 self.a, self.b, self.c = (a, b, c)
78 def from_ad(cls, (a, d), size):
79 p = linef.line_from_angl_dist((a, d), size)
80 return cls(ransac.points_to_line(*p))
90 def __getitem__(self, key):
98 def gen_corners(d1, d2):
104 c2 = [p for p in d2.points if p in c1.l1.points][0]
105 c3 = [p for p in d1.points if p in c2.l2.points][0]
106 c4 = [p for p in d2.points if p in c3.l1.points][0]
109 # there is not a corresponding intersection
110 # TODO create an intersection?
112 yield manual.lines(map(lambda p: p.to_tuple(), [c2, c1, c3, c4]))
115 # the square was too small to fit 17 lines inside
116 # TODO define SquareTooSmallError or something
119 (x, y), (a, b, c) = p, ransac.points_to_line(*l)
120 return abs(a * x + b * y + c) / sqrt(a*a+b*b)
122 def score(lines, points):
125 s = min(map(lambda l: dst(p, l), lines))
131 def find(lines, size, l1, l2, bounds, hough, show_all, do_something, logger):
132 logger("finding the grid")
133 new_lines1 = map(lambda l: Line.from_ad(l, size), lines[0])
134 new_lines2 = map(lambda l: Line.from_ad(l, size), lines[1])
135 for l1 in new_lines1:
136 for l2 in new_lines2:
137 p = Point(intersection(l1, l2))
143 points = [l.points for l in new_lines1]
145 line1, cons = ransac.estimate(points, 2, 800, Diagonal_model)
146 points2 = map(lambda l: [(p if not p in cons else None) for p in l], points)
147 line2, cons2 = ransac.estimate(points2, 2, 800, Diagonal_model)
148 center = intersection(line1, line2)
149 data = sum(points, [])
151 diag1.points = ransac.filter_near(data, diag1, 2)
153 diag2.points = ransac.filter_near(data, diag2, 2)
156 import matplotlib.pyplot as pyplot
159 def plot_line_g((a, b, c), max_x):
160 find_y = lambda x: - (c + a * x) / b
161 pyplot.plot([0, max_x], [find_y(0), find_y(max_x)], color='b')
163 fig = pyplot.figure(figsize=(8, 6))
164 plot_line_g(diag1, size[0])
165 plot_line_g(diag2, size[0])
166 pyplot.scatter(*zip(*sum(points, [])))
167 pyplot.scatter([center[0]], [center[1]], color='r')
168 pyplot.xlim(0, size[0])
169 pyplot.ylim(0, size[1])
171 size_f = fig.canvas.get_width_height()
172 buff = fig.canvas.tostring_rgb()
173 image_p = Image.fromstring('RGB', size_f, buff, 'raw')
174 do_something(image_p, "finding diagonal")
177 grids = list(gen_corners(diag1, diag2))
179 sc, grid = min(map(lambda g: (score(sum(g, []), data), g), grids))
181 grid_lines = [[l2ad(l, size) for l in grid[0]],
182 [l2ad(l, size) for l in grid[1]]]
183 grid_lines[0].sort(key=lambda l: l[1])
184 grid_lines[1].sort(key=lambda l: l[1])
185 if grid_lines[0][0][0] > grid_lines[1][0][0]:
186 grid_lines = grid_lines[1], grid_lines[0]
188 return grid, grid_lines
192 import matplotlib.pyplot as pyplot
194 lines = pickle.load(open('lines.pickle'))
197 new_lines1 = map(lambda l: Line.from_ad(l, size), lines[0])
198 new_lines2 = map(lambda l: Line.from_ad(l, size), lines[1])
199 for l1 in new_lines1:
200 for l2 in new_lines2:
201 p = Point(intersection(l1, l2))
207 points = [l.points for l in new_lines1]
209 line1, cons = ransac.estimate(points, 2, 800, Diagonal_model)
210 points2 = map(lambda l: [(p if not p in cons else None) for p in l], points)
211 line2, cons2 = ransac.estimate(points2, 2, 800, Diagonal_model)
212 center = intersection(line1, line2)
213 data = sum(points, [])
215 diag1.points = ransac.filter_near(data, diag1, 2)
217 diag2.points = ransac.filter_near(data, diag2, 2)
219 plot_line_g(diag1, 520)
220 plot_line_g(diag2, 520)
221 pyplot.scatter(*zip(*sum(points, [])))
222 pyplot.scatter([center[0]], [center[1]], color='r')
227 grids = map(manual.lines, list(gen_corners(diag1, diag2)))
228 plot_grid = lambda g: map(lambda l: pyplot.plot(*zip(*l), color='g'), sum(g, []))
229 map(plot_grid, grids)
232 sc, grid = min(map(lambda g: (score(sum(g, []), data), g), grids))
234 map(lambda l: pyplot.plot(*zip(*l), color='g'), sum(grid, []))
235 pyplot.scatter(*zip(*sum(points, [])))