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