From: Tomas Musil Date: Mon, 30 Jun 2014 03:34:38 +0000 (+0200) Subject: more work on new gridf X-Git-Url: http://git.tomasm.cz/imago.git/commitdiff_plain/f0b736c0abd7ea49b99106b20ece02466b7b19c0?ds=inline;hp=e4044c7515289bb999ee98698abbe09aa6e4b517 more work on new gridf --- diff --git a/new_geometry.py b/new_geometry.py index 3727056..a9b8bdc 100644 --- a/new_geometry.py +++ b/new_geometry.py @@ -39,6 +39,16 @@ def fill(l1, l2, lh, n): intersection((l12, l22), (nrt, nrb))) return [nl, nr] +def expand_right(grid, middle): + return expand(grid[-2], grid[-1], + (intersection(middle, grid[-2]), + (intersection(middle, grid[-1])))) + +def expand_left(grid, middle): + return expand(grid[1], grid[0], + (intersection(middle, grid[1]), + (intersection(middle, grid[0])))) + def expand(l1, l2, lh): l11, l12 = l1 l21, l22 = l2 diff --git a/ransac_grid.py b/ransac_grid.py index 3e06002..df83b7f 100644 --- a/ransac_grid.py +++ b/ransac_grid.py @@ -27,8 +27,26 @@ def to_general(line): def nearest(lines, point): return min(map(lambda l: dst(point, l), lines)) +def nearest2(lines, point): + return min(map(lambda l: dst(point, points_to_line(*l)), lines)) + size = (520, 390) +def generate_models(sample, middle): + sgrid = map(lambda l:linef.line_from_angl_dist(l, size), sample) + lh = (gm.intersection(sgrid[0], middle), gm.intersection(sgrid[1], middle)) + for f in [0, 1, 2, 3, 5]: + grid = gm.fill(sgrid[0], sgrid[1], lh , f) + grid = [sgrid[0]] + grid + [sgrid[1]] + for s in xrange(17 - f): + grid = [gm.expand_left(grid, middle)] + grid + yield grid + for i in xrange(17 - f): + grid = grid[1:] + grid.append(gm.expand_right(grid, middle)) + yield grid + + points = pickle.load(open('edges.pickle')) lines = pickle.load(open('lines.pickle')) @@ -40,36 +58,31 @@ r_lines = pickle.load(open('r_lines.pickle')) l1, l2 = lines +lines_general = map(to_general, sum(lines, [])) +near_points = [p for p in points if nearest(lines_general, p) <= 2] +score = lambda grid: len([p for p in points if nearest2(grid, p) <= 2]) + while True: l1s = random.sample(l2, 2) l1s.sort(key=lambda l: l[1]) - corners = map(lambda l:linef.line_from_angl_dist(l, size), l1s) middle = ((0, 195),(520, 195)) - # TODO! can I assume anything to be perspectively disorted square? + # TODO! can I assume anything to be perspectively disorted square? No. # TODO! take lower and middle and construct top - lh = (gm.intersection(corners[0], middle), gm.intersection(corners[1], middle)) - grid = gm.fill(corners[0], corners[1], lh , 3) - grid = [corners[0]] + grid + [corners[1]] - grid.append(gm.expand(grid[-2], grid[-1], ((gm.intersection(middle, grid[-2]), - (gm.intersection(middle, grid[-1])))))) - grid.append(gm.expand(grid[-2], grid[-1], ((gm.intersection(middle, grid[-2]), - (gm.intersection(middle, grid[-1])))))) + # TODO iterate that ^^^? + + sc, grid = max(map(lambda g: (score(g), g), generate_models(l1s, middle))) + pyplot.scatter(*zip(*near_points)) map(lambda l: pyplot.plot(*zip(*l), color='b'), grid) - plot_line(l1s[0], 'g') + plot_line(l1s[0], 'r') plot_line(l1s[1], 'r') - pyplot.xlim(0, 520) pyplot.ylim(0, 390) pyplot.show() sys.exit() -lines_general = map(to_general, sum(lines, [])) - -near_points = [p for p in points if nearest(lines_general, p) <= 2] -pyplot.scatter(*zip(*near_points)) for l in lines[0]: