3 """Go image recognition"""
11 import Image, ImageDraw
12 except ImportError, msg:
13 print >>sys.stderr, msg
18 from hough import Hough
24 """Main function of the program."""
26 parser = argparse.ArgumentParser(description=__doc__)
27 parser.add_argument('file', metavar='file', nargs=1,
28 help="image to anlyse")
29 parser.add_argument('-w', type=int, default=640,
30 help="scales image to the specified width before analysis")
31 parser.add_argument('-d', '--debug', dest='show_all', action='store_true',
32 help="show every step of the computation")
33 parser.add_argument('-s', '--save', dest='do_something', action='store_const',
34 const=image_save, default=im_debug.show,
35 help="save images instead of displaying them")
36 args = parser.parse_args()
38 show_all = args.show_all
39 do_something = args.do_something
42 image = Image.open(args.file[0])
44 print >>sys.stderr, msg
46 if image.size[0] > args.w:
47 image = image.resize((args.w, int((float(args.w)/image.size[0]) *
48 image.size[1])), Image.ANTIALIAS)
50 Saving_dir = "saved/" + args.file[0][:-4] + "_" + str(image.size[0]) + "/"
53 do_something(image, "original image")
55 im_l = image.convert('L')
57 do_something(im_l, "ITU-R 601-2 luma transform")
59 im_edges = filters.edge_detection(im_l)
61 do_something(im_edges, "edge detection")
63 im_h = filters.high_pass(im_edges, 100)
65 do_something(im_h, "high pass filters")
67 hough1 = Hough(im_h.size)
68 im_hough = hough1.transform(im_h)
70 do_something(im_hough, "hough transform")
72 im_hough = filters.peaks(im_hough)
74 do_something(im_hough, "peak extraction")
76 im_h2 = filters.high_pass(im_hough, 120)
78 do_something(im_h2, "second high pass filters")
80 im_h2 = filters.components(im_h2)
82 do_something(im_h2, "components centers")
84 hough2 = Hough(im_h2.size)
85 im_hough2 = hough2.transform(im_h2)
87 do_something(im_hough2, "second hough transform")
89 im_h3 = filters.high_pass(im_hough2, 120)
91 do_something(im_h3, "third high pass filters")
93 lines = hough2.find_angle_distance(im_h3)
95 im_lines = Image.new('L', im_h2.size)
97 draw = ImageDraw.Draw(im_lines)
100 draw.line(line_from_angl_dist(line, im_h2.size), fill=255, width=5)
102 do_something(im_lines, "lines")
104 im_c = combine(im_h2, im_lines)
106 do_something(im_c, "first hough x lines")
110 do_something(im_c, "optimalised hough")
112 lines = hough1.all_lines(im_c)
113 draw = ImageDraw.Draw(image)
115 draw.line(line_from_angl_dist(line, image.size), fill=(120, 255, 120))
117 do_something(image, "the grid")
121 def image_save(image, title=''):
124 filename = Saving_dir + "{0:0>2}".format(Saving_num) + '.jpg'
125 if not os.path.isdir(Saving_dir):
126 os.makedirs(Saving_dir)
127 image.save(filename, 'JPEG')
134 for y in xrange(image.size[1]):
135 for x in xrange(image.size[0]):
136 if im_l[x, y] and last:
144 def combine(image1, image2):
145 im_l1 = image1.load()
146 im_l2 = image2.load()
148 im_n = Image.new('L', image1.size)
151 for x in xrange(image1.size[0]):
152 for y in xrange(image1.size[1]):
153 if im_l1[x, y] and im_l2[x, y]:
157 def line_from_angl_dist((angle, distance), size):
159 y1 = int(round((x1 * math.sin(angle) - distance)/math.cos(angle))) + size[1] / 2
161 y2 = int(round((x2 * math.sin(angle) - distance)/math.cos(angle))) + size[1] / 2
162 return [(0, y1), (size[0] - 1, y2)]
164 if __name__ == '__main__':