1 """Imago grid-fitting module"""
4 from functools import partial
6 import Image, ImageDraw, ImageFilter
8 from geometry import V, projection
9 from manual import lines as g_grid, l2ad
10 from intrsc import intersections_from_angl_dist
11 from linef import line_from_angl_dist
15 class GridFittingFailedError(Exception):
18 class MyGaussianBlur(ImageFilter.Filter):
21 def __init__(self, radius=2):
23 def filter(self, image):
24 return image.gaussian_blur(self.radius)
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))
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)
44 v1, v2 = hough.lines_from_list([a, b])
45 h1, h2 = hough.lines_from_list([c, d])
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]
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)
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()
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)
65 x_v, y_v, x_h, y_h = pso.optimize(4, 30, f_dist, 32, 1028)
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)
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]]]
76 print time.time() - start
78 ### Show error surface
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)
85 ### Show grid over lines
87 im_t = Image.new('RGB', im_l.size, None)
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)
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))
98 do_something(im_t, "lines and grid")
101 return grid, grid_lines
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])
109 raise GridFittingFailedError
110 grid = g_grid(corners)
113 def line_out(line, size):
115 if p[0] < 0 or p[0] > size[0] or p[1] < 0 or p[1] > size[1]:
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):
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())