+import pickle
+import matplotlib.pyplot as pyplot
+import random
+
+lines = pickle.load(open('lines.pickle'))
+
+from src.intrsc import intersections_from_angl_dist
+import src.linef as linef
+import src.ransac as ransac
+
+points = intersections_from_angl_dist(lines, (520, 390))
+
+pyplot.scatter(*zip(*sum(points, [])))
+
+def plot_line(line, c):
+ points = linef.line_from_angl_dist(line, (520, 390))
+ pyplot.plot(*zip(*points), color=c)
+
+def plot_line_g((a, b, c), max_x):
+ find_y = lambda x: - (c + a * x) / b
+ pyplot.plot([0, max_x], [find_y(0), find_y(max_x)], color='b')
+
+class Diagonal_model:
+ def __init__(self, data):
+ self.data = [p for p in sum(data, []) if p]
+ self.lines = data
+ self.gen = self.initial_g()
+
+ def initial_g(self):
+ l1, l2 = random.sample(self.lines, 2)
+ for i in xrange(len(l1)):
+ for j in xrange(len(l2)):
+ if i == j:
+ continue
+ if l1[i] and l2[j]:
+ yield (l1[i], l2[j])
+
+ def initial(self):
+ try:
+ return self.gen.next()
+ except StopIteration:
+ self.gen = self.initial_g()
+ return self.gen.next()
+
+ def get(self, sample):
+ if len(sample) == 2:
+ return ransac.points_to_line(*sample)
+ else:
+ return ransac.least_squares(sample)
+
+def intersection((a1, b1, c1), (a2, b2, c2)):
+ delim = float(a1 * b2 - b1 * a2)
+ x = (b1 * c2 - c1 * b2) / delim
+ y = (c1 * a2 - a1 * c2) / delim
+ return x, y
+
+
+
+while True:
+ line1, cons = ransac.estimate(points, 2, 800, Diagonal_model)
+ points2 = map(lambda l: [(p if not p in cons else None) for p in l], points)
+ line2, cons2 = ransac.estimate(points2, 2, 800, Diagonal_model)
+ center = intersection(line1, line2)
+
+
+ plot_line_g(line1, 520)
+ plot_line_g(line2, 520)
+ pyplot.scatter(*zip(*sum(points, [])))
+ pyplot.scatter([center[0]], [center[1]], color='r')
+ pyplot.xlim(0, 520)
+ pyplot.ylim(0, 390)
+ pyplot.show()
+
+#map(lambda l: plot_line(l, 'g'), sum(lines, []))
+
+pyplot.show()
+
+