e95788079b41cc8bdfa37b7e1255c0136466dc95
[imago.git] / gridf.py
1 """Imago grid-fitting module"""
2
3 import multiprocessing
4 from functools import partial
5
6 import Image, ImageDraw, ImageFilter
7
8 from geometry import V, projection, l2ad
9 from manual import lines as g_grid
10 from intrsc import intersections_from_angl_dist
11 from linef import line_from_angl_dist
12 import pcf
13 import pso
14
15 class GridFittingFailedError(Exception):
16     pass
17
18 class MyGaussianBlur(ImageFilter.Filter):
19     name = "GaussianBlur"
20
21     def __init__(self, radius=2):
22         self.radius = radius
23     def filter(self, image):
24         return image.gaussian_blur(self.radius)
25
26 def job_4(x, y, w, z, im_l, v1, v2, h1, h2, dv, dh, size):
27     v1 = (v1[0] + x * dv, v1[1] + x)
28     v2 = (v2[0] + y * dv, v2[1] + y)
29     h1 = (h1[0] + w * dh, h1[1] + w)
30     h2 = (h2[0] + z * dh, h2[1] + z)
31     return (distance(im_l, get_grid([v1, v2], [h1, h2], size), size))
32
33 def find(lines, size, l1, l2, bounds, hough, do_something, im_h):
34     l1 = line_from_angl_dist(l1, size)
35     l2 = line_from_angl_dist(l2, size)
36     v1 = V(*l1[0]) - V(*l1[1])
37     v2 = V(*l2[0]) - V(*l2[1])
38     a, b, c, d = [V(*a) for a in bounds]
39     a = projection(a, l1, v1) 
40     b = projection(b, l1, v1) 
41     c = projection(c, l2, v2) 
42     d = projection(d, l2, v2) 
43     
44     v1, v2 = hough.lines_from_list([a, b])
45     h1, h2 = hough.lines_from_list([c, d])
46
47     delta_v = ((l1[1][1] - l1[0][1]) * hough.dt) / l1[1][0]
48     delta_h = ((l2[1][1] - l2[0][1]) * hough.dt) / l2[1][0]
49
50     im_l = Image.new('L', size)
51     dr_l = ImageDraw.Draw(im_l)
52     for line in sum(lines, []):
53         dr_l.line(line_from_angl_dist(line, size), width=1, fill=255)
54
55     im_l = im_l.filter(MyGaussianBlur(radius=5))
56     #GaussianBlur is undocumented class, may not work in future versions of PIL
57     im_l_s = im_l.tostring()
58
59     import time
60     start = time.time()
61
62     f_dist = partial(job_4, im_l=im_l_s, v1=v1, v2=v2, h1=h1, h2=h2,
63                      dv=delta_v, dh=delta_h, size=size)
64
65     x_v, y_v, x_h, y_h = pso.optimize(4, 30, f_dist, 32, 1028)
66
67     v1 = (v1[0] + x_v * delta_v, v1[1] + x_v)
68     v2 = (v2[0] + y_v * delta_v, v2[1] + y_v)
69     h1 = (h1[0] + x_h * delta_h, h1[1] + x_h)
70     h2 = (h2[0] + y_h * delta_h, h2[1] + y_h)
71
72     grid = get_grid([v1, v2], [h1, h2], size) 
73     grid_lines = [[l2ad(l, size) for l in grid[0]], 
74                   [l2ad(l, size) for l in grid[1]]]
75     
76     print time.time() - start
77     
78 ### Show error surface
79 #
80 #    from gridf_analyzer import error_surface
81 #    error_surface(k, im_l_s, v1_i, v2_i, h1_i, h2_i, 
82 #                  delta_v, delta_h, x_v, y_v, x_h, y_h, size)
83 ###
84
85 ### Show grid over lines
86 #
87     im_t = Image.new('RGB', im_l.size, None)
88     im_t_l = im_t.load()
89     im_l_l = im_l.load()
90     for x in xrange(im_t.size[0]):
91         for y in xrange(im_t.size[1]):
92             im_t_l[x, y] = (im_l_l[x, y], 0, 0)
93
94     im_t_d = ImageDraw.Draw(im_t)
95     for l in grid[0] + grid[1]:
96         im_t_d.line(l, width=1, fill=(0, 255, 0))
97
98     do_something(im_t, "lines and grid")
99 ###
100
101     return grid, grid_lines
102
103 def get_grid(l1, l2, size):
104     c = intersections_from_angl_dist([l1, l2], size, get_all=True)
105     #TODO do something when a corner is outside the image
106     corners = (c[0] + c[1])
107     if len(corners) < 4:
108         print l1, l2, c
109         raise GridFittingFailedError
110     grid = g_grid(corners)
111     return grid
112
113 def line_out(line, size):
114     for p in line:
115         if p[0] < 0 or p[0] > size[0] or p[1] < 0 or p[1] > size[1]:
116             return True
117     else:
118         return False
119
120 def distance(im_l, grid, size):
121     im_g = Image.new('L', size)
122     dr_g = ImageDraw.Draw(im_g)
123     for line in grid[0] + grid[1]:
124         dr_g.line(line, width=1, fill=255)
125         if line_out(line, size):
126             return 0
127     #im_g = im_g.filter(MyGaussianBlur(radius=3))
128     #GaussianBlur is undocumented class, may not work in future versions of PIL
129     #im_d, distance = combine(im_l, im_g)
130     distance_d = pcf.combine(im_l, im_g.tostring())
131     return distance_d