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_image, verbose):
18 im_l = image.convert('L')
19 show_image(im_l, "ITU-R 601-2 luma transform")
22 print >> sys.stderr, "edge detection"
23 im_edges = filters.edge_detection(im_l)
24 show_image(im_edges, "edge detection")
26 im_h = filters.high_pass(im_edges, 100)
27 show_image(im_h, "high pass filters")
31 def transform(image, hough, show_image):
33 im_hough = hough.transform(image)
34 show_image(im_hough, "hough transform")
36 # im_hough.image = filters.peaks(im_hough.image)
37 # show_image(im_hough.image, "peak extraction")
39 im_h2 = filters.high_pass(im_hough, 96)
40 show_image(im_h2, "second high pass filters")
42 im_h2 = filters.components2(im_h2)
43 show_image(im_h2, "components centers")
47 def find_lines(image, show_image, verbose):
48 """Find lines in the *image*."""
49 # TODO refactor into smaller functions
52 print >> sys.stderr, "preprocessing"
54 show_image(image, "original image")
56 im_h = prepare(image, show_image, verbose)
58 hough = Hough.default(im_h)
61 print >> sys.stderr, "hough transform"
63 im_h2 = transform(im_h, hough, show_image)
66 print >> sys.stderr, "second hough transform"
68 # im_hough might be used instead im_h2, but at the moment it brings a lot of
69 # noise to the second transform, which later confuses the center-finding
70 # mechanism (which is not very robust yet)
71 hough2 = Hough.default(im_h2)
72 im_hough2 = hough2.transform(im_h2)
73 show_image(im_hough2, "second hough transform")
75 im_h3 = filters.high_pass(im_hough2, 120)
76 show_image(im_h3, "third high pass filter")
78 im_h3 = filters.components(im_h3)
79 show_image(im_h3, "half centers")
82 print >> sys.stderr, "finding the grid"
84 lines_m = hough2.all_lines_h(im_h3)
86 im_c = im_h2.convert('RGB').convert('RGB', (1, 0.5, 0.5, 0))
87 draw_c = ImageDraw.Draw(im_c)
90 for line_l in lines_m:
91 im_line = Image.new('L', im_h2.size)
92 draw = ImageDraw.Draw(im_line)
95 draw.line(line_from_angl_dist(line, im_h2.size), fill=255, width=7)
96 draw_c.line(line_from_angl_dist(line, im_c.size),
97 fill=(70, 70, 70), width=7)
98 for p in combine(im_h2, im_line):
100 for point in line_points:
101 draw_c.point(point, fill=(120, 255, 120))
102 lines.append(hough.lines_from_list(line_points))
103 line_points = list(line_points)
105 bounds += [line_points[0], line_points[-1]]
107 show_image(im_c, "hough x lines")
109 image_g = image.copy()
110 draw = ImageDraw.Draw(image_g)
111 for line in [l for s in lines for l in s]:
112 draw.line(line_from_angl_dist(line, image.size), fill=(120, 255, 120))
113 show_image(image_g, "lines")
115 return lines, lines_m[0][0], lines_m[1][0], bounds, hough
117 def combine(image1, image2):
118 """Return a list of points that are present in both images."""
119 im_l1 = image1.load()
120 im_l2 = image2.load()
124 for x in xrange(image1.size[0]):
125 for y in xrange(image1.size[1]):
126 if im_l1[x, y] and im_l2[x, y]:
127 on_both.append((x, y))
130 def line_from_angl_dist((angle, distance), size):
131 """Take *angle* and *distance* (from the center of the image) of a line and
132 size of the image. Return the line represented by two points."""
133 if pi / 4 < angle < 3 * pi / 4:
135 x1 = int(round((y1 * cos(angle) + distance) / sin(angle))) + size[0] / 2
137 x2 = int(round((y2 * cos(angle) + distance) / sin(angle))) + size[0] / 2
138 return [(x1, 0), (x2, size[1])]
141 y1 = int(round((x1 * sin(angle) - distance) / cos(angle))) + size[1] / 2
143 y2 = int(round((x2 * sin(angle) - distance) / cos(angle))) + size[1] / 2
144 return [(0, y1), (size[0], y2)]