CLWONG CLWONG - 2 months ago 20x
Python Question

License plate Detection from car camera

I am using Python and Opencv. I am doing a project to recognize the license plate from a car camera.

I have tried to use

, but I still cant recognize the plate.

Here is the frame I captured.
enter image description here


First, I convert the image into gray-scale, increase the contract of color and finally convert it into "edged image"

img = cv2.imread("plate.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.equalizeHist(gray)
edged = cv2.Canny(gray, 200, 255)

Here is the result that what I get:
enter image description here


Afterwards, I try to find a rectangle contour as following, I tried to filter out the irrelevant rectangle by area and length and irregular polygon by

(cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnts=sorted(cnts, key = cv2.contourArea, reverse = True)[:10]

# loop over our contours
plate_candidates = []
for c in cnts:
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.02 * peri, True)
area = cv2.contourArea(approx)
if len(approx) == 4 and area <=1000 and area >=500 and self._length_checking(approx):
hull = cv2.convexHull(approx,returnPoints = True)
if len(hull) ==4:
cv2.drawContours(show, [approx], -1, (0,255,0), 3)

But still, I cannot recognize the plate. I am looking for help how can I detect the license plate. Thank you.


You could use minimal bounding rectangle of the convex hull to calculate the "rectangleness" of your candidate contours (in the latest version of openCV you can use cv2.boxPoints to calculate rectPoints):

def rectangleness(hull):
    rect = cv2.boundingRect(hull)
    rectPoints = np.array([[rect[0], rect[1]], 
                           [rect[0] + rect[2], rect[1]],
                           [rect[0] + rect[2], rect[1] + rect[3]],
                           [rect[0], rect[1] + rect[3]]])
    intersection_area = cv2.intersectConvexConvex(np.array(rectPoints), hull)[0] 
    rect_area = cv2.contourArea(rectPoints)
    rectangleness = intersection_area/rect_area
    return rectangleness

However in your case this is actually overkill, it's enough to use the area of the polygon — either of the polygons within your area cutoff (the first two contours in cnts) could be used to obtain the bounding rectangle surrounding the licence plate.