Я написал фрагмент кода для определения формы дорожных знаков. Код работает достаточно хорошо для обнаружения форм, за исключением дорожных знаков треугольной формы. Я попытался настроить значение (т. Е. 0,01) на второй параметр функции cv2.approxPolyDP (c, 0,01 * peri, True), но я не могу найти правильное значение.
Я попытался настроить значение (т. Е. 0,01) на второй параметр функции cv2.approxPolyDP (c, 0,01 * пери, True), но мне не удалось найти правильное значение.
from os import listdir
from os.path import isfile, join
import cv2
myPath = '/home/raghu/Downloads/SSIMProject/ImageData/sample'
files = [ f for f in listdir(myPath) if isfile(join(myPath, f))]
for eachFile in files:
img = cv2.imread(join(myPath, eachFile), 1)
cv2.imshow('img1', img[:,:,0])
ret, thresh1 = cv2.threshold(img[:,:,0], 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
contours,hierarchy = cv2.findContours(thresh1, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
font = cv2.FONT_HERSHEY_COMPLEX
for cnt in contours:
approx = cv2.approxPolyDP(cnt, 0.0112 * cv2.arcLength(cnt, True), True)
print(len(approx))
if len(approx) == 8:
print("Octagon")
cv2.putText(img, 'Octagon', (10, 30), font, 0.3, (0, 0, 0), 1, cv2.LINE_AA)
cv2.drawContours(img, [cnt], 0, (0, 255, 0), 2)
elif len(approx) == 7:
print("Heptagon")
cv2.putText(img, 'Heptagon', (10, 30), font, 0.3, (0, 0, 0), 1, cv2.LINE_AA)
cv2.drawContours(img, [cnt], 0, (0, 255, 0), 2)
elif len(approx) == 6:
print("Hexagon")
cv2.putText(img, 'Hexagon', (10, 30), font, 0.3, (0, 0, 0), 1, cv2.LINE_AA)
cv2.drawContours(img, [cnt], 0, (0, 255, 0), 2)
elif len(approx) == 5:
print("Pentagon")
cv2.putText(img, 'Pentagon', (10, 30), font, 0.3, (0, 0, 0), 1, cv2.LINE_AA)
cv2.drawContours(img, [cnt], 0, (0, 255, 0), 2)
elif len(approx) == 4:
print("Square")
cv2.putText(img, 'Square', (10, 30), font, 0.3, (0, 0, 0), 1, cv2.LINE_AA)
cv2.drawContours(img, [cnt], 0, (0, 255, 0), 2)
elif len(approx) == 3:
print("Triangle")
cv2.putText(img, 'Triangle', (10, 30), font, 0.3, (0, 0, 0), 1, cv2.LINE_AA)
cv2.drawContours(img, [cnt], 0, (0, 255, 0), 2)
else:
print("Circle")
cv2.putText(img, 'Circle', (10, 30), font, 0.3, (0, 0, 0), 1, cv2.LINE_AA)
cv2.drawContours(img, [cnt], 0, (0, 255, 0), 2)
cv2.imshow('sign', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Я ожидаю, что будут обнаружены дорожные знаки треугольной формы.