An example program illustrates the use of findContours and drawContours in python
1
2
3'''
4This program illustrates the use of findContours and drawContours.
5The original image is put up along with the image of drawn contours.
6
7Usage:
8 contours.py
9A trackbar is put up which controls the contour level from -3 to 3
10'''
11
12import numpy as np
13import cv2 as cv
14
15def make_image():
16 img = np.zeros((500, 500), np.uint8)
17 black, white = 0, 255
18 for i in range(6):
19 dx = int((i%2)*250 - 30)
20 dy = int((i/2.)*150)
21
22 if i == 0:
23 for j in range(11):
24 angle = (j+5)*np.pi/21
25 c, s = np.cos(angle), np.sin(angle)
26 x1, y1 = np.int32([dx+100+j*10-80*c, dy+100-90*s])
27 x2, y2 = np.int32([dx+100+j*10-30*c, dy+100-30*s])
28 cv.line(img, (x1, y1), (x2, y2), white)
29
30 cv.ellipse( img, (dx+150, dy+100), (100,70), 0, 0, 360, white, -1 )
31 cv.ellipse( img, (dx+115, dy+70), (30,20), 0, 0, 360, black, -1 )
32 cv.ellipse( img, (dx+185, dy+70), (30,20), 0, 0, 360, black, -1 )
33 cv.ellipse( img, (dx+115, dy+70), (15,15), 0, 0, 360, white, -1 )
34 cv.ellipse( img, (dx+185, dy+70), (15,15), 0, 0, 360, white, -1 )
35 cv.ellipse( img, (dx+115, dy+70), (5,5), 0, 0, 360, black, -1 )
36 cv.ellipse( img, (dx+185, dy+70), (5,5), 0, 0, 360, black, -1 )
37 cv.ellipse( img, (dx+150, dy+100), (10,5), 0, 0, 360, black, -1 )
38 cv.ellipse( img, (dx+150, dy+150), (40,10), 0, 0, 360, black, -1 )
39 cv.ellipse( img, (dx+27, dy+100), (20,35), 0, 0, 360, white, -1 )
40 cv.ellipse( img, (dx+273, dy+100), (20,35), 0, 0, 360, white, -1 )
41 return img
42
44 img = make_image()
45 h, w = img.shape[:2]
46
47 contours0, hierarchy =
cv.findContours( img.copy(), cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
49
50 def update(levels):
51 vis = np.zeros((h, w, 3), np.uint8)
52 levels = levels - 3
54 3, cv.LINE_AA, hierarchy, abs(levels) )
56 update(3)
60 print('Done')
61
62
63if __name__ == '__main__':
64 print(__doc__)
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
int waitKey(int delay=0)
Waits for a pressed key.
void destroyAllWindows()
Destroys all of the HighGUI windows.
int createTrackbar(const String &trackbarname, const String &winname, int *value, int count, TrackbarCallback onChange=0, void *userdata=0)
Creates a trackbar and attaches it to the specified window.
void ellipse(InputOutputArray img, Point center, Size axes, double angle, double startAngle, double endAngle, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
Draws a simple or thick elliptic arc or fills an ellipse sector.
void line(InputOutputArray img, Point pt1, Point pt2, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
Draws a line segment connecting two points.
void drawContours(InputOutputArray image, InputArrayOfArrays contours, int contourIdx, const Scalar &color, int thickness=1, int lineType=LINE_8, InputArray hierarchy=noArray(), int maxLevel=INT_MAX, Point offset=Point())
Draws contours outlines or filled contours.
void approxPolyDP(InputArray curve, OutputArray approxCurve, double epsilon, bool closed)
Approximates a polygonal curve(s) with the specified precision.
void findContours(InputArray image, OutputArrayOfArrays contours, OutputArray hierarchy, int mode, int method, Point offset=Point())
Finds contours in a binary image.
int main(int argc, char *argv[])
Definition highgui_qt.cpp:3