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
17 def prepare(image, show_image, logger):
19 im_l = image.convert('L')
20 show_image(im_l, "ITU-R 601-2 luma transform")
22 logger("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.components(im_h2, 2)
43 show_image(im_h2, "components centers")
47 def run_ransac(image):
50 image_l = image.load()
51 width, height = image.size
55 for y in xrange(0, height):
56 for x in xrange(0, width):
57 if image_l[x, y] > 128:
61 (line, points), (line2, points2) = ransac.ransac_duo(data, dist, 75, 15)
62 line_to_points = lambda (a, b, c), x: (x, (a*x + c) / (- b))
63 # TODO width should not be here vvv
64 # TODO refactor gridf to use standard equations instead of points
65 line = [line_to_points(line, 0), line_to_points(line, width - 1)]
66 line2 = [line_to_points(line2, 0), line_to_points(line2, width - 1)]
67 return [sorted(points), sorted(points2)], line, line2
71 def find_lines(image, show_image, logger):
72 """Find lines in the *image*."""
73 # TODO refactor into smaller functions
75 logger("preprocessing")
76 show_image(image, "original image")
78 im_h = prepare(image, show_image, logger)
80 hough = Hough.default(im_h)
82 logger("hough transform")
84 im_h2 = transform(im_h, hough, show_image)
86 logger("finding the lines")
88 r_lines, l1, l2 = run_ransac(im_h2)
90 lines = map(hough.lines_from_list, r_lines)
92 # TODO refactor gridf to get rid of this:
93 bounds = sum(map(lambda l: [l[0], l[-1]], r_lines), [])
94 # sum(list, []) = flatten list
96 # TODO do this only if show_all is true:
97 image_g = image.copy()
98 draw = ImageDraw.Draw(image_g)
99 for line in [l for s in lines for l in s]:
100 draw.line(line_from_angl_dist(line, image.size), fill=(120, 255, 120))
101 show_image(image_g, "lines")
103 return lines, l1, l2, bounds, hough # TODO
105 def combine(image1, image2):
106 """Return a list of points that are present in both images."""
107 im_l1 = image1.load()
108 im_l2 = image2.load()
112 for x in xrange(image1.size[0]):
113 for y in xrange(image1.size[1]):
114 if im_l1[x, y] and im_l2[x, y]:
115 on_both.append((x, y))
118 def line_from_angl_dist((angle, distance), size):
119 """Take *angle* and *distance* (from the center of the image) of a line and
120 size of the image. Return the line represented by two points."""
121 if pi / 4 < angle < 3 * pi / 4:
123 x1 = int(round((y1 * cos(angle) + distance) / sin(angle))) + size[0] / 2
125 x2 = int(round((y2 * cos(angle) + distance) / sin(angle))) + size[0] / 2
126 return [(x1, 0), (x2, size[1])]
129 y1 = int(round((x1 * sin(angle) - distance) / cos(angle))) + size[1] / 2
131 y2 = int(round((x2 * sin(angle) - distance) / cos(angle))) + size[1] / 2
132 return [(0, y1), (size[0], y2)]