2 import matplotlib.pyplot as pyplot
5 from src.intrsc import intersections_from_angl_dist
6 import src.linef as linef
7 import src.ransac as ransac
9 def plot_line(line, c):
10 points = linef.line_from_angl_dist(line, (520, 390))
11 pyplot.plot(*zip(*points), color=c)
13 def plot_line_g((a, b, c), max_x):
14 find_y = lambda x: - (c + a * x) / b
15 pyplot.plot([0, max_x], [find_y(0), find_y(max_x)], color='b')
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 def __init__(self, (a, b, c)):
71 self.a, self.b, self.c = (a, b, c)
75 def from_ad(cls, (a, d), size):
76 p = linef.line_from_angl_dist((a, d), size)
77 return cls(ransac.points_to_line(*p))
87 lines = pickle.load(open('lines.pickle'))
90 new_lines1 = map(lambda l: Line.from_ad(l, size), lines[0])
91 new_lines2 = map(lambda l: Line.from_ad(l, size), lines[1])
94 p = Point(intersection(l1, l2))
100 points = [l.points for l in new_lines1]
102 def gen_corners(d1, d2):
111 line1, cons = ransac.estimate(points, 2, 800, Diagonal_model)
112 points2 = map(lambda l: [(p if not p in cons else None) for p in l], points)
113 line2, cons2 = ransac.estimate(points2, 2, 800, Diagonal_model)
114 center = intersection(line1, line2)
115 data = sum(points, [])
117 diag1.points = ransac.filter_near(data, diag1, 2)
119 diag2.points = ransac.filter_near(data, diag2, 2)
122 plot_line_g(diag1, 520)
123 plot_line_g(diag2, 520)
124 pyplot.scatter(*zip(*sum(points, [])))
125 pyplot.scatter([center[0]], [center[1]], color='r')
130 #map(lambda l: plot_line(l, 'g'), sum(lines, []))