0

Good Evening everyone, I'm grateful in advance for your help.
I'm a newbee, however trying to write a code whick helps computer to recognise Traffic Signs on images.
I use OpenCV library, python and google colab.
I used template matching method, however I've faced with a serious problem : this application has a very low accuracy and I need help to improve this code to increase detection ability.
Maybe I should iteratively rotate image or maybe other solutions exist. Help please, how can I improve my code. The code is below :

img_original = cv2.imread(fileName, 0)


scale_percentage = 150
width = int(img_original.shape[1]*scale_percentage/100)
height = int(img_original.shape[0]*scale_percentage/100)
new_scale = (width, height)
img_original1 = cv2.resize(img_original, new_scale, interpolation = cv2.INTER_AREA)
cv2_imshow(img_original1)
img_original1.shape

img_original2 = cv2.medianBlur(img_original1, 5)

template = cv2.imread(image_template, 0)

width1, height1 = template.shape[:2]

#Обработка изображения
threshold_image = cv2.adaptiveThreshold(img_original2, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
threshold_template = cv2.adaptiveThreshold(template, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
cv2_imshow(threshold_image)

#Размытие изображения
blurred_image = cv2.GaussianBlur(threshold_image, (11,11), 0)

#Сравнение с оригиналом, порог, 
result = cv2.matchTemplate(blurred_image,threshold_template,cv2.TM_SQDIFF_NORMED)
threshold = 0.46


#np.where (condition, [x,y]) при условии(булеан, массив), где True, возвращает значения х или у в зависимости от того, где True
loc = np.where( result >= threshold)

#Цикл где происходит обход изображения по рядам и колоннам по очереди и затем рисуется прямоугольник в областях, где находится совпадение
for pt in zip(*loc[::-1]):
    cv2.rectangle(blurred_image, pt, (pt[0] + width1, pt[1] + height1), (0,0,255), 1)
cv2.imwrite('result.jpg', blurred_image)
cv2_imshow(blurred_image)
cv2_imshow(threshold_template)
cv2_imshow(result)

Here are the results: result

original

template

Yunus Temurlenk
  • 4,085
  • 4
  • 18
  • 39

0 Answers0