-
- for trial in xrange(3):
- model = Diagonal_model(points)
- diag_lines = ransac.ransac_multi(2, points, 2, 800, model=model)
- [(line1, cons), (line2, cons2)] = diag_lines
- center = intersection(line1, line2)
- data = sum(points, [])
+ def dst_p(x, y):
+ x = x - size[0] / 2
+ y = y - size[1] / 2
+ return sqrt(x * x + y * y)
+
+ model = Diagonal_model(points)
+ diag_lines = ransac.ransac_multi(6, points, 2, 800, model=model)
+ diag_lines = [l[0] for l in diag_lines]
+ centers = []
+ cen_lin = []
+ for i in xrange(len(diag_lines)):
+ line1 = diag_lines[i]
+ for line2 in diag_lines[i+1:]:
+ c = intersection(line1, line2)
+ if c and dst_p(*c) < min(size) / 2:
+ cen_lin.append((line1, line2, c))
+ centers.append(c)
+
+ if show_all:
+ import matplotlib.pyplot as pyplot
+ import Image
+
+ 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')
+
+ fig = pyplot.figure(figsize=(8, 6))
+ for l in diag_lines:
+ plot_line_g(l, size[0])
+ pyplot.scatter(*zip(*sum(points, [])))
+ pyplot.scatter(*zip(*centers), color='r')
+ pyplot.xlim(0, size[0])
+ pyplot.ylim(0, size[1])
+ pyplot.gca().invert_yaxis()
+ fig.canvas.draw()
+ size_f = fig.canvas.get_width_height()
+ buff = fig.canvas.tostring_rgb()
+ image_p = Image.fromstring('RGB', size_f, buff, 'raw')
+ do_something(image_p, "finding diagonals")
+
+ data = sum(points, [])
+ # TODO what if lines are missing?
+ sc = float("inf")
+ grid = None
+ for (line1, line2, c) in cen_lin: