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