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_h2 = filters.high_pass(im_hough, 120)
74 do_something(im_h2, "second high pass filters")
76 hough2 = Hough(im_h2.size)
77 im_hough2 = hough2.transform(im_h2)
79 do_something(im_hough2, "second hough transform")
81 im_h3 = filters.high_pass(im_hough2, 120)
83 do_something(im_h3, "third high pass filters")
85 lines = hough2.find_angle_distance(im_h3)
87 im_lines = Image.new('L', im_h2.size)
89 draw = ImageDraw.Draw(im_lines)
92 draw.line(line_from_angl_dist(line, im_h2.size), fill=255)
94 do_something(im_lines, "lines")
96 im_c = combine(im_h2, im_lines)
98 do_something(im_c, "first hough x lines")
102 do_something(im_c, "optimalised hough")
104 lines = hough1.all_lines(im_c)
105 draw = ImageDraw.Draw(image)
107 draw.line(line_from_angl_dist(line, image.size), fill=(120, 255, 120))
109 do_something(image, "the grid")
113 def image_save(image, title=''):
116 filename = Saving_dir + "{0:0>2}".format(Saving_num) + '.jpg'
117 if not os.path.isdir(Saving_dir):
118 os.makedirs(Saving_dir)
119 image.save(filename, 'JPEG')
126 for y in xrange(image.size[1]):
127 for x in xrange(image.size[0]):
128 if im_l[x, y] and last:
136 def combine(image1, image2):
137 im_l1 = image1.load()
138 im_l2 = image2.load()
140 im_n = Image.new('L', image1.size)
143 for x in xrange(image1.size[0]):
144 for y in xrange(image1.size[1]):
145 if im_l1[x, y] and im_l2[x, y]:
149 def line_from_angl_dist((angle, distance), size):
151 y1 = int(round((x1 * math.sin(angle) - distance)/math.cos(angle))) + size[1] / 2
153 y2 = int(round((x2 * math.sin(angle) - distance)/math.cos(angle))) + size[1] / 2
154 return [(0, y1), (size[0] - 1, y2)]
156 if __name__ == '__main__':