3 print "OpenCV Python version of convexhull"
5 # import the necessary things for OpenCV
7 from opencv import highgui
9 # to generate random values
12 # how many points we want at max
15 if __name__ == '__main__':
17 # main object to get random values from
18 my_random = random.Random ()
20 # create the image where we want to display results
21 image = cv.cvCreateImage (cv.cvSize (500, 500), 8, 3)
23 # create the window to put the image in
24 highgui.cvNamedWindow ('hull', highgui.CV_WINDOW_AUTOSIZE)
29 # get a random number of points
30 count = my_random.randrange (0, _MAX_POINTS) + 1
35 for i in range (count):
36 # generate a random point
37 points.append (cv.cvPoint (
38 my_random.randrange (0, image.width / 2) + image.width / 4,
39 my_random.randrange (0, image.width / 2) + image.width / 4
42 # compute the convex hull
43 hull = cv.cvConvexHull2 (points, cv.CV_CLOCKWISE, 0)
45 # start with an empty image
48 for i in range (count):
50 cv.cvCircle (image, points [i], 2,
51 cv.cvScalar (0, 0, 255, 0),
52 cv.CV_FILLED, cv.CV_AA, 0)
54 # start the line from the last point
55 pt0 = points [hull [-1]]
57 for point_index in hull:
58 # connect the previous point to the current one
61 pt1 = points [point_index]
64 cv.cvLine (image, pt0, pt1,
65 cv.cvScalar (0, 255, 0, 0),
68 # now, current one will be the previous one for the next iteration
71 # display the final image
72 highgui.cvShowImage ('hull', image)
74 # handle events, and wait a key pressed
75 k = highgui.cvWaitKey (0)
77 # user has press the ESC key, so exit