1

I've created simple neural network which can recognize separate digits and characters. I want the neural network to recognize licence plate on the car. In order to do it I have to separate symbols on image. For example, i have to find symbols on the image and save each symbol to file (png or jpg):

Source image:

Founded symbols:

Separated symbol in file:

How can I find symbol and save green rectangles to simple png (or jpg) file using python?

Ishara Madhawa
  • 3,549
  • 5
  • 24
  • 42
Arty
  • 39
  • 1
  • 3

1 Answers1

1

If you are looking to perform using OpenCV you can check out this solution:

You can perform symbol detection by finding contours above a certain area. Their corresponding bounding boxes can be drawn on a blank image of the same shape.

import cv2

img = cv2.imread(r'C:\Users\Desktop\pic.png') 
cv2.imshow('Image', img)

#--- create a blank image of the same size for storing the green rectangles (boundaries) ---
black = np.zeros_like(img)

#--- convert your image to grayscale and apply a threshold ---
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 
ret2, th2 = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

#--- perform morphological operation to ensure smaller portions are part of a single character ---
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
threshed = cv2.morphologyEx(th2, cv2.MORPH_CLOSE, kernel)

#--- find contours ---
imgContours, Contours, Hierarchy = cv2.findContours(threshed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
for contour in Contours:

    #--- select contours above a certain area ---
    if cv2.contourArea(contour) > 200:

        #--- store the coordinates of the bounding boxes ---
        [X, Y, W, H] = cv2.boundingRect(contour)

        #--- draw those bounding boxes in the actual image as well as the plain blank image ---
        cv2.rectangle(img2, (X, Y), (X + W, Y + H), (0,0,255), 2)
        cv2.rectangle(black, (X, Y), (X + W, Y + H), (0,255,0), 2)

cv2.imshow('contour', img2)
cv2.imshow('black', black)

Here is the result:

enter image description here

enter image description here

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87