bug fixed in edge detection
[imago.git] / gridf.py
1 import Image, ImageDraw, ImageFilter
2
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
6
7 class GridFittingFailedError(Exception):
8     pass
9
10 class MyGaussianBlur(ImageFilter.Filter):
11     name = "GaussianBlur"
12
13     def __init__(self, radius=2):
14         self.radius = radius
15     def filter(self, image):
16         return image.gaussian_blur(self.radius)
17
18 class V():
19     def __init__(self, x, y):
20         self.x = x
21         self.y = y
22     
23     def __add__(self, other):
24         return V(self.x + other.x, self.y + other.y)
25
26     def __sub__(self, other):
27         return V(self.x - other.x, self.y - other.y)
28
29     def __rmul__(self, other):
30         return V(other * self.x, other * self.y)
31
32     def t(self):
33         return (self.x, self.y)
34
35     def normal(self):
36         return V(-self.y, self.x)
37
38 def projection(point, line, vector):
39     n = vector.normal()
40     l2 = g_line(point.t(), (point + n).t())
41     return V(*intersection(l2, g_line(*line)))
42     
43
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)
56     print dist
57     
58     s = 0.02
59     while True:
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]
69             t = distances[0][2]
70             a, b = a + t[0] * v1, b + t[1] * v1
71             print dist
72             s *= 0.75
73         else: 
74            break
75
76     print "---"
77
78     s = 0.02
79     while True:
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]
89             t = distances[0][2]
90             c, d = c + t[0] * v2, d + t[1] * v2
91             print dist
92             s *= 0.75
93         else:
94             break
95
96     grid_lines = [[l2ad(l, size) for l in grid[0]], [l2ad(l, size) for l in grid[1]]]
97     return grid, grid_lines
98
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])
104     if len(corners) < 4:
105         print l1, l2, c
106         raise GridFittingFailedError
107     grid = g_grid(corners)
108     return grid
109
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)
123     return distance
124
125 def combine(bg, fg):
126     bg_l = bg.load()
127     fg_l = fg.load()
128     res = Image.new('L', fg.size)
129     res_l = res.load()
130
131     score = 0
132     area = 0
133
134     for x in xrange(fg.size[0]):
135         for y in xrange(fg.size[1]):
136             if fg_l[x, y]:
137                 res_l[x, y] = bg_l[x, y] * fg_l[x, y]
138                 score +=  bg_l[x, y]
139                 area += 1
140
141     return res, float(score)/area