3 print "OpenCV Python version of drawing"
5 # import the necessary things for OpenCV
7 from opencv import highgui
9 # for making random numbers
10 from random import Random
12 def random_color (random):
16 icolor = random.randint (0, 0xFFFFFF)
17 return cv.cvScalar (icolor & 0xff, (icolor >> 8) & 0xff, (icolor >> 16) & 0xff)
19 if __name__ == '__main__':
24 window_name = "Drawing Demo"
27 line_type = cv.CV_AA # change it to 8 to see non-antialiased graphics
29 # create the source image
30 image = cv.cvCreateImage (cv.cvSize (width, height), 8, 3)
32 # create window and display the original picture in it
33 highgui.cvNamedWindow (window_name, 1)
35 highgui.cvShowImage (window_name, image)
37 # create the random number
41 for i in range (number):
42 pt1 = cv.cvPoint (random.randrange (-width, 2 * width),
43 random.randrange (-height, 2 * height))
44 pt2 = cv.cvPoint (random.randrange (-width, 2 * width),
45 random.randrange (-height, 2 * height))
46 cv.cvLine (image, pt1, pt2,
47 random_color (random),
48 random.randrange (0, 10),
51 highgui.cvShowImage (window_name, image)
52 highgui.cvWaitKey (delay)
54 # draw some rectangles
55 for i in range (number):
56 pt1 = cv.cvPoint (random.randrange (-width, 2 * width),
57 random.randrange (-height, 2 * height))
58 pt2 = cv.cvPoint (random.randrange (-width, 2 * width),
59 random.randrange (-height, 2 * height))
60 cv.cvRectangle (image, pt1, pt2,
61 random_color (random),
62 random.randrange (-1, 9),
65 highgui.cvShowImage (window_name, image)
66 highgui.cvWaitKey (delay)
69 for i in range (number):
70 pt1 = cv.cvPoint (random.randrange (-width, 2 * width),
71 random.randrange (-height, 2 * height))
72 sz = cv.cvSize (random.randrange (0, 200),
73 random.randrange (0, 200))
74 angle = random.randrange (0, 1000) * 0.180
75 cv.cvEllipse (image, pt1, sz, angle, angle - 100, angle + 200,
76 random_color (random),
77 random.randrange (-1, 9),
80 highgui.cvShowImage (window_name, image)
81 highgui.cvWaitKey (delay)
83 # init the list of polylines
86 pt = [0,] * nb_polylines
87 for a in range (nb_polylines):
88 pt [a] = [0,] * polylines_size
91 for i in range (number):
92 for a in range (nb_polylines):
93 for b in range (polylines_size):
94 pt [a][b] = cv.cvPoint (random.randrange (-width, 2 * width),
95 random.randrange (-height, 2 * height))
96 cv.cvPolyLine (image, pt, 1,
97 random_color (random),
98 random.randrange (1, 9),
101 highgui.cvShowImage (window_name, image)
102 highgui.cvWaitKey (delay)
104 # draw some filled polylines
105 for i in range (number):
106 for a in range (nb_polylines):
107 for b in range (polylines_size):
108 pt [a][b] = cv.cvPoint (random.randrange (-width, 2 * width),
109 random.randrange (-height, 2 * height))
110 cv.cvFillPoly (image, pt,
111 random_color (random),
114 highgui.cvShowImage (window_name, image)
115 highgui.cvWaitKey (delay)
118 for i in range (number):
119 pt1 = cv.cvPoint (random.randrange (-width, 2 * width),
120 random.randrange (-height, 2 * height))
121 cv.cvCircle (image, pt1, random.randrange (0, 300),
122 random_color (random),
123 random.randrange (-1, 9),
126 highgui.cvShowImage (window_name, image)
127 highgui.cvWaitKey (delay)
130 for i in range (number):
131 pt1 = cv.cvPoint (random.randrange (-width, 2 * width),
132 random.randrange (-height, 2 * height))
133 font = cv.cvInitFont (random.randrange (0, 8),
134 random.randrange (0, 100) * 0.05 + 0.01,
135 random.randrange (0, 100) * 0.05 + 0.01,
136 random.randrange (0, 5) * 0.1,
137 random.randrange (0, 10),
140 cv.cvPutText (image, "Testing text rendering!",
142 random_color (random))
144 highgui.cvShowImage (window_name, image)
145 highgui.cvWaitKey (delay)
147 # prepare a text, and get it's properties
148 font = cv.cvInitFont (cv.CV_FONT_HERSHEY_COMPLEX,
149 3, 3, 0.0, 5, line_type)
150 text_size, ymin = cv.cvGetTextSize ("OpenCV forever!", font)
151 pt1.x = (width - text_size.width) / 2
152 pt1.y = (height + text_size.height) / 2
153 image2 = cv.cvCloneImage(image)
155 # now, draw some OpenCV pub ;-)
156 for i in range (255):
157 cv.cvSubS (image2, cv.cvScalarAll (i), image, None)
158 cv.cvPutText (image, "OpenCV forever!",
159 pt1, font, cv.cvScalar (255, i, i))
160 highgui.cvShowImage (window_name, image)
161 highgui.cvWaitKey (delay)
163 # wait some key to end
164 highgui.cvWaitKey (0)