5f41e1616ba5016cfd4152c931b065704a2740a1
[opencv] / samples / swig_python / contours.py
1 #! /usr/bin/env python
2
3 print "OpenCV Python version of contours"
4
5 # import the necessary things for OpenCV
6 from opencv import cv
7 from opencv import highgui
8
9 # some default constants
10 _SIZE = 500
11 _DEFAULT_LEVEL = 3
12
13 # definition of some colors
14 _red = cv.cvScalar (0, 0, 255, 0);
15 _green = cv.cvScalar (0, 255, 0, 0);
16 _white = cv.cvRealScalar (255)
17 _black = cv.cvRealScalar (0)
18
19 # the callback on the trackbar, to set the level of contours we want
20 # to display
21 def on_trackbar (position):
22
23     # create the image for putting in it the founded contours
24     contours_image = cv.cvCreateImage (cv.cvSize (_SIZE, _SIZE), 8, 3)
25
26     # compute the real level of display, given the current position
27     levels = position - 3
28
29     # initialisation
30     _contours = contours
31     
32     if levels <= 0:
33         # zero or negative value
34         # => get to the nearest face to make it look more funny
35         _contours = contours.h_next.h_next.h_next
36         
37     # first, clear the image where we will draw contours
38     cv.cvSetZero (contours_image)
39     
40     # draw contours in red and green
41     cv.cvDrawContours (contours_image, _contours,
42                        _red, _green,
43                        levels, 3, cv.CV_AA,
44                        cv.cvPoint (0, 0))
45
46     # finally, show the image
47     highgui.cvShowImage ("contours", contours_image)
48
49 if __name__ == '__main__':
50
51     # create the image where we want to display results
52     image = cv.cvCreateImage (cv.cvSize (_SIZE, _SIZE), 8, 1)
53
54     # start with an empty image
55     cv.cvSetZero (image)
56
57     # draw the original picture
58     for i in range (6):
59         dx = (i % 2) * 250 - 30
60         dy = (i / 2) * 150
61         
62         cv.cvEllipse (image,
63                       cv.cvPoint (dx + 150, dy + 100),
64                       cv.cvSize (100, 70),
65                       0, 0, 360, _white, -1, 8, 0)
66         cv.cvEllipse (image,
67                       cv.cvPoint (dx + 115, dy + 70),
68                       cv.cvSize (30, 20),
69                       0, 0, 360, _black, -1, 8, 0)
70         cv.cvEllipse (image,
71                       cv.cvPoint (dx + 185, dy + 70),
72                       cv.cvSize (30, 20),
73                       0, 0, 360, _black, -1, 8, 0)
74         cv.cvEllipse (image,
75                       cv.cvPoint (dx + 115, dy + 70),
76                       cv.cvSize (15, 15),
77                       0, 0, 360, _white, -1, 8, 0)
78         cv.cvEllipse (image,
79                       cv.cvPoint (dx + 185, dy + 70),
80                       cv.cvSize (15, 15),
81                       0, 0, 360, _white, -1, 8, 0)
82         cv.cvEllipse (image,
83                       cv.cvPoint (dx + 115, dy + 70),
84                       cv.cvSize (5, 5),
85                       0, 0, 360, _black, -1, 8, 0)
86         cv.cvEllipse (image,
87                       cv.cvPoint (dx + 185, dy + 70),
88                       cv.cvSize (5, 5),
89                       0, 0, 360, _black, -1, 8, 0)
90         cv.cvEllipse (image,
91                       cv.cvPoint (dx + 150, dy + 100),
92                       cv.cvSize (10, 5),
93                       0, 0, 360, _black, -1, 8, 0)
94         cv.cvEllipse (image,
95                       cv.cvPoint (dx + 150, dy + 150),
96                       cv.cvSize (40, 10),
97                       0, 0, 360, _black, -1, 8, 0)
98         cv.cvEllipse (image,
99                       cv.cvPoint (dx + 27, dy + 100),
100                       cv.cvSize (20, 35),
101                       0, 0, 360, _white, -1, 8, 0)
102         cv.cvEllipse (image,
103                       cv.cvPoint (dx + 273, dy + 100),
104                       cv.cvSize (20, 35),
105                       0, 0, 360, _white, -1, 8, 0)
106
107     # create window and display the original picture in it
108     highgui.cvNamedWindow ("image", 1)
109     highgui.cvShowImage ("image", image)
110
111     # create the storage area
112     storage = cv.cvCreateMemStorage (0)
113     
114     # find the contours
115     nb_contours, contours = cv.cvFindContours (image,
116                                                storage,
117                                                cv.sizeof_CvContour,
118                                                cv.CV_RETR_TREE,
119                                                cv.CV_CHAIN_APPROX_SIMPLE,
120                                                cv.cvPoint (0,0))
121
122     # comment this out if you do not want approximation
123     contours = cv.cvApproxPoly (contours, cv.sizeof_CvContour,
124                                 storage,
125                                 cv.CV_POLY_APPROX_DP, 3, 1)
126     
127     # create the window for the contours
128     highgui.cvNamedWindow ("contours", 1)
129
130     # create the trackbar, to enable the change of the displayed level
131     highgui.cvCreateTrackbar ("levels+3", "contours", 3, 7, on_trackbar)
132
133     # call one time the callback, so we will have the 1st display done
134     on_trackbar (_DEFAULT_LEVEL)
135
136     # wait a key pressed to end
137     highgui.cvWaitKey (0)