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