threaded error surface plotting
[imago.git] / linef.py
1 #!/usr/bin/env python
2
3 """Go image recognition lines-finding module"""
4
5 import sys
6 from math import sin, cos, pi
7
8 try:
9     import Image, ImageDraw
10 except ImportError, msg:
11     print >> sys.stderr, msg
12     sys.exit(1)
13
14 import filters
15 from hough import Hough
16
17 def find_lines(image, show_all, do_something, verbose):
18
19     if verbose:
20         print >> sys.stderr, "preprocessing"
21
22     if show_all:
23         do_something(image, "original image")
24
25     im_l = image.convert('L')
26     if show_all:
27         do_something(im_l, "ITU-R 601-2 luma transform")
28
29     if verbose:
30         print >> sys.stderr, "edge detection"
31
32     im_edges = filters.edge_detection(im_l)
33     if show_all:    
34         do_something(im_edges, "edge detection")
35
36     im_h = filters.high_pass(im_edges, 100)
37     if show_all:
38         do_something(im_h, "high pass filters")
39     
40     if verbose:
41         print >> sys.stderr, "hough transform"
42
43     hough1 = Hough(im_h.size)
44     im_hough = hough1.transform(im_h)
45     if show_all:
46         do_something(im_hough, "hough transform")
47
48     im_hough = filters.peaks(im_hough)
49     if show_all:
50         do_something(im_hough, "peak extraction")
51                
52     im_h2 = filters.high_pass(im_hough, 120)
53     if show_all:
54         do_something(im_h2, "second high pass filters")
55
56     im_h2 = filters.components2(im_h2)
57     if show_all:
58         do_something(im_h2, "components centers")
59
60     if verbose:
61         print >> sys.stderr, "second hough transform"
62
63     hough2 = Hough(im_h2.size) 
64     # im_hough might be used instead im_h2, but at the moment it brings a lot of
65     # noise to the second transform, which later confuses the center-finding
66     # mechanism (which is not very robust yet)
67     im_hough2 = hough2.transform(im_h2)
68     if show_all:
69         do_something(im_hough2, "second hough transform")
70
71     im_h3 = filters.high_pass(im_hough2, 120)
72     if show_all:
73         do_something(im_h3, "third high pass filter")
74      
75     im_h3 = filters.components(im_h3)
76     if show_all:
77         do_something(im_h3, "half centers")
78
79     if verbose:
80         print >> sys.stderr, "finding the grid"
81
82     lines_m = hough2.all_lines_h(im_h3)
83     lines = []
84     im_c = im_h2.convert('RGB').convert('RGB', (1, 0.5, 0.5, 0))
85     draw_c = ImageDraw.Draw(im_c)
86     bounds = []
87
88     for line_l in lines_m:
89         im_line = Image.new('L', im_h2.size)
90         draw = ImageDraw.Draw(im_line)
91         line_points = set()
92         for line in line_l:
93             draw.line(line_from_angl_dist(line, im_h2.size), fill=255, width=7)
94             draw_c.line(line_from_angl_dist(line, im_c.size), fill=(70, 70, 70), width=7)
95             for p in combine(im_h2, 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(hough1.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, hough1
115
116 def combine(image1, image2):
117     im_l1 = image1.load()
118     im_l2 = image2.load()
119
120     on_both = []
121
122     for x in xrange(image1.size[0]):
123         for y in xrange(image1.size[1]):
124             if im_l1[x, y] and im_l2[x, y]:
125                 on_both.append((x, y))
126     return on_both
127
128 def line_from_angl_dist((angle, distance), size):
129     if pi / 4 < angle < 3 * pi / 4:
130         y1 = - size[1] / 2
131         x1 = int(round((y1 * cos(angle) + distance) / sin(angle))) + size[0] / 2
132         y2 = size[1] / 2 
133         x2 = int(round((y2 * cos(angle) + distance) / sin(angle))) + size[0] / 2
134         return [(x1, 0), (x2, size[1])]
135     else:
136         x1 = - size[0] / 2
137         y1 = int(round((x1 * sin(angle) - distance) / cos(angle))) + size[1] / 2
138         x2 = size[0] / 2 
139         y2 = int(round((x2 * sin(angle) - distance) / cos(angle))) + size[1] / 2
140         return [(0, y1), (size[0], y2)]