6ab51a188b5fbe53357d9de3adf969befc957aea
[imago.git] / imago.py
1 #!/usr/bin/env python
2 """Usage: imago.py file"""
3
4 import sys
5 import math
6 import Image, ImageDraw
7 import im_debug
8 import filter
9 from hough import Hough
10
11 class UsageError(Exception):
12     def __init__(self, msg):
13         self.msg = msg
14
15 def main(*argv):
16     """Main function of the program."""
17     
18     show_all = False
19
20     try:
21         if argv is ():
22             argv = sys.argv[1:]
23             if argv == []:
24                 raise UsageError('Missing filename')
25         if "--help" in argv:
26             print __doc__
27             return 0
28         if "--debug" in argv:
29             show_all = True
30     except UsageError, err:
31         print >>sys.stderr, err.msg, "(\"imago.py --help\" for help)"
32         return 2
33
34     try:
35         image = Image.open(argv[0])
36     except IOError, msg:
37         print >>sys.stderr, msg
38         return 1
39     if show_all:
40        im_debug.show(image, "original image")
41
42     im_l = image.convert('L')
43     if show_all:
44         im_debug.show(im_l, "ITU-R 601-2 luma transform")
45
46     im_edges = filter.edge_detection(im_l)
47     if show_all:    
48         im_debug.show(im_edges, "edge detection")
49
50     im_h = filter.high_pass(im_edges, 100)
51     if show_all:
52         im_debug.show(im_h, "high pass filter")
53     
54     hough1 = Hough(im_h.size)
55     im_hough = hough1.transform(im_h)
56     if show_all:
57         im_debug.show(im_hough, "hough transform")
58
59     im_h2 = filter.high_pass(im_hough, 120)
60     if show_all:
61         im_debug.show(im_h2, "second high pass filter")
62
63     hough2 = Hough(im_h2.size)
64     im_hough2 = hough2.transform(im_h2)
65     if show_all:
66         im_debug.show(im_hough2, "second hough transform")
67
68     im_h3 = filter.high_pass(im_hough2, 120)
69     if show_all:
70         im_debug.show(im_h3, "third high pass filter")
71      
72     lines = hough2.find_angle_distance(im_h3)
73
74     im_lines = Image.new('L', im_h2.size)
75
76     draw = ImageDraw.Draw(im_lines)
77
78     for line in lines:
79         draw.line(line_from_angl_dist(line, im_h2.size), fill=255)
80     if show_all:
81         im_debug.show(im_lines, "lines")
82
83     im_c = combine(im_h2, im_lines)
84     if show_all:
85         im_debug.show(im_c, "first hough x lines")
86
87     collapse(im_c)
88     if show_all:
89         im_debug.show(im_c, "optimalised hough")
90
91     lines = hough1.all_lines(im_c)
92     draw = ImageDraw.Draw(image)
93     for line in lines:
94         draw.line(line_from_angl_dist(line, image.size), fill=(120, 255, 120))
95
96     im_debug.show(image, "the grid")
97
98     return 0
99
100 def collapse(image):
101     #HACK
102     im_l = image.load()
103     last = False
104     for y in xrange(image.size[1]):
105         for x in xrange(image.size[0]):
106             if im_l[x,y] and last:
107                 im_l[x, y] = 0
108                 last = False
109             elif im_l[x, y]:
110                 last = True
111             elif last:
112                 last = False
113
114 def combine(image1, image2):
115     im_l1 = image1.load()
116     im_l2 = image2.load()
117
118     im_n = Image.new('L', image1.size)
119     im_nl = im_n.load()
120
121     for x in xrange(image1.size[0]):
122         for y in xrange(image1.size[1]):
123             if im_l1[x, y] and im_l2[x, y]:
124                 im_nl[x, y] = 255
125     return im_n
126
127 def line_from_angl_dist((angle, distance), size):
128     x1 = - size[0] / 2
129     y1 = int(round((x1 * math.sin(angle) - distance)/math.cos(angle))) + size[1] / 2
130     x2 = size[0] / 2 
131     y2 = int(round((x2 * math.sin(angle) - distance)/math.cos(angle))) + size[1] / 2
132     return [(0, y1), (size[0] - 1, y2)]
133
134 if __name__ == '__main__':
135     sys.exit(main())