reorganised TODO
[imago.git] / imago_pack / hough.py
1 """Hough transform module."""
2
3 from math import pi
4
5 from PIL import Image
6
7 import pcf
8
9 class Hough:
10     """Hough transform.
11
12     This class stores the transformed image and metainformation.
13     """
14     def __init__(self, size, dt, init_angle, image):
15         self.size = size # this is a tuple (width, height)
16         self.dt = dt     # this is the angle step in hough transform
17         self.initial_angle = init_angle
18         self.image = image
19
20     @classmethod
21     def Transform(cls, image):
22         """Create Hough transform of the *image* with default parameters."""
23         # TODO rename to transform?
24         size = image.size
25         dt = pi / size[1]
26         initial_angle = (pi / 4) + (dt / 2)
27         image_s = pcf.hough(size, image.tostring(), initial_angle, dt)
28         image_t = Image.fromstring('L', size, image_s)
29         return cls(size, dt, initial_angle, image_t)
30
31     def apply_filter(self, filter_f):
32         return Hough(self.size, self.dt, self.initial_angle,
33                      filter_f(self.image))
34
35     def lines_from_list(self, p_list):
36         """Take a list of transformed points and return a list of corresponding
37         lines as (angle, distance) tuples."""
38         lines = []
39         for p in p_list:
40             lines.append(self.angle_distance(p))
41         return lines
42
43     def all_lines_h(self):
44         # TODO what is this?
45         im_l = self.image.load()
46         lines1 = []
47         for x in xrange(self.size[0] / 2):
48             for y in xrange(self.size[1]):
49                 if im_l[x, y]:
50                     lines1.append(self.angle_distance((x, y)))
51         lines2 = []
52         for x in xrange(self.size[0] / 2, self.size[0]):
53             for y in xrange(self.size[1]):
54                 if im_l[x, y]:
55                     lines2.append(self.angle_distance((x, y)))
56         return [lines1, lines2]
57
58     def all_lines(self):
59         # TODO what is this? how does it differ from the upper one?
60         im_l = self.image.load()
61         lines = []
62         for x in xrange(self.size[0]):
63             for y in xrange(self.size[1]):
64                 if im_l[x, y]:
65                     lines.append(self.angle_distance((x, y)))
66         return lines
67     
68     def angle_distance(self, point):
69         """Take a point from the transformed image and return the corresponding
70         line in the original as (angle, distance) tuple."""
71         return (self.dt * point[1] + self.initial_angle,
72                 point[0] - self.size[0] / 2)
73