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]
109 yield map(lambda p: p.to_tuple(), [c1, c2, c3, c4])
112 (x, y), (a, b, c) = p, ransac.points_to_line(*l)
113 return abs(a * x + b * y + c) / sqrt(a*a+b*b)
115 def score(lines, points):
118 s = min(map(lambda l: dst(p, l), lines))
124 def find(lines, size, l1, l2, bounds, hough, show_all, do_something, logger):
125 logger("finding the grid")
126 new_lines1 = map(lambda l: Line.from_ad(l, size), lines[0])
127 new_lines2 = map(lambda l: Line.from_ad(l, size), lines[1])
128 for l1 in new_lines1:
129 for l2 in new_lines2:
130 p = Point(intersection(l1, l2))
136 points = [l.points for l in new_lines1]
138 line1, cons = ransac.estimate(points, 2, 800, Diagonal_model)
139 points2 = map(lambda l: [(p if not p in cons else None) for p in l], points)
140 line2, cons2 = ransac.estimate(points2, 2, 800, Diagonal_model)
141 center = intersection(line1, line2)
142 data = sum(points, [])
144 diag1.points = ransac.filter_near(data, diag1, 2)
146 diag2.points = ransac.filter_near(data, diag2, 2)
148 grids = map(manual.lines, list(gen_corners(diag1, diag2)))
150 sc, grid = min(map(lambda g: (score(sum(g, []), data), g), grids))
152 grid_lines = [[l2ad(l, size) for l in grid[0]],
153 [l2ad(l, size) for l in grid[1]]]
155 return grid, grid_lines
159 import matplotlib.pyplot as pyplot
161 lines = pickle.load(open('lines.pickle'))
164 new_lines1 = map(lambda l: Line.from_ad(l, size), lines[0])
165 new_lines2 = map(lambda l: Line.from_ad(l, size), lines[1])
166 for l1 in new_lines1:
167 for l2 in new_lines2:
168 p = Point(intersection(l1, l2))
174 points = [l.points for l in new_lines1]
176 line1, cons = ransac.estimate(points, 2, 800, Diagonal_model)
177 points2 = map(lambda l: [(p if not p in cons else None) for p in l], points)
178 line2, cons2 = ransac.estimate(points2, 2, 800, Diagonal_model)
179 center = intersection(line1, line2)
180 data = sum(points, [])
182 diag1.points = ransac.filter_near(data, diag1, 2)
184 diag2.points = ransac.filter_near(data, diag2, 2)
186 plot_line_g(diag1, 520)
187 plot_line_g(diag2, 520)
188 pyplot.scatter(*zip(*sum(points, [])))
189 pyplot.scatter([center[0]], [center[1]], color='r')
194 grids = map(manual.lines, list(gen_corners(diag1, diag2)))
195 plot_grid = lambda g: map(lambda l: pyplot.plot(*zip(*l), color='g'), sum(g, []))
196 map(plot_grid, grids)
199 sc, grid = min(map(lambda g: (score(sum(g, []), data), g), grids))
201 map(lambda l: pyplot.plot(*zip(*l), color='g'), sum(grid, []))
202 pyplot.scatter(*zip(*sum(points, [])))