documentation
[imago.git] / imago_pack / linef.py
1 """Go image recognition lines-finding module"""
2
3 import sys
4 from math import sin, cos, pi
5
6 try:
7     import Image, ImageDraw
8 except ImportError, msg:
9     print >> sys.stderr, msg
10     sys.exit(1)
11
12 import filters
13 from hough import Hough
14
15 def find_lines(image, show_all, do_something, verbose):
16
17     if verbose:
18         print >> sys.stderr, "preprocessing"
19
20     if show_all:
21         do_something(image, "original image")
22
23     im_l = image.convert('L')
24     if show_all:
25         do_something(im_l, "ITU-R 601-2 luma transform")
26
27     if verbose:
28         print >> sys.stderr, "edge detection"
29
30     im_edges = filters.edge_detection(im_l)
31     if show_all:    
32         do_something(im_edges, "edge detection")
33
34     im_h = filters.high_pass(im_edges, 100)
35     if show_all:
36         do_something(im_h, "high pass filters")
37     
38     if verbose:
39         print >> sys.stderr, "hough transform"
40
41     hough1 = Hough(im_h.size)
42     im_hough = hough1.transform(im_h)
43     if show_all:
44         do_something(im_hough, "hough transform")
45
46    # im_hough = filters.peaks(im_hough)
47    # if show_all:
48    #     do_something(im_hough, "peak extraction")
49                
50     im_h2 = filters.high_pass(im_hough, 96)
51     if show_all:
52         do_something(im_h2, "second high pass filters")
53
54     im_h2 = filters.components2(im_h2)
55     if show_all:
56         do_something(im_h2, "components centers")
57
58     if verbose:
59         print >> sys.stderr, "second hough transform"
60
61     hough2 = Hough(im_h2.size) 
62     # im_hough might be used instead im_h2, but at the moment it brings a lot of
63     # noise to the second transform, which later confuses the center-finding
64     # mechanism (which is not very robust yet)
65     im_hough2 = hough2.transform(im_h2)
66     if show_all:
67         do_something(im_hough2, "second hough transform")
68
69     im_h3 = filters.high_pass(im_hough2, 120)
70     if show_all:
71         do_something(im_h3, "third high pass filter")
72      
73     im_h3 = filters.components(im_h3)
74     if show_all:
75         do_something(im_h3, "half centers")
76
77     if verbose:
78         print >> sys.stderr, "finding the grid"
79
80     lines_m = hough2.all_lines_h(im_h3)
81     lines = []
82     im_c = im_h2.convert('RGB').convert('RGB', (1, 0.5, 0.5, 0))
83     draw_c = ImageDraw.Draw(im_c)
84     bounds = []
85
86     for line_l in lines_m:
87         im_line = Image.new('L', im_h2.size)
88         draw = ImageDraw.Draw(im_line)
89         line_points = set()
90         for line in line_l:
91             draw.line(line_from_angl_dist(line, im_h2.size), fill=255, width=7)
92             draw_c.line(line_from_angl_dist(line, im_c.size), fill=(70, 70, 70), width=7)
93             for p in combine(im_h2, im_line):
94                 line_points.add(p)
95         for point in line_points:
96             draw_c.point(point, fill=(120, 255, 120))
97         lines.append(hough1.lines_from_list(line_points))
98         line_points = list(line_points)
99         line_points.sort()
100         bounds += [line_points[0], line_points[-1]]
101
102     if show_all:
103         do_something(im_c, "hough x lines")
104
105     image_g = image.copy()
106     draw = ImageDraw.Draw(image_g)
107     for line in [l for s in lines for l in s]:
108         draw.line(line_from_angl_dist(line, image.size), fill=(120, 255, 120))
109     if show_all:
110         do_something(image_g, "lines")
111    
112     return lines, lines_m[0][0], lines_m[1][0], bounds, hough1, im_h
113
114 def combine(image1, image2):
115     im_l1 = image1.load()
116     im_l2 = image2.load()
117
118     on_both = []
119
120     for x in xrange(image1.size[0]):
121         for y in xrange(image1.size[1]):
122             if im_l1[x, y] and im_l2[x, y]:
123                 on_both.append((x, y))
124     return on_both
125
126 def line_from_angl_dist((angle, distance), size):
127     if pi / 4 < angle < 3 * pi / 4:
128         y1 = - size[1] / 2
129         x1 = int(round((y1 * cos(angle) + distance) / sin(angle))) + size[0] / 2
130         y2 = size[1] / 2 
131         x2 = int(round((y2 * cos(angle) + distance) / sin(angle))) + size[0] / 2
132         return [(x1, 0), (x2, size[1])]
133     else:
134         x1 = - size[0] / 2
135         y1 = int(round((x1 * sin(angle) - distance) / cos(angle))) + size[1] / 2
136         x2 = size[0] / 2 
137         y2 = int(round((x2 * sin(angle) - distance) / cos(angle))) + size[1] / 2
138         return [(0, y1), (size[0], y2)]