How to filter out unwanted contours during real-time OCR?
Additional unwanted contour
In the image above, with just a slight camera movement, additional contours appear.
I’ve tried to implement the following method:
Processing
plate_img = img[y1:y2, x1:x2] # extract license plate from the image
gray = cv.cvtColor(plate_img, cv.COLOR_BGR2GRAY)
if np.mean(gray) > 128:
# white plate
gray = 255 - gray
_, binary = cv.threshold(gray, 200, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)
erode = cv.erode(binary, (3,3))
dilate = cv.dilate(erode, (3,3))
Find Contours
contours, _ = cv.findContours(erode, cv.RETR_EXTERNAL , cv.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key=lambda contour: cv.boundingRect(contour)[0])
image_copy = plate_img.copy()
# find contour
num_plate = [] # Clear the list for each new plate
for contour in contours:
x, y, w, h = cv.boundingRect(contour)
ratio = h/w
if 1 <= ratio <= 3.5: # Check the aspect ratio of the contour
if h/plate_img.shape[0] >= 0.3: # Check the height ratio of the contour
cv.rectangle(plate_img, (x, y), (x + w, y + h), (0, 0, 255), thickness=3) # Draw a rectangle around the contour
expected : contours only focusing on the characters
result : random contours still appear and being detected as character