new gridf almost working
authorTomas Musil <tomik.musil@gmail.com>
Tue, 1 Jul 2014 03:21:41 +0000 (05:21 +0200)
committerTomas Musil <tomik.musil@gmail.com>
Tue, 1 Jul 2014 03:21:41 +0000 (05:21 +0200)
new_geometry.py
ransac_grid.py

index 148430b..ab62210 100644 (file)
@@ -39,11 +39,17 @@ def fill(l1, l2, lh, n):
         if n == 2:
             return [nl, nr]
         elif n == 8:
-            return (fill(l1, nl, lh, 2) +
+            return (fill(l1, nl, 
+                         (intersection(l1, lh),
+                          intersection(nl, lh)), 2) +
                     [nl] +
-                    fill(nl, nr, lh, 2) +
+                    fill(nl, nr, 
+                         (intersection(nl, lh),
+                          intersection(nr, lh)), 2) +
                     [nr] +
-                    fill(nr, l2, lh, 2))
+                    fill(nr, l2, 
+                         (intersection(nr, lh),
+                          intersection(l2, lh)), 2))
         
 def expand_right(grid, middle):
     return expand(grid[-2], grid[-1], 
index 4182555..a483a9e 100644 (file)
@@ -3,6 +3,7 @@ import matplotlib.pyplot as pyplot
 from math import sqrt
 import random
 import sys
+import time
 
 import src.linef as linef
 import src.gridf as gridf
@@ -10,6 +11,8 @@ from src.manual import lines as g_grid
 
 import new_geometry as gm
 
+random.seed(12345)
+
 def plot_line(line, c):
     points = linef.line_from_angl_dist(line, (520, 390))
     pyplot.plot(*zip(*points), color=c)
@@ -32,20 +35,23 @@ def nearest2(lines, point):
 
 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))
+def generate_models(sgrid, lh):
     for f in [0, 1, 2, 3, 5, 7, 8, 11, 15, 17]:
         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
+            grid = [gm.expand_left(grid, lh)] + grid
         yield grid
         for i in xrange(17 - f):
             grid = grid[1:]
-            grid.append(gm.expand_right(grid, middle))
+            grid.append(gm.expand_right(grid, lh))
             yield grid
 
+def score(grid, points, limit):
+    d = max(map(lambda l: dst((0, 0), points_to_line(*l)), grid + grid))
+    if d > limit:
+        return 0
+    return len([p for p in points if nearest2(grid, p) <= 2])
 
 points = pickle.load(open('edges.pickle'))
 
@@ -60,20 +66,34 @@ 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)
+    t0 = time.time()
+    #l1s = random.sample(l1, 2)
+    l1s = [l1[0], l1[-1]]
     l1s.sort(key=lambda l: l[1])
-    middle = ((0, 195),(520, 195))
-    # TODO! can I assume anything to be perspectively disorted square? No.
-    # TODO! take lower and middle and construct top
-    # TODO iterate that ^^^?
-    
-    sc, grid = max(map(lambda g: (score(g), g), generate_models(l1s, middle)))
-    pyplot.scatter(*zip(*near_points))
+    sgrid = map(lambda l:linef.line_from_angl_dist(l, size), l1s) 
+    middle = lambda m: ((m, 0),(m, 390))
+    middle = middle(gm.intersection((sgrid[0][0], sgrid[1][1]), 
+                                    (sgrid[0][1], sgrid[1][0]))[0])
+    lh = (gm.intersection(sgrid[0], middle), gm.intersection(sgrid[1], middle))
+    sc, grid = max(map(lambda g: (score(g, points, 400), g), generate_models(sgrid, lh)))
     map(lambda l: pyplot.plot(*zip(*l), color='b'), grid)
+    #l2s = random.sample(l2, 2)
+    l2s = [l2[0], l2[-1]]
+    l2s.sort(key=lambda l: l[1])
+    sgrid = map(lambda l:linef.line_from_angl_dist(l, size), l2s) 
+    middle = lambda m: ((0, m),(520, m))
+    middle = middle(gm.intersection((sgrid[0][0], sgrid[1][1]), 
+                                    (sgrid[0][1], sgrid[1][0]))[1])
+    lh = (gm.intersection(sgrid[0], middle), gm.intersection(sgrid[1], middle))
+    sc, grid = max(map(lambda g: (score(g, points, 530), g), generate_models(sgrid, lh)))
+    print time.time() - t0
 
+    pyplot.scatter(*zip(*near_points))
+    map(lambda l: pyplot.plot(*zip(*l), color='b'), grid)
+    plot_line(l2s[0], 'r')
+    plot_line(l2s[1], 'r')
     plot_line(l1s[0], 'r')
     plot_line(l1s[1], 'r')
     pyplot.xlim(0, 520)