fb83db45671cbea77ff667813da03ceac6f723d6
[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_image, verbose):
17     # TODO comment
18     im_l = image.convert('L')
19     show_image(im_l, "ITU-R 601-2 luma transform")
20
21     if verbose:
22         print >> sys.stderr, "edge detection"
23     im_edges = filters.edge_detection(im_l)
24     show_image(im_edges, "edge detection")
25
26     im_h = filters.high_pass(im_edges, 100)
27     show_image(im_h, "high pass filters")
28
29     return im_h
30  
31 def transform(image, hough, show_image):
32     # TODO comment
33     im_hough = hough.transform(image)
34     show_image(im_hough, "hough transform")
35
36    # im_hough.image = filters.peaks(im_hough.image)
37    # if show_all:
38    #     do_something(im_hough.image, "peak extraction")
39                
40     im_h2 = filters.high_pass(im_hough, 96)
41     show_image(im_h2, "second high pass filters")
42
43     im_h2 = filters.components2(im_h2)
44     show_image(im_h2, "components centers")
45
46     return im_h2
47
48 def find_lines(image, show_all, do_something, verbose):
49     """Find lines in the *image*."""
50     # TODO refactor into smaller functions
51
52     def nothing(a, b):
53         pass
54
55     if show_all:
56         show_image = do_something
57     else:
58         show_image = nothing
59
60     if verbose:
61         print >> sys.stderr, "preprocessing"
62
63     show_image(image, "original image")
64
65     im_h = prepare(image, show_image, verbose)
66
67     hough = Hough.default(im_h)
68
69     if verbose:
70         print >> sys.stderr, "hough transform"
71     
72     im_h2 = transform(im_h, hough, show_image)
73
74     if verbose:
75         print >> sys.stderr, "second hough transform"
76
77     # im_hough might be used instead im_h2, but at the moment it brings a lot of
78     # noise to the second transform, which later confuses the center-finding
79     # mechanism (which is not very robust yet)
80     hough2 = Hough.default(im_h2)
81     im_hough2 = hough2.transform(im_h2)
82     show_image(im_hough2, "second hough transform")
83
84     im_h3 = filters.high_pass(im_hough2, 120)
85     show_image(im_h3, "third high pass filter")
86      
87     im_h3 = filters.components(im_h3)
88     show_image(im_h3, "half centers")
89
90     if verbose:
91         print >> sys.stderr, "finding the grid"
92
93     lines_m = hough2.all_lines_h(im_h3)
94     lines = []
95     im_c = im_h2.convert('RGB').convert('RGB', (1, 0.5, 0.5, 0))
96     draw_c = ImageDraw.Draw(im_c)
97     bounds = []
98
99     for line_l in lines_m:
100         im_line = Image.new('L', im_h2.size)
101         draw = ImageDraw.Draw(im_line)
102         line_points = set()
103         for line in line_l:
104             draw.line(line_from_angl_dist(line, im_h2.size), fill=255, width=7)
105             draw_c.line(line_from_angl_dist(line, im_c.size),
106                         fill=(70, 70, 70), width=7)
107             for p in combine(im_h2, im_line):
108                 line_points.add(p)
109         for point in line_points:
110             draw_c.point(point, fill=(120, 255, 120))
111         lines.append(hough.lines_from_list(line_points))
112         line_points = list(line_points)
113         line_points.sort()
114         bounds += [line_points[0], line_points[-1]]
115
116     show_image(im_c, "hough x lines")
117
118     image_g = image.copy()
119     draw = ImageDraw.Draw(image_g)
120     for line in [l for s in lines for l in s]:
121         draw.line(line_from_angl_dist(line, image.size), fill=(120, 255, 120))
122     show_image(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)]