output bug fixed
[imago.git] / gridf.py
index 406285c..e957880 100644 (file)
--- a/gridf.py
+++ b/gridf.py
@@ -1,11 +1,16 @@
+"""Imago grid-fitting module"""
+
 import multiprocessing
 import multiprocessing
+from functools import partial
 
 import Image, ImageDraw, ImageFilter
 
 
 import Image, ImageDraw, ImageFilter
 
-from manual import lines as g_grid, l2ad, intersection, line as g_line
+from geometry import V, projection, l2ad
+from manual import lines as g_grid
 from intrsc import intersections_from_angl_dist
 from linef import line_from_angl_dist
 import pcf
 from intrsc import intersections_from_angl_dist
 from linef import line_from_angl_dist
 import pcf
+import pso
 
 class GridFittingFailedError(Exception):
     pass
 
 class GridFittingFailedError(Exception):
     pass
@@ -18,221 +23,84 @@ class MyGaussianBlur(ImageFilter.Filter):
     def filter(self, image):
         return image.gaussian_blur(self.radius)
 
     def filter(self, image):
         return image.gaussian_blur(self.radius)
 
-class V(object):
-    def __init__(self, x, y):
-        self.x = x
-        self.y = y
-    
-    def __add__(self, other):
-        return V(self.x + other.x, self.y + other.y)
-
-    def __sub__(self, other):
-        return V(self.x - other.x, self.y - other.y)
-
-    def __rmul__(self, other):
-        return V(other * self.x, other * self.y)
-
-    def __len__(self):
-        return 2;
-
-    def __getitem__(self, key):
-        if key == 0:
-            return self.x
-        elif key == 1:
-            return self.y
-        elif type(key) != int:
-            raise TypeError("V indices must be integers") 
-        else:
-            raise KeyError("V index ({}) out of range".format(key))
-
-    def __iter__(self):
-        yield self.x
-        yield self.y
-
-    @property
-    def normal(self):
-        return V(-self.y, self.x)
-
-def projection(point, line, vector):
-    return V(*intersection(g_line(point, point + vector.normal), g_line(*line)))
-
-def job(args):
-    X, Y, im_l, a, b, c, d, s, v1, k, hough, size = args
-    return [distance(im_l, 
-                     get_grid(a + X[y] * s * v1, 
-                              b + Y[y] * s * v1, 
-                              c, d, hough, size),
-                     size) for y in range(2 * k)]
-
-def job_br1(args):
-    X, Y, im_l, a, b, c, d, s, v1, v2, k, hough, size = args
-    return [(distance(im_l, 
-                     get_grid(a + X[y] * s * v1, 
-                              b + Y[y] * s * v1, 
-                              c, d, hough, size),
-                     size), a + X[y] * s * v1, b + Y[y] * s * v1) for y in range(2 *k)]
-
-def job_br2(args):
-    X, Y, im_l, a, b, c, d, s, v1, v2, k, hough, size = args
-    return [(distance(im_l, 
-                     get_grid(a, b, c + X[y] * s * v2, 
-                              d + Y[y] * s * v2, 
-                              hough, size),
-                     size), c + X[y] * s * v2, d + Y[y] * s * v2) for y in range(2 *k)]
+def job_4(x, y, w, z, im_l, v1, v2, h1, h2, dv, dh, size):
+    v1 = (v1[0] + x * dv, v1[1] + x)
+    v2 = (v2[0] + y * dv, v2[1] + y)
+    h1 = (h1[0] + w * dh, h1[1] + w)
+    h2 = (h2[0] + z * dh, h2[1] + z)
+    return (distance(im_l, get_grid([v1, v2], [h1, h2], size), size))
 
 
-def error_surface(im_l, a, b, c, d, hough, size, v1):
-    import matplotlib.pyplot as plt
-    from matplotlib import cm
-    import time
-    import sys
-    import pickle
-
-    X = []
-    Y = []
-    Z = []
-    s = 0.001
-    k = 250
-    for i in range(-k, k):
-        X.append(range(-k, k))
-        Y.append(2*k*[i])
-
-    tasks = [(X[x], Y[x], im_l, a, b, c, d, s, v1, k, hough, size) for x in xrange(0, 2 * k)]
-    #everything is passed by value here; can it somehow be passed by reference?
-
-    pool = multiprocessing.Pool(None)
-    
-    start = time.time()
-    Z = pool.map(job, tasks, 1)
-    print time.time() - start
-
-    s_file = open('surface' + str(k), 'w')
-    pickle.dump((X, Y, Z), s_file)
-    s_file.close()
-    plt.imshow(Z, cmap=cm.jet, interpolation='bicubic', 
-               origin='upper', extent=(-k, k, -k, k), aspect='equal')
-    plt.colorbar()
-
-    plt.show()
-
-    sys.exit()
-
-def find(lines, size, l1, l2, bounds, hough, do_something):
-    a, b, c, d = [V(*a) for a in bounds]
+def find(lines, size, l1, l2, bounds, hough, do_something, im_h):
     l1 = line_from_angl_dist(l1, size)
     l2 = line_from_angl_dist(l2, size)
     v1 = V(*l1[0]) - V(*l1[1])
     v2 = V(*l2[0]) - V(*l2[1])
     l1 = line_from_angl_dist(l1, size)
     l2 = line_from_angl_dist(l2, size)
     v1 = V(*l1[0]) - V(*l1[1])
     v2 = V(*l2[0]) - V(*l2[1])
+    a, b, c, d = [V(*a) for a in bounds]
     a = projection(a, l1, v1) 
     b = projection(b, l1, v1) 
     c = projection(c, l2, v2) 
     d = projection(d, l2, v2) 
     a = projection(a, l1, v1) 
     b = projection(b, l1, v1) 
     c = projection(c, l2, v2) 
     d = projection(d, l2, v2) 
+    
+    v1, v2 = hough.lines_from_list([a, b])
+    h1, h2 = hough.lines_from_list([c, d])
+
+    delta_v = ((l1[1][1] - l1[0][1]) * hough.dt) / l1[1][0]
+    delta_h = ((l2[1][1] - l2[0][1]) * hough.dt) / l2[1][0]
 
     im_l = Image.new('L', size)
     dr_l = ImageDraw.Draw(im_l)
     for line in sum(lines, []):
         dr_l.line(line_from_angl_dist(line, size), width=1, fill=255)
 
     im_l = Image.new('L', size)
     dr_l = ImageDraw.Draw(im_l)
     for line in sum(lines, []):
         dr_l.line(line_from_angl_dist(line, size), width=1, fill=255)
-    im_l = im_l.filter(MyGaussianBlur(radius=30))
-    #GaussianBlur is undocumented class, may not work in future versions of PIL
-    im_l = im_l.tostring()
-
-    #error_surface(im_l, a, b, c, d, hough, size, v1)
-
-    grid = get_grid(a, b, c, d, hough, size)
-    dist = distance(im_l, grid, size)
-    
-    #let's try the bruteforce aproach:
-    s = 0.001
-    k = 50
-    X, Y = [], []
-    for i in range(-k, k):
-        X.append(range(-k, k))
-        Y.append(2*k*[i])
-
-    tasks = [(X[x], Y[x], im_l, a, b, c, d, s, v1, v2, k, hough, size) for x in xrange(0, 2 * k)]
-
-    pool = multiprocessing.Pool(None)
-    
-    #start = time.time()
-    opt_ab = pool.map(job_br1, tasks, 1)
-    opt_cd = pool.map(job_br2, tasks, 1)
-    an, bn, cn, dn = 4 * [0]
-    d1 = 0
-    for lst in opt_ab:
-        for tpl in lst:
-            if tpl[0] > d1:
-                d1 = tpl[0]
-                an, bn = tpl[1], tpl[2]
-    d1 = 0
-    for lst in opt_cd:
-        for tpl in lst:
-            if tpl[0] > d1:
-                d1 = tpl[0]
-                cn, dn = tpl[1], tpl[2]
-    #print time.time() - start
-    grid = get_grid(an, bn, cn, dn, hough, size) 
-    grid_lines = [[l2ad(l, size) for l in grid[0]], [l2ad(l, size) for l in grid[1]]]
-    return grid, grid_lines
-
-    #old optimization experiments: 
-    print dist
-   
-    path = [(0,0)] #MNTR
-    s = 0.01
-    for _ in range(10):
-        ts1 = [(s, 0), (0, s), (-s, 0), (0, -s)]
-        grids = [(get_grid(a + t[0] * v1, b + t[1] * v1, 
-                           c, d, hough, size), t) for t in ts1]
-        distances = [distance(im_l, grid, size) for (grid, t) in grids]
-        gradient = [(di - dist) for di in distances]
-        gradient = [gradient[0] - gradient[2], gradient[1] - gradient[3]]
-        norm = (gradient[0] ** 2 + gradient[1] ** 2) ** 0.5
-        gradient = [g / (100 * norm) for g in gradient]
-        path.append(gradient)
-        a, b = a + gradient[0] * v1, b + gradient[1] * v1
-        dist = distance(im_l, grid, size)
-        print dist
 
 
-    ###MNTR
-    import matplotlib.pyplot as plt
-    from matplotlib import cm
-    import pickle
+    im_l = im_l.filter(MyGaussianBlur(radius=5))
+    #GaussianBlur is undocumented class, may not work in future versions of PIL
+    im_l_s = im_l.tostring()
 
 
-    X, Y, Z = pickle.load(open('surface250'))
+    import time
+    start = time.time()
 
 
-    plt.imshow(Z, cmap=cm.jet, interpolation='none', 
-               origin='upper', extent=(-0.250, 0.250, -0.250, 0.250), aspect='equal')
-    plt.colorbar()
-    plt.plot([y for (x, y) in path], [x for (x, y) in path], 'go-')
+    f_dist = partial(job_4, im_l=im_l_s, v1=v1, v2=v2, h1=h1, h2=h2,
+                     dv=delta_v, dh=delta_h, size=size)
 
 
-    plt.show()
-    ###MNTR
+    x_v, y_v, x_h, y_h = pso.optimize(4, 30, f_dist, 32, 1028)
 
 
-    print "---"
+    v1 = (v1[0] + x_v * delta_v, v1[1] + x_v)
+    v2 = (v2[0] + y_v * delta_v, v2[1] + y_v)
+    h1 = (h1[0] + x_h * delta_h, h1[1] + x_h)
+    h2 = (h2[0] + y_h * delta_h, h2[1] + y_h)
 
 
-    s = 0.02
-    while True:
-        ts1 = [(s, 0), (-s, 0), (s, s), (-s, -s), (-s, s), (s, -s), (0, s),  (0, -s)]
-        grids = [(get_grid(a, b, 
-                           c + t[0] * v2, d + t[1] * v2, hough, size), t) for t in ts1]
-        distances = [(distance(im_l, grid, size), 
-                      grid, t) for grid, t in grids]
-        distances.sort(reverse=True)
-        if distances[0][0] > dist:
-            dist = distances[0][0]
-            grid = distances[0][1]
-            t = distances[0][2]
-            c, d = c + t[0] * v2, d + t[1] * v2
-            print dist
-            s *= 0.75
-        else:
-            break
+    grid = get_grid([v1, v2], [h1, h2], size) 
+    grid_lines = [[l2ad(l, size) for l in grid[0]], 
+                  [l2ad(l, size) for l in grid[1]]]
+    
+    print time.time() - start
+    
+### Show error surface
+#
+#    from gridf_analyzer import error_surface
+#    error_surface(k, im_l_s, v1_i, v2_i, h1_i, h2_i, 
+#                  delta_v, delta_h, x_v, y_v, x_h, y_h, size)
+###
+
+### Show grid over lines
+#
+    im_t = Image.new('RGB', im_l.size, None)
+    im_t_l = im_t.load()
+    im_l_l = im_l.load()
+    for x in xrange(im_t.size[0]):
+        for y in xrange(im_t.size[1]):
+            im_t_l[x, y] = (im_l_l[x, y], 0, 0)
+
+    im_t_d = ImageDraw.Draw(im_t)
+    for l in grid[0] + grid[1]:
+        im_t_d.line(l, width=1, fill=(0, 255, 0))
+
+    do_something(im_t, "lines and grid")
+###
 
 
-    grid_lines = [[l2ad(l, size) for l in grid[0]], [l2ad(l, size) for l in grid[1]]]
     return grid, grid_lines
 
     return grid, grid_lines
 
-def get_grid(a, b, c, d, hough, size):
-    l1 = hough.lines_from_list([a, b])
-    l2 = hough.lines_from_list([c, d])
+def get_grid(l1, l2, size):
     c = intersections_from_angl_dist([l1, l2], size, get_all=True)
     #TODO do something when a corner is outside the image
     corners = (c[0] + c[1])
     c = intersections_from_angl_dist([l1, l2], size, get_all=True)
     #TODO do something when a corner is outside the image
     corners = (c[0] + c[1])
@@ -242,32 +110,22 @@ def get_grid(a, b, c, d, hough, size):
     grid = g_grid(corners)
     return grid
 
     grid = g_grid(corners)
     return grid
 
+def line_out(line, size):
+    for p in line:
+        if p[0] < 0 or p[0] > size[0] or p[1] < 0 or p[1] > size[1]:
+            return True
+    else:
+        return False
+
 def distance(im_l, grid, size):
     im_g = Image.new('L', size)
     dr_g = ImageDraw.Draw(im_g)
     for line in grid[0] + grid[1]:
         dr_g.line(line, width=1, fill=255)
 def distance(im_l, grid, size):
     im_g = Image.new('L', size)
     dr_g = ImageDraw.Draw(im_g)
     for line in grid[0] + grid[1]:
         dr_g.line(line, width=1, fill=255)
+        if line_out(line, size):
+            return 0
     #im_g = im_g.filter(MyGaussianBlur(radius=3))
     #GaussianBlur is undocumented class, may not work in future versions of PIL
     #im_d, distance = combine(im_l, im_g)
     #im_g = im_g.filter(MyGaussianBlur(radius=3))
     #GaussianBlur is undocumented class, may not work in future versions of PIL
     #im_d, distance = combine(im_l, im_g)
-    distance = pcf.combine(im_l, im_g.tostring())
-    return distance
-
-def combine(bg, fg):
-    bg_l = bg.load()
-    fg_l = fg.load()
-    #res = Image.new('L', fg.size)
-    #res_l = res.load()
-
-    score = 0
-    area = 0
-
-    for x in xrange(fg.size[0]):
-        for y in xrange(fg.size[1]):
-            if fg_l[x, y]:
-                #res_l[x, y] = bg_l[x, y] * fg_l[x, y]
-                score +=  bg_l[x, y]
-                area += 1
-
-    #return res, float(score)/area
-    return None, float(score)/area
+    distance_d = pcf.combine(im_l, im_g.tostring())
+    return distance_d