How can I draw only the largest rectangle contour to detect Vehicle License Plate (specifically for motorbike license plate)

Question:

I’ve been working on detecting motorbike license plate using only OpenCV. My question is:

  1. Can I find and draw only the largest contour (which is around the license plate)?
  2. If not then what should I do for this problem?
  3. Are there any other methods for detecting motorbike license plate?

Here is my original image

And here is the image after I detect every rectangle contours

Here is the source code

import cv2

image = cv2.imread('image_0002.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (3, 3), 0)
canny = cv2.Canny(blurred, 120, 255, 1)

# Find contours
cnts = cv2.findContours(canny, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]

# Iterate thorugh contours and draw rectangles around contours
for c in cnts:
    x,y,w,h = cv2.boundingRect(c)
    cv2.rectangle(image, (x, y), (x + w, y + h), (36,255,12), 2)

cv2.imshow('canny', canny)
cv2.imshow('image', image)
cv2.imwrite('canny.png', canny)
cv2.imwrite('image.png', image)
cv2.waitKey(0)
Asked By: Michael Nguyen

||

Answers:

1- For the largest rectangle – calculate areas of all rectangles

largest_area = 0
for c in cnts:
    x,y,w,h = cv2.boundingRect(c)
    area = 4*w*h
    if area > largest_area:
        largest_area = area
        largest_area_contour = c

Now, we have got the largest rectangle. Lets draw it (outside of for loop).

x,y,w,h = cv2.boundingRect(largest_area_contour)
cv2.rectangle(image, (x, y), (x + w, y + h), (36,255,12), 2)

2- Your model is giving you too many false positives. Ensure that largest rectangle method is always working.

3- License plate (car or bike doesn’t matter) reading is a well studied toy problem. You will find many discussion on internet.

Answered By: Abhi25t