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):
13 points = linef.line_from_angl_dist(line, (520, 390))
14 pyplot.plot(*zip(*points), color=c)
16 def plot_line_g((a, b, c), max_x):
17 find_y = lambda x: - (c + a * x) / b
18 pyplot.plot([0, max_x], [find_y(0), find_y(max_x)], color='b')
21 def __init__(self, data):
22 self.data = [p for p in sum(data, []) if p]
24 self.gen = self.initial_g()
27 l1, l2 = random.sample(self.lines, 2)
28 for i in xrange(len(l1)):
29 for j in xrange(len(l2)):
37 return self.gen.next()
39 self.gen = self.initial_g()
40 return self.gen.next()
42 def get(self, sample):
44 return ransac.points_to_line(*sample)
46 return ransac.least_squares(sample)
48 def intersection((a1, b1, c1), (a2, b2, c2)):
49 delim = float(a1 * b2 - b1 * a2)
50 x = (b1 * c2 - c1 * b2) / delim
51 y = (c1 * a2 - a1 * c2) / delim
55 def __init__(self, (x, y)):
59 def __getitem__(self, key):
73 return (self.x, self.y)
76 def __init__(self, (a, b, c)):
77 self.a, self.b, self.c = (a, b, c)
81 def from_ad(cls, (a, d), size):
82 p = linef.line_from_angl_dist((a, d), size)
83 return cls(ransac.points_to_line(*p))
93 def __getitem__(self, key):
101 def gen_corners(d1, d2):
106 c2 = [p for p in d2.points if p in c1.l1.points][0]
107 c3 = [p for p in d1.points if p in c2.l2.points][0]
108 c4 = [p for p in d2.points if p in c3.l1.points][0]
110 yield manual.lines(map(lambda p: p.to_tuple(), [c2, c1, c3, c4]))
113 # the square was too small to fit 17 lines inside
114 # TODO define SquareTooSmallError or something
117 (x, y), (a, b, c) = p, ransac.points_to_line(*l)
118 return abs(a * x + b * y + c) / sqrt(a*a+b*b)
120 def score(lines, points):
123 s = min(map(lambda l: dst(p, l), lines))
129 def find(lines, size, l1, l2, bounds, hough, show_all, do_something, logger):
130 logger("finding the grid")
131 new_lines1 = map(lambda l: Line.from_ad(l, size), lines[0])
132 new_lines2 = map(lambda l: Line.from_ad(l, size), lines[1])
133 for l1 in new_lines1:
134 for l2 in new_lines2:
135 p = Point(intersection(l1, l2))
141 points = [l.points for l in new_lines1]
143 line1, cons = ransac.estimate(points, 2, 800, Diagonal_model)
144 points2 = map(lambda l: [(p if not p in cons else None) for p in l], points)
145 line2, cons2 = ransac.estimate(points2, 2, 800, Diagonal_model)
146 center = intersection(line1, line2)
147 data = sum(points, [])
149 diag1.points = ransac.filter_near(data, diag1, 2)
151 diag2.points = ransac.filter_near(data, diag2, 2)
153 grids = list(gen_corners(diag1, diag2))
155 sc, grid = min(map(lambda g: (score(sum(g, []), data), g), grids))
157 grid_lines = [[l2ad(l, size) for l in grid[0]],
158 [l2ad(l, size) for l in grid[1]]]
159 grid_lines[0].sort(key=lambda l: l[1])
160 grid_lines[1].sort(key=lambda l: l[1])
161 if grid_lines[0][0][0] > grid_lines[1][0][0]:
162 grid_lines = grid_lines[1], grid_lines[0]
164 return grid, grid_lines
168 import matplotlib.pyplot as pyplot
170 lines = pickle.load(open('lines.pickle'))
173 new_lines1 = map(lambda l: Line.from_ad(l, size), lines[0])
174 new_lines2 = map(lambda l: Line.from_ad(l, size), lines[1])
175 for l1 in new_lines1:
176 for l2 in new_lines2:
177 p = Point(intersection(l1, l2))
183 points = [l.points for l in new_lines1]
185 line1, cons = ransac.estimate(points, 2, 800, Diagonal_model)
186 points2 = map(lambda l: [(p if not p in cons else None) for p in l], points)
187 line2, cons2 = ransac.estimate(points2, 2, 800, Diagonal_model)
188 center = intersection(line1, line2)
189 data = sum(points, [])
191 diag1.points = ransac.filter_near(data, diag1, 2)
193 diag2.points = ransac.filter_near(data, diag2, 2)
195 plot_line_g(diag1, 520)
196 plot_line_g(diag2, 520)
197 pyplot.scatter(*zip(*sum(points, [])))
198 pyplot.scatter([center[0]], [center[1]], color='r')
203 grids = map(manual.lines, list(gen_corners(diag1, diag2)))
204 plot_grid = lambda g: map(lambda l: pyplot.plot(*zip(*l), color='g'), sum(g, []))
205 map(plot_grid, grids)
208 sc, grid = min(map(lambda g: (score(sum(g, []), data), g), grids))
210 map(lambda l: pyplot.plot(*zip(*l), color='g'), sum(grid, []))
211 pyplot.scatter(*zip(*sum(points, [])))