-
Notifications
You must be signed in to change notification settings - Fork 0
/
20_contour_features.py
43 lines (33 loc) · 988 Bytes
/
20_contour_features.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import cv2
import numpy as np
# load image
img = cv2.imread('colored_shapes.png')
imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# get contours based on threshold
ret, thresh = cv2.threshold(imgray, 200, 255, 0)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)
# get specific contour
cnt = contours[1]
# get contour area
area = cv2.contourArea(cnt)
print('area: {}'.format(area))
# get contour moment
M = cv2.moments(cnt)
print('moment: {}'.format(M))
# get contour perimeter
perimeter = cv2.arcLength(cnt, True)
print('perimeter: {}'.format(perimeter))
# check if convex
convex = cv2.isContourConvex(cnt)
print('convex: {}'.format(convex))
# get contour approx
epsilon = 0.1*cv2.arcLength(cnt, True)
approx = cv2.approxPolyDP(cnt, epsilon, True)
# draw contour
cv2.drawContours(img, [cnt], 0, (0, 255, 0), 3)
# show image
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.waitKey(1)