3 import Image, ImageDraw, ImageFilter
6 from manual import lines as g_grid, l2ad, intersection, line as g_line
7 from intrsc import intersections_from_angl_dist
8 from linef import line_from_angl_dist
11 class GridFittingFailedError(Exception):
14 class MyGaussianBlur(ImageFilter.Filter):
17 def __init__(self, radius=2):
19 def filter(self, image):
20 return image.gaussian_blur(self.radius)
22 def projection(point, line, vector):
23 return V(*intersection(g_line(point, point + vector.normal), g_line(*line)))
26 X, Y, im_l, a, b, c, d, s, v1, v2, k, hough, size = args
27 return [(distance(im_l,
28 get_grid(a + X[y] * s * v1,
31 size), a + X[y] * s * v1, b + Y[y] * s * v1) for y in range(2 *k)]
34 X, Y, im_l, a, b, c, d, s, v1, v2, k, hough, size = args
35 return [(distance(im_l,
36 get_grid(a, b, c + X[y] * s * v2,
39 size), c + X[y] * s * v2, d + Y[y] * s * v2) for y in range(2 *k)]
41 def find(lines, size, l1, l2, bounds, hough, do_something, im_h):
42 a, b, c, d = [V(*a) for a in bounds]
43 l1 = line_from_angl_dist(l1, size)
44 l2 = line_from_angl_dist(l2, size)
45 v1 = V(*l1[0]) - V(*l1[1])
46 v2 = V(*l2[0]) - V(*l2[1])
47 a = projection(a, l1, v1)
48 b = projection(b, l1, v1)
49 c = projection(c, l2, v2)
50 d = projection(d, l2, v2)
52 im_l = Image.new('L', size)
53 dr_l = ImageDraw.Draw(im_l)
54 for line in sum(lines, []):
55 dr_l.line(line_from_angl_dist(line, size), width=1, fill=255)
57 #im_l = im_h #hocus pocus
58 im_l = im_l.filter(MyGaussianBlur(radius=2))
59 #GaussianBlur is undocumented class, may not work in future versions of PIL
60 im_l_s = im_l.tostring()
62 #from gridf_analyzer import error_surface
63 #error_surface(im_l, a, b, c, d, hough, size, v1 ,v2)
65 grid = get_grid(a, b, c, d, hough, size)
66 dist = distance(im_l_s, grid, size)
68 #let's try the bruteforce aproach:
72 for i in range(-k, k):
73 X.append(range(-k, k))
76 pool = multiprocessing.Pool(None)
78 tasks = [(X[x], Y[x], im_l_s, a, b, c, d, s, v1, v2, k, hough, size) for x in xrange(0, 2 * k)]
82 opt_ab = pool.map(job_br1, tasks, 1)
83 opt_cd = pool.map(job_br2, tasks, 1)
84 an, bn, cn, dn = 4 * [0]
97 print time.time() - start
98 grid = get_grid(a, b, c, d, hough, size)
99 grid_lines = [[l2ad(l, size) for l in grid[0]], [l2ad(l, size) for l in grid[1]]]
103 im_t = Image.new('RGB', im_l.size, None)
106 for x in xrange(im_t.size[0]):
107 for y in xrange(im_t.size[1]):
108 im_t_l[x, y] = (im_l_l[x, y], 0, 0)
110 im_t_d = ImageDraw.Draw(im_t)
112 for l in grid[0] + grid[1]:
113 im_t_d.line(l, width=1, fill=(0, 255, 0))
115 do_something(im_t, "lines and grid")
120 return grid, grid_lines
122 def get_grid(a, b, c, d, hough, size):
123 l1 = hough.lines_from_list([a, b])
124 l2 = hough.lines_from_list([c, d])
125 c = intersections_from_angl_dist([l1, l2], size, get_all=True)
126 #TODO do something when a corner is outside the image
127 corners = (c[0] + c[1])
130 raise GridFittingFailedError
131 grid = g_grid(corners)
134 def line_out(line, size):
136 if p[0] < 0 or p[0] > size[0] or p[1] < 0 or p[1] > size[1]:
141 def distance(im_l, grid, size):
142 im_g = Image.new('L', size)
143 dr_g = ImageDraw.Draw(im_g)
144 for line in grid[0] + grid[1]:
145 dr_g.line(line, width=1, fill=255)
146 if line_out(line, size):
148 #im_g = im_g.filter(MyGaussianBlur(radius=3))
149 #GaussianBlur is undocumented class, may not work in future versions of PIL
150 #im_d, distance = combine(im_l, im_g)
151 distance = pcf.combine(im_l, im_g.tostring())