-
Notifications
You must be signed in to change notification settings - Fork 37
/
1296-1.py
59 lines (47 loc) · 1.88 KB
/
1296-1.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import cv2 as cv
def setLabel(image, str, contour):
(text_width, text_height), baseline = cv.getTextSize(str, cv.FONT_HERSHEY_SIMPLEX, 0.7, 1)
x,y,width,height = cv.boundingRect(contour)
pt_x = x+int((width-text_width)/2)
pt_y = y+int((height + text_height)/2)
cv.rectangle(image, (pt_x, pt_y+baseline), (pt_x+text_width, pt_y-text_height), (200,200,200), cv.FILLED)
cv.putText(image, str, (pt_x, pt_y), cv.FONT_HERSHEY_SIMPLEX, 0.7, (0,0,0), 1, 8)
img_color = cv.imread('test.png', cv.IMREAD_COLOR)
cv.imshow('result', img_color)
cv.waitKey(0)
img_gray = cv.cvtColor(img_color, cv.COLOR_BGR2GRAY)
cv.imshow('result', img_gray)
cv.waitKey(0)
ret,img_binary = cv.threshold(img_gray, 127, 255, cv.THRESH_BINARY_INV|cv.THRESH_OTSU)
cv.imshow('result', img_binary)
cv.waitKey(0)
contours, hierarchy = cv.findContours(img_binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
for cnt in contours:
size = len(cnt)
print(size)
epsilon = 0.005 * cv.arcLength(cnt, True)
approx = cv.approxPolyDP(cnt, epsilon, True)
size = len(approx)
print(size)
cv.line(img_color, tuple(approx[0][0]), tuple(approx[size-1][0]), (0, 255, 0), 3)
for k in range(size-1):
cv.line(img_color, tuple(approx[k][0]), tuple(approx[k+1][0]), (0, 255, 0), 3)
if cv.isContourConvex(approx):
if size == 3:
setLabel(img_color, "triangle", cnt)
elif size == 4:
setLabel(img_color, "rectangle", cnt)
elif size == 5:
setLabel(img_color, "pentagon", cnt)
elif size == 6:
setLabel(img_color, "hexagon", cnt)
elif size == 8:
setLabel(img_color, "octagon", cnt)
elif size == 10:
setLabel(img_color, "decagon", cnt)
else:
setLabel(img_color, str(size), cnt)
else:
setLabel(img_color, str(size), cnt)
cv.imshow('result', img_color)
cv.waitKey(0)