2 from math import sin, cos, pi
3 from commons import clear
6 def __init__(self, size):
9 self.initial_angle = (pi / 4) + (self.dt / 2)
11 def transform(self, image):
12 image_l = image.load()
15 matrix = [[0]*size[1] for _ in xrange(size[0])]
18 initial_angle = self.initial_angle
20 for x in xrange(size[0]):
22 print "hough transform: {0:>3}/{1}".format(x + 1, size[0])
23 for y in xrange(size[1]):
26 for a in xrange(size[1]):
28 # distance is the dot product of vector (x, y) - centerpoint
29 # and a unit vector orthogonal to the angle
30 distance = (((x - (size[0] / 2)) * sin((dt * a) + initial_angle)) +
31 ((y - (size[1] / 2)) * -cos((dt * a) + initial_angle)) +
33 column = int(round(distance)) # column of the matrix closest to the distance
34 if column >= 0 and column < size[0]:
35 matrix[column][a] += 1
37 new_image = Image.new('L', size)
38 new_image_l = new_image.load()
40 minimum = min([min(m) for m in matrix])
42 maximum = max([max(m) for m in matrix]) - minimum
44 for y in xrange(size[1]):
45 for x in xrange(size[0]):
46 new_image_l[x, y] = (float(matrix[x][y] - minimum) / maximum) * 255
50 def all_lines(self, image):
53 for x in xrange(image.size[0]):
54 for y in xrange(image.size[1]):
56 lines.append(self.angle_distance((x, y)))
59 def find_angle_distance(self, image):
60 image_l = image.load()
67 for x in xrange(image.size[0] / 2):
68 for y in xrange(image.size[1] / 2, image.size[1]):
73 points.append((float(point_x) / count, float(point_y) / count))
78 for x in xrange(image.size[0] / 2, image.size[0]):
79 for y in xrange(image.size[1] / 2, image.size[1]):
84 points.append((float(point_x) / count, float(point_y) / count))
86 return [self.angle_distance(p) for p in points]
88 def angle_distance(self, point):
89 return (self.dt * point[1] + self.initial_angle, point[0] - self.size[0] / 2)