-
Notifications
You must be signed in to change notification settings - Fork 1
/
poly_detector2.py
35 lines (32 loc) · 1.2 KB
/
poly_detector2.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
import cv2
import numpy as np
output=open('out.txt', 'w')
img = cv2.imread("hexa.jpg", cv2.IMREAD_GRAYSCALE)
_, threshold = cv2.threshold(img, 240, 255, cv2.THRESH_BINARY)
_, contours, _ = cv2.findContours(threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
font = cv2.FONT_HERSHEY_COMPLEX
for cnt in contours:
approx = cv2.approxPolyDP(cnt, 0.01*cv2.arcLength(cnt, True), True)
cv2.drawContours(img, [approx], 0, (0), 5)
# x = approx.ravel()[0]
#y = approx.ravel()[1]
x = approx.ravel()[0]
y = approx.ravel()[1]
if len(approx) == 3:
cv2.putText(img, "Triangle", (x, y), font, 1, (0))
elif len(approx) == 4:
cv2.putText(img, "Rectangle", (x, y), font, 1, (0))
elif len(approx) == 5:
cv2.putText(img, "Pentagon", (x, y), font, 1, (0))
elif len(approx) == 6:
cv2.putText(img, "Hexagon", (x, y), font, 1, (0))
output.write("SHAPE : Hexagon" + '\n')
elif 6 < len(approx) < 15:
cv2.putText(img, "Ellipse", (x, y), font, 1, (0))
else:
cv2.putText(img, "Circle", (x, y), font, 1, (0))
cv2.imshow("shapes", img)
cv2.imshow("Threshold", threshold)
cv2.waitKey(0)
cv2.destroyAllWindows()
output.close()