1 """Lines finding module."""
3 from functools import partial
5 from math import sin, cos, pi
8 import Image, ImageDraw
9 except ImportError, msg:
10 print >> sys.stderr, msg
14 from hough import Hough
16 def prepare(image, show_all, do_something, verbose):
18 im_l = image.convert('L')
20 do_something(im_l, "ITU-R 601-2 luma transform")
23 print >> sys.stderr, "edge detection"
24 im_edges = filters.edge_detection(im_l)
26 do_something(im_edges, "edge detection")
28 im_h = filters.high_pass(im_edges, 100)
30 do_something(im_h, "high pass filters")
34 def transform(image, hough, show_all, do_something):
35 im_hough = hough.transform(image)
37 do_something(im_hough, "hough transform")
39 # im_hough.image = filters.peaks(im_hough.image)
41 # do_something(im_hough.image, "peak extraction")
43 im_h2 = filters.high_pass(im_hough, 96)
45 do_something(im_h2, "second high pass filters")
47 im_h2 = filters.components2(im_h2)
49 do_something(im_h2, "components centers")
53 def find_lines(image, show_all, do_something, verbose):
54 """Find lines in the *image*."""
55 # TODO refactor into smaller functions
58 print >> sys.stderr, "preprocessing"
61 do_something(image, "original image")
63 im_h = prepare(image, show_all, do_something, verbose)
65 hough = Hough.default(im_h)
68 print >> sys.stderr, "hough transform"
70 im_h2 = transform(im_h, hough, show_all, do_something)
73 print >> sys.stderr, "second hough transform"
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)
81 do_something(im_hough2, "second hough transform")
83 im_h3 = filters.high_pass(im_hough2, 120)
85 do_something(im_h3, "third high pass filter")
87 im_h3 = filters.components(im_h3)
89 do_something(im_h3, "half centers")
92 print >> sys.stderr, "finding the grid"
94 lines_m = hough2.all_lines_h(im_h3)
96 im_c = im_h2.convert('RGB').convert('RGB', (1, 0.5, 0.5, 0))
97 draw_c = ImageDraw.Draw(im_c)
100 for line_l in lines_m:
101 im_line = Image.new('L', im_h2.size)
102 draw = ImageDraw.Draw(im_line)
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):
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)
115 bounds += [line_points[0], line_points[-1]]
118 do_something(im_c, "hough x lines")
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))
125 do_something(image_g, "lines")
127 return lines, lines_m[0][0], lines_m[1][0], bounds, hough
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()
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))
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:
147 x1 = int(round((y1 * cos(angle) + distance) / sin(angle))) + size[0] / 2
149 x2 = int(round((y2 * cos(angle) + distance) / sin(angle))) + size[0] / 2
150 return [(x1, 0), (x2, size[1])]
153 y1 = int(round((x1 * sin(angle) - distance) / cos(angle))) + size[1] / 2
155 y2 = int(round((x2 * sin(angle) - distance) / cos(angle))) + size[1] / 2
156 return [(0, y1), (size[0], y2)]