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