I
I
Ilya Neizvestnyj2019-11-10 15:08:12
Python
Ilya Neizvestnyj, 2019-11-10 15:08:12

How to find a specific object in an image?

There is a neural network that classifies images of road signs 200x200.
5dc7fcf66a520545212482.png
5dc7fd01eb043531459996.png
How can we now find these signs in a 1920x1080 image? I tried to cut along the contours, but along with the signs, any garbage is also saved.
5dc7fd7583f1d976979995.png

import cv2
from PIL import Image

image = cv2.imread("C:/Users/pikro/PycharmProjects/ML/road_sign/test.png")

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
edged = cv2.Canny(image, 10, 200)
_, cnts, _ = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
idx = 0

rects = sorted([cv2.boundingRect(c) for c in cnts], key=lambda x: x[0])

for x, y, w, h in rects:
    if w > 100 and h > 100:
        idx += 1
        new_img = image[y - 3:y + h + 3, x - 3:x + w + 3]
        cv2.imwrite("C:/Users/pikro/PycharmProjects/ML/road_sign/crop/" + str(idx) + '.png', new_img)
cv2.imshow("im", image)
cv2.waitKey(0)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dimonchik, 2019-11-10
@dimonchik2013

https://www.pyimagesearch.com/2019/11/04/traffic-s...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question