1 import Image, ImageDraw, ImageFilter
3 from manual import lines as g_grid, l2ad, intersection, line as g_line
4 from intrsc import intersections_from_angl_dist
5 from linef import line_from_angl_dist
7 class GridFittingFailedError(Exception):
10 class MyGaussianBlur(ImageFilter.Filter):
13 def __init__(self, radius=2):
15 def filter(self, image):
16 return image.gaussian_blur(self.radius)
19 def __init__(self, x, y):
23 def __add__(self, other):
24 return V(self.x + other.x, self.y + other.y)
26 def __sub__(self, other):
27 return V(self.x - other.x, self.y - other.y)
29 def __rmul__(self, other):
30 return V(other * self.x, other * self.y)
33 return (self.x, self.y)
36 return V(-self.y, self.x)
38 def projection(point, line, vector):
40 l2 = g_line(point.t(), (point + n).t())
41 return V(*intersection(l2, g_line(*line)))
44 def find(lines, size, l1, l2, bounds, hough, do_something):
45 a, b, c, d = [V(*a) for a in bounds]
46 l1 = line_from_angl_dist(l1, size)
47 l2 = line_from_angl_dist(l2, size)
48 v1 = V(*l1[0]) - V(*l1[1])
49 v2 = V(*l2[0]) - V(*l2[1])
50 a = projection(a, l1, v1)
51 b = projection(b, l1, v1)
52 c = projection(c, l2, v2)
53 d = projection(d, l2, v2)
54 grid = get_grid(a, b, c, d, hough, size)
55 dist = distance(lines, grid, size)
60 ts1 = [(s, 0), (-s, 0), (s, s), (-s, -s), (-s, s), (s, -s), (0, s), (0, -s)]
61 grids = [(get_grid(a + t[0] * v1, b + t[1] * v1,
62 c, d, hough, size), t) for t in ts1]
63 distances = [(distance(lines, grid, size),
64 grid, t) for grid, t in grids]
65 distances.sort(reverse=True)
66 if distances[0][0] > dist:
67 dist = distances[0][0]
68 grid = distances[0][1]
70 a, b = a + t[0] * v1, b + t[1] * v1
80 ts1 = [(s, 0), (-s, 0), (s, s), (-s, -s), (-s, s), (s, -s), (0, s), (0, -s)]
81 grids = [(get_grid(a, b,
82 c + t[0] * v2, d + t[1] * v2, hough, size), t) for t in ts1]
83 distances = [(distance(lines, grid, size),
84 grid, t) for grid, t in grids]
85 distances.sort(reverse=True)
86 if distances[0][0] > dist:
87 dist = distances[0][0]
88 grid = distances[0][1]
90 c, d = c + t[0] * v2, d + t[1] * v2
96 grid_lines = [[l2ad(l, size) for l in grid[0]], [l2ad(l, size) for l in grid[1]]]
97 return grid, grid_lines
99 def get_grid(a, b, c, d, hough, size):
100 l1 = hough.lines_from_list([a.t(), b.t()])
101 l2 = hough.lines_from_list([c.t(), d.t()])
102 c = intersections_from_angl_dist([l1, l2], size, get_all=True)
103 corners = (c[0] + c[1])
106 raise GridFittingFailedError
107 grid = g_grid(corners)
110 def distance(lines, grid, size):
111 im_l = Image.new('L', size)
112 dr_l = ImageDraw.Draw(im_l)
113 for line in sum(lines, []):
114 dr_l.line(line_from_angl_dist(line, size), width=1, fill=255)
115 im_l = im_l.filter(MyGaussianBlur(radius=3))
116 # GaussianBlur is undocumented class, may not work in future versions of PIL
117 im_g = Image.new('L', size)
118 dr_g = ImageDraw.Draw(im_g)
119 for line in grid[0] + grid[1]:
120 dr_g.line(line, width=1, fill=255)
121 #im_g = im_g.filter(MyGaussianBlur(radius=3))
122 im_d, distance = combine(im_l, im_g)
128 res = Image.new('L', fg.size)
134 for x in xrange(fg.size[0]):
135 for y in xrange(fg.size[1]):
137 res_l[x, y] = bg_l[x, y] * fg_l[x, y]
141 return res, float(score)/area