readme file, help
[imago.git] / hough.py
1 from PIL import Image
2 from math import sin, cos, pi
3 from commons import clear
4
5 class Hough:
6     def __init__(self, size):
7         self.size = size
8         self.dt = pi / size[1]
9         self.initial_angle = (pi / 4) + (self.dt / 2)
10
11     def transform(self, image):
12         image_l = image.load()
13         size = image.size
14         
15         matrix = [[0]*size[1] for _ in xrange(size[0])]
16
17         dt = self.dt
18         initial_angle = self.initial_angle
19
20         for x in xrange(size[0]):
21             clear()
22             print "hough transform: {0:>3}/{1}".format(x + 1, size[0])
23             for y in xrange(size[1]):
24                 if image_l[x, y]:
25                     # for every angle:
26                     for a in xrange(size[1]):
27                         # find the distance:
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)) +
32                                     size[0] / 2)
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
36
37         new_image = Image.new('L', size)
38         new_image_l = new_image.load()
39
40         minimum = min([min(m) for m in matrix])
41
42         maximum = max([max(m) for m in matrix]) - minimum
43
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
47             
48         return new_image
49
50     def all_lines(self, image):
51         im_l = image.load()
52         lines = []
53         for x in xrange(image.size[0]):
54             for y in xrange(image.size[1]):
55                 if im_l[x, y]:
56                     lines.append(self.angle_distance((x, y)))
57         return lines
58     
59     def find_angle_distance(self, image):
60         image_l = image.load()
61
62         points = []
63
64         count = 0
65         point_x = 0
66         point_y = 0
67         for x in xrange(image.size[0] / 2):
68             for y in xrange(image.size[1] / 2, image.size[1]):
69                 if image_l[x, y]:
70                     count += 1
71                     point_x += x
72                     point_y += y
73         points.append((float(point_x) / count, float(point_y) / count))
74
75         count = 0
76         point_x = 0
77         point_y = 0
78         for x in xrange(image.size[0] / 2, image.size[0]):
79             for y in xrange(image.size[1] / 2, image.size[1]):
80                 if image_l[x, y]:
81                     count += 1
82                     point_x += x
83                     point_y += y
84         points.append((float(point_x) / count, float(point_y) / count))
85
86         return [self.angle_distance(p) for p in points]
87
88     def angle_distance(self, point):
89         return (self.dt * point[1] + self.initial_angle, point[0] - self.size[0] / 2)
90