eda82ceb4bc6aa813eeb5e720fbcee9faf620841
[imago.git] / imago_pack / linef.py
1 """Go image recognition lines-finding module"""
2
3 from functools import partial
4 import sys
5 from math import sin, cos, pi
6
7 try:
8     import Image, ImageDraw
9 except ImportError, msg:
10     print >> sys.stderr, msg
11     sys.exit(1)
12
13 import filters
14 from hough import Hough
15
16 def find_lines(image, show_all, do_something, verbose):
17
18     if verbose:
19         print >> sys.stderr, "preprocessing"
20
21     if show_all:
22         do_something(image, "original image")
23
24     im_l = image.convert('L')
25     if show_all:
26         do_something(im_l, "ITU-R 601-2 luma transform")
27
28     if verbose:
29         print >> sys.stderr, "edge detection"
30
31     im_edges = filters.edge_detection(im_l)
32     if show_all:    
33         do_something(im_edges, "edge detection")
34
35     im_h = filters.high_pass(im_edges, 100)
36     if show_all:
37         do_something(im_h, "high pass filters")
38     
39     if verbose:
40         print >> sys.stderr, "hough transform"
41
42     im_hough = Hough.Transform(im_h)
43     if show_all:
44         do_something(im_hough.image, "hough transform")
45
46    # im_hough.image = filters.peaks(im_hough.image)
47    # if show_all:
48    #     do_something(im_hough.image, "peak extraction")
49                
50     im_h2 = im_hough.apply_filter(partial(filters.high_pass, height=96))
51     if show_all:
52         do_something(im_h2.image, "second high pass filters")
53
54     im_h2 = im_h2.apply_filter(filters.components2)
55     if show_all:
56         do_something(im_h2.image, "components centers")
57
58     if verbose:
59         print >> sys.stderr, "second hough transform"
60
61     # im_hough might be used instead im_h2, but at the moment it brings a lot of
62     # noise to the second transform, which later confuses the center-finding
63     # mechanism (which is not very robust yet)
64     im_hough2 = Hough.Transform(im_h2.image)
65     if show_all:
66         do_something(im_hough2.image, "second hough transform")
67
68     im_h3 = im_hough2.apply_filter(partial(filters.high_pass, height=120))
69     if show_all:
70         do_something(im_h3.image, "third high pass filter")
71      
72     im_h3 = im_h3.apply_filter(filters.components)
73     if show_all:
74         do_something(im_h3.image, "half centers")
75
76     if verbose:
77         print >> sys.stderr, "finding the grid"
78
79     lines_m = im_h3.all_lines_h()
80     lines = []
81     im_c = im_h2.image.convert('RGB').convert('RGB', (1, 0.5, 0.5, 0))
82     draw_c = ImageDraw.Draw(im_c)
83     bounds = []
84
85     for line_l in lines_m:
86         im_line = Image.new('L', im_h2.size)
87         draw = ImageDraw.Draw(im_line)
88         line_points = set()
89         for line in line_l:
90             draw.line(line_from_angl_dist(line, im_h2.size), fill=255, width=7)
91             draw_c.line(line_from_angl_dist(line, im_c.size),
92                         fill=(70, 70, 70), width=7)
93             for p in combine(im_h2.image, 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(im_hough.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, im_hough
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)]