1 """Go image recognition lines-finding module"""
4 from math import sin, cos, pi
7 import Image, ImageDraw
8 except ImportError, msg:
9 print >> sys.stderr, msg
13 from hough import Hough
15 def find_lines(image, show_all, do_something, verbose):
18 print >> sys.stderr, "preprocessing"
21 do_something(image, "original image")
23 im_l = image.convert('L')
25 do_something(im_l, "ITU-R 601-2 luma transform")
28 print >> sys.stderr, "edge detection"
30 im_edges = filters.edge_detection(im_l)
32 do_something(im_edges, "edge detection")
34 im_h = filters.high_pass(im_edges, 100)
36 do_something(im_h, "high pass filters")
39 print >> sys.stderr, "hough transform"
41 hough1 = Hough(im_h.size)
42 im_hough = hough1.transform(im_h)
44 do_something(im_hough, "hough transform")
46 im_hough = filters.peaks(im_hough)
48 do_something(im_hough, "peak extraction")
50 im_h2 = filters.high_pass(im_hough, 120)
52 do_something(im_h2, "second high pass filters")
54 im_h2 = filters.components2(im_h2)
56 do_something(im_h2, "components centers")
59 print >> sys.stderr, "second hough transform"
61 hough2 = Hough(im_h2.size)
62 # im_hough might be used instead im_h2, but at the moment it brings a lot of
63 # noise to the second transform, which later confuses the center-finding
64 # mechanism (which is not very robust yet)
65 im_hough2 = hough2.transform(im_h2)
67 do_something(im_hough2, "second hough transform")
69 im_h3 = filters.high_pass(im_hough2, 120)
71 do_something(im_h3, "third high pass filter")
73 im_h3 = filters.components(im_h3)
75 do_something(im_h3, "half centers")
78 print >> sys.stderr, "finding the grid"
80 lines_m = hough2.all_lines_h(im_h3)
82 im_c = im_h2.convert('RGB').convert('RGB', (1, 0.5, 0.5, 0))
83 draw_c = ImageDraw.Draw(im_c)
86 for line_l in lines_m:
87 im_line = Image.new('L', im_h2.size)
88 draw = ImageDraw.Draw(im_line)
91 draw.line(line_from_angl_dist(line, im_h2.size), fill=255, width=7)
92 draw_c.line(line_from_angl_dist(line, im_c.size), fill=(70, 70, 70), width=7)
93 for p in combine(im_h2, im_line):
95 for point in line_points:
96 draw_c.point(point, fill=(120, 255, 120))
97 lines.append(hough1.lines_from_list(line_points))
98 line_points = list(line_points)
100 bounds += [line_points[0], line_points[-1]]
103 do_something(im_c, "hough x lines")
105 image_g = image.copy()
106 draw = ImageDraw.Draw(image_g)
107 for line in [l for s in lines for l in s]:
108 draw.line(line_from_angl_dist(line, image.size), fill=(120, 255, 120))
110 do_something(image_g, "lines")
112 return lines, lines_m[0][0], lines_m[1][0], bounds, hough1, im_h
114 def combine(image1, image2):
115 im_l1 = image1.load()
116 im_l2 = image2.load()
120 for x in xrange(image1.size[0]):
121 for y in xrange(image1.size[1]):
122 if im_l1[x, y] and im_l2[x, y]:
123 on_both.append((x, y))
126 def line_from_angl_dist((angle, distance), size):
127 if pi / 4 < angle < 3 * pi / 4:
129 x1 = int(round((y1 * cos(angle) + distance) / sin(angle))) + size[0] / 2
131 x2 = int(round((y2 * cos(angle) + distance) / sin(angle))) + size[0] / 2
132 return [(x1, 0), (x2, size[1])]
135 y1 = int(round((x1 * sin(angle) - distance) / cos(angle))) + size[1] / 2
137 y2 = int(round((x2 * sin(angle) - distance) / cos(angle))) + size[1] / 2
138 return [(0, y1), (size[0], y2)]