b09825778485e4cc0bbaeca7a3a7efaf3184f1c7
[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     if verbose:
48         print >> sys.stderr, "hough transform"
49
50     im_hough = Hough.Transform(im_h)
51     if show_all:
52         do_something(im_hough.image, "hough transform")
53
54    # im_hough.image = filters.peaks(im_hough.image)
55    # if show_all:
56    #     do_something(im_hough.image, "peak extraction")
57                
58     im_h2 = im_hough.apply_filter(partial(filters.high_pass, height=96))
59     if show_all:
60         do_something(im_h2.image, "second high pass filters")
61
62     im_h2 = im_h2.apply_filter(filters.components2)
63     if show_all:
64         do_something(im_h2.image, "components centers")
65
66     if verbose:
67         print >> sys.stderr, "second hough transform"
68
69     # im_hough might be used instead im_h2, but at the moment it brings a lot of
70     # noise to the second transform, which later confuses the center-finding
71     # mechanism (which is not very robust yet)
72     im_hough2 = Hough.Transform(im_h2.image)
73     if show_all:
74         do_something(im_hough2.image, "second hough transform")
75
76     im_h3 = im_hough2.apply_filter(partial(filters.high_pass, height=120))
77     if show_all:
78         do_something(im_h3.image, "third high pass filter")
79      
80     im_h3 = im_h3.apply_filter(filters.components)
81     if show_all:
82         do_something(im_h3.image, "half centers")
83
84     if verbose:
85         print >> sys.stderr, "finding the grid"
86
87     lines_m = im_h3.all_lines_h()
88     lines = []
89     im_c = im_h2.image.convert('RGB').convert('RGB', (1, 0.5, 0.5, 0))
90     draw_c = ImageDraw.Draw(im_c)
91     bounds = []
92
93     for line_l in lines_m:
94         im_line = Image.new('L', im_h2.size)
95         draw = ImageDraw.Draw(im_line)
96         line_points = set()
97         for line in line_l:
98             draw.line(line_from_angl_dist(line, im_h2.size), fill=255, width=7)
99             draw_c.line(line_from_angl_dist(line, im_c.size),
100                         fill=(70, 70, 70), width=7)
101             for p in combine(im_h2.image, im_line):
102                 line_points.add(p)
103         for point in line_points:
104             draw_c.point(point, fill=(120, 255, 120))
105         lines.append(im_hough.lines_from_list(line_points))
106         line_points = list(line_points)
107         line_points.sort()
108         bounds += [line_points[0], line_points[-1]]
109
110     if show_all:
111         do_something(im_c, "hough x lines")
112
113     image_g = image.copy()
114     draw = ImageDraw.Draw(image_g)
115     for line in [l for s in lines for l in s]:
116         draw.line(line_from_angl_dist(line, image.size), fill=(120, 255, 120))
117     if show_all:
118         do_something(image_g, "lines")
119    
120     return lines, lines_m[0][0], lines_m[1][0], bounds, im_hough
121
122 def combine(image1, image2):
123     """Return a list of points that are present in both images."""
124     im_l1 = image1.load()
125     im_l2 = image2.load()
126
127     on_both = []
128
129     for x in xrange(image1.size[0]):
130         for y in xrange(image1.size[1]):
131             if im_l1[x, y] and im_l2[x, y]:
132                 on_both.append((x, y))
133     return on_both
134
135 def line_from_angl_dist((angle, distance), size):
136     """Take *angle* and *distance* (from the center of the image) of a line and
137     size of the image. Return the line represented by two points."""
138     if pi / 4 < angle < 3 * pi / 4:
139         y1 = - size[1] / 2
140         x1 = int(round((y1 * cos(angle) + distance) / sin(angle))) + size[0] / 2
141         y2 = size[1] / 2 
142         x2 = int(round((y2 * cos(angle) + distance) / sin(angle))) + size[0] / 2
143         return [(x1, 0), (x2, size[1])]
144     else:
145         x1 = - size[0] / 2
146         y1 = int(round((x1 * sin(angle) - distance) / cos(angle))) + size[1] / 2
147         x2 = size[0] / 2 
148         y2 = int(round((x2 * sin(angle) - distance) / cos(angle))) + size[1] / 2
149         return [(0, y1), (size[0], y2)]