I am making plate recognition app. I managed to find contours of each element of plate. How can I cut them out, resize and combine, so tesseract ocr can do it's job and recognize the text from the plate. To make it more clear I attach pictures from the article.
Asked
Active
Viewed 482 times
0
1 Answers
0
This is pretty straight forward. Find the contours, loop through the contours, find bounding rect of each contour, crop out the part, resize the cropped part and store them in a list. You can then iterate the new list and join them using numpy's hstack
. I've shared the full code, however you need some filtering technique to remove redundant contours so take care of that.
code:
import cv2
import numpy as np
img = cv2.imread('1.png',0)
image, contours, hierarchy = cv2.findContours(img.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
boxes = list()
cropped_imageList = list()
for c in contours:
#if you want to store the original coordinates
x,y,w,h = cv2.boundingRect(c)
boxes.append((x,y,w,h))
#crop out the part of the image and resize
crop_img = img[y:y+h, x:x+w]
crop_img = cv2.resize(crop_img,(50,50))
cropped_imageList.append(crop_img)
#join them back
imstack = cropped_imageList[0]
for imgs in cropped_imageList[1:]:
imstack = np.hstack((imstack,imgs))
NOTE: This will randomly join the letters found, one way to solve this is to sort the boxes
list using x,y
and then crop instead.
Why are you doing all this when tesseract can directly detect the text? If you want better results, you should go for modern approached using deep learning.

Rakshith G B
- 816
- 2
- 8
- 24
-
For some reason tesseract can't manage part of russian plate that is on the right (172 rus), so I decided to make it more clear for him. I don't use deep learning cause lack of skills and time and task is basically to read text from clear plate – Dec 09 '18 at 17:03
-
Hmm, yeah I guess you could do this type of preprocessing then. Good luck. – Rakshith G B Dec 09 '18 at 17:08
-
Can you give me a tip how to compare bounding rec's coordinates? Need to do it to sort the list of rec's and to delete rectangle that is in another rectangle. I just can't understand the syntax – Dec 09 '18 at 17:20
-
For sorting only based on x coordinate, you can just do `boxes = sorted(boxes)`. And for filtering, I can only think of brute force right now. Iterate each box and check if x,y,w,h falls inside another box. – Rakshith G B Dec 09 '18 at 17:25
-
can you give me the cue about how to iterate through imstack so I can read contours separatly (some troubles with tesseract reading)? I guess I don't completely understand it's structure – Dec 17 '18 at 18:10
-
@LokiTheCreator I can’t really help you out without seeing what’s you’ve done and where you’re stuck at. I need to see some code. – Rakshith G B Dec 17 '18 at 18:13