ds = map(dsg, grid)
d = max(map(abs, ds))
if d > limit:
- return 999999
+ return float("inf")
score = 0
for line in lines:
s = min(map(lambda g: abs(line[1] - g), ds))
while True:
t0 = time.time()
- sc1, gridv = 999999, None
- sc2, gridh = 999999, None
- sc1_n, sc2_n = 999999, 999999
+ sc1, gridv = float("inf"), None
+ sc2, gridh = float("inf"), None
+ sc1_n, sc2_n = float("inf"), float("inf")
gridv_n, gridh_n = None, None
for k in range(50):
for i in range(5):
def find(lines, size, l1, l2, bounds, hough, show_all, do_something, logger):
logger("finding the grid")
l1, l2 = lines
- sc1, gridv = 999999, None
+ sc1, gridv = float("inf"), None
for i in range(250):
l1s = random.sample(l1, 2)
l1s.sort(key=lambda l: l[1])
if sc1_n < sc1:
sc1, gridv = sc1_n, gridv_n
- sc2, gridh = 999999, None
+ sc2, gridh = float("inf"), None
for i in range(250):
l2s = random.sample(l2, 2)
l2s.sort(key=lambda l: l[1])
# TODO comments, refactoring, move methods to appropriate modules
+class GridFittingFailedError(Exception):
+ pass
+
def plot_line(line, c, size):
points = linef.line_from_angl_dist(line, size)
pyplot.plot(*zip(*points), color=c)
points = [l.points for l in new_lines1]
- 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)
- data = sum(points, [])
- diag1 = Line(line1)
- diag1.points = ransac.filter_near(data, diag1, 2)
- diag2 = Line(line2)
- diag2.points = ransac.filter_near(data, diag2, 2)
-
- 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))
- plot_line_g(diag1, size[0])
- plot_line_g(diag2, size[0])
- pyplot.scatter(*zip(*sum(points, [])))
- pyplot.scatter([center[0]], [center[1]], 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 diagonal")
-
-
- grids = list(gen_corners(diag1, diag2))
-
- sc, grid = min(map(lambda g: (score(sum(g, []), data), g), grids))
-
+ for trial in xrange(3):
+ 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)
+ data = sum(points, [])
+ diag1 = Line(line1)
+ diag1.points = ransac.filter_near(data, diag1, 2)
+ diag2 = Line(line2)
+ diag2.points = ransac.filter_near(data, diag2, 2)
+
+ 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))
+ plot_line_g(diag1, size[0])
+ plot_line_g(diag2, size[0])
+ pyplot.scatter(*zip(*sum(points, [])))
+ pyplot.scatter([center[0]], [center[1]], 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 diagonal")
+
+
+ grids = list(gen_corners(diag1, diag2))
+
+ try:
+ sc, grid = min(map(lambda g: (score(sum(g, []), data), g), grids))
+ break
+ except ValueError:
+ pass
+ else:
+ raise GridFittingFailedError
+
grid_lines = [[l2ad(l, size) for l in grid[0]],
[l2ad(l, size) for l in grid[1]]]
grid_lines[0].sort(key=lambda l: l[1])