d39bdd52e8c07d50ff50d3b6939cf0d6a0adaf87
[imago.git] / gridf.py
1 """Imago grid-fitting module"""
2
3 import multiprocessing
4
5 import Image, ImageDraw, ImageFilter
6
7 from geometry import V, projection
8 from manual import lines as g_grid, l2ad
9 from intrsc import intersections_from_angl_dist
10 from linef import line_from_angl_dist
11 import pcf
12
13 class GridFittingFailedError(Exception):
14     pass
15
16 class MyGaussianBlur(ImageFilter.Filter):
17     name = "GaussianBlur"
18
19     def __init__(self, radius=2):
20         self.radius = radius
21     def filter(self, image):
22         return image.gaussian_blur(self.radius)
23
24 def job_br1(args):
25     X, Y, im_l, a, b, c, d, s, v1, v2, k, hough, size = args
26     return [(distance(im_l, 
27              get_grid(a + X[y] * s * v1, 
28                       b + Y[y] * s * v1, 
29                       c, d, hough, size),
30              size), a + X[y] * s * v1, b + Y[y] * s * v1) for y in range(2 *k)]
31
32 def job_br2(args):
33     X, Y, im_l, a, b, c, d, s, v1, v2, k, hough, size = args
34     return [(distance(im_l, 
35              get_grid(a, b, c + X[y] * s * v2, 
36                       d + Y[y] * s * v2, 
37                       hough, size),
38              size), c + X[y] * s * v2, d + Y[y] * s * v2) for y in range(2 *k)]
39
40 def find(lines, size, l1, l2, bounds, hough, do_something, im_h):
41     a, b, c, d = [V(*a) for a in bounds]
42     l1 = line_from_angl_dist(l1, size)
43     l2 = line_from_angl_dist(l2, size)
44     v1 = V(*l1[0]) - V(*l1[1])
45     v2 = V(*l2[0]) - V(*l2[1])
46     a = projection(a, l1, v1) 
47     b = projection(b, l1, v1) 
48     c = projection(c, l2, v2) 
49     d = projection(d, l2, v2) 
50
51     im_l = Image.new('L', size)
52     dr_l = ImageDraw.Draw(im_l)
53     for line in sum(lines, []):
54         dr_l.line(line_from_angl_dist(line, size), width=1, fill=255)
55
56     #im_l = im_h #hocus pocus    
57     im_l = im_l.filter(MyGaussianBlur(radius=2))
58     #GaussianBlur is undocumented class, may not work in future versions of PIL
59     im_l_s = im_l.tostring()
60
61     #from gridf_analyzer import error_surface
62     #error_surface(im_l, a, b, c, d, hough, size, v1 ,v2)
63
64     grid = get_grid(a, b, c, d, hough, size)
65     
66     #let's try the bruteforce aproach:
67     s = 0.001
68     k = 50
69     X, Y = [], []
70     for i in range(-k, k):
71         X.append(range(-k, k))
72         Y.append(2*k*[i])
73
74     pool = multiprocessing.Pool(None)
75
76     tasks = [(X[x], Y[x], im_l_s, a, b, c, d, s, 
77               v1, v2, k, hough, size) for x in xrange(0, 2 * k)]
78
79     import time
80     start = time.time()
81     opt_ab = pool.map(job_br1, tasks, 1)
82     opt_cd = pool.map(job_br2, tasks, 1)
83     d1 = 0
84     for lst in opt_ab:
85         for tpl in lst:
86             if tpl[0] > d1:
87                 d1 = tpl[0]
88                 a, b = tpl[1], tpl[2]
89     d1 = 0
90     for lst in opt_cd:
91         for tpl in lst:
92             if tpl[0] > d1:
93                 d1 = tpl[0]
94                 c, d = tpl[1], tpl[2]
95     print time.time() - start
96     grid = get_grid(a, b, c, d, hough, size) 
97     grid_lines = [[l2ad(l, size) for l in grid[0]], 
98                   [l2ad(l, size) for l in grid[1]]]
99     
100     ### 
101
102     im_t = Image.new('RGB', im_l.size, None)
103     im_t_l = im_t.load()
104     im_l_l = im_l.load()
105     for x in xrange(im_t.size[0]):
106         for y in xrange(im_t.size[1]):
107             im_t_l[x, y] = (im_l_l[x, y], 0, 0)
108
109     #im_t_d = ImageDraw.Draw(im_t)
110     #for l in grid[0] + grid[1]:
111     #    im_t_d.line(l, width=1, fill=(0, 255, 0))
112
113     #do_something(im_t, "lines and grid")
114
115
116     ###
117
118     return grid, grid_lines
119
120 def get_grid(a, b, c, d, hough, size):
121     l1 = hough.lines_from_list([a, b])
122     l2 = hough.lines_from_list([c, d])
123     c = intersections_from_angl_dist([l1, l2], size, get_all=True)
124     #TODO do something when a corner is outside the image
125     corners = (c[0] + c[1])
126     if len(corners) < 4:
127         print l1, l2, c
128         raise GridFittingFailedError
129     grid = g_grid(corners)
130     return grid
131
132 def line_out(line, size):
133     for p in line:
134         if p[0] < 0 or p[0] > size[0] or p[1] < 0 or p[1] > size[1]:
135             return True
136     else:
137         return False
138
139 def distance(im_l, grid, size):
140     im_g = Image.new('L', size)
141     dr_g = ImageDraw.Draw(im_g)
142     for line in grid[0] + grid[1]:
143         dr_g.line(line, width=1, fill=255)
144         if line_out(line, size):
145             return 0
146     #im_g = im_g.filter(MyGaussianBlur(radius=3))
147     #GaussianBlur is undocumented class, may not work in future versions of PIL
148     #im_d, distance = combine(im_l, im_g)
149     distance_d = pcf.combine(im_l, im_g.tostring())
150     return distance_d