Number plate Detection with Python.

Divesh Karkera
3 min readJul 2, 2021

Dear Readers,

In this blog, I will try to explain how we can perform Number Plate Recognition in Python using Tesseract-OCR.

It can also be performed in Matlab using steps:
• Read the number plate.
• Resize the image to keep the aspect ratio the same.
• Convert the image into a greyscale image.
• Apply a median filter to remove noise.
• Use a structuring element to perform morphological operations.
• Dilate the grey image with the structuring element
• Use morphological gradient for edge enhancement.
• Convert the class to double.
• Perform the convolution of the doubled image for brightening the images.
• Scale the intensity between 0 to 1.
• Convert the class from double to binary.
• Eliminating the possible horizontal lines from the output image of the region grow that could be edges of the license plate.
• Filling all the regions of the image.
• Thinning the image to ensure character isolation.
• Selecting all the regions that are of pixel area more than 100.
• Two properties ‘BoundingBox’ and binary ‘Image’ corresponding to these
• Bounding boxes are acquired.
• Selecting all the bounding boxes in a matrix of order numberofboxesX4.
• Calling of controlling function.
• Function ‘controlling’ outputs the array of indices of boxes required for the extraction of characters.
• Cell array of ‘Image’ (one of the properties of region props)
• Initializing the variable of the number plate string.
• Extracting the binary image corresponding to the indices in ‘re.
• Reading the letter corresponding to the binary image ‘N’.

For Python:
1. License Plate Detection
2. Character Segmentation
3. Character Recognition

  1. Read the image
img = cv2.imread('D://maru.jpeg',cv2.IMREAD_COLOR)

2. Resize the input image.

img = cv2.resize(img, (620,480) )

3. Convert it into a grayscale image.

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

4. Apply Blurring filter

destination_image = cv2.bilateralFilter(source_image, diameter of pixel, sigmaColor, sigmaSpace)sigmaColor  and sigmaSpace are values from 15 to above to blur out background information.gray = cv2.bilateralFilter(gray, 13, 15, 15)

5. Detection of edges

Canny Edge Detection is the one that I am using.

edged = cv2.Canny(gray, 30, 200)

6. Look for contours in the image

Contours can be explained simply as a curve joining all the continuous points, having the same color or intensity.

contours = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = imutils.grab_contours(contours)
contours = sorted(contours, key = cv2.contourArea, reverse = True)[:10]

Once the contours have been detected we sort them from big to small and consider only the first 10 results ignoring the others. In our image, the contour could be anything that has a closed surface but of all the obtained results the license plate number will also be there since it is also a closed surface.

To filter the license plate image among the obtained results, we will loop through all the results and check which has a rectangle shape contour with four sides and a closed figure. Since a license plate would definitely be a rectangle four-sided figure.

for c in contours:
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.018 * peri, True)
if len(approx) == 4:
screenCnt = approx
break
if screenCnt is None:
detected = 0
print ("No contour detected")
else:
detected = 1
if detected == 1:
cv2.drawContours(img, [screenCnt], -1, (0, 0, 255), 3)

Once we have found the right counter we save it in a variable called screening and then draw a rectangle box around it to make sure we have detected the license plate correctly.

7. Masking

Masking is an image processing method in which we define a small ‘image piece’ and use it to modify a larger image. Masking is the process that is underneath many types of image processing, including edge detection, motion detection, and noise reduction.

mask = np.zeros(gray.shape,np.uint8)
new_image = cv2.drawContours(mask,[screenCnt],0,255,-1,)
new_image = cv2.bitwise_and(img,img,mask=mask)

8. Character Segmentation

The next step in Number Plate Recognition is to segment the license plate out of the image by cropping it and saving it as a new image. We can then use this image to detect the character in it.

(x, y) = np.where(mask == 255)
(topx, topy) = (np.min(x), np.min(y))
(bottomx, bottomy) = (np.max(x), np.max(y))
Cropped = gray[topx:bottomx+1, topy:bottomy+1]

9. Character Recognition

The final step in this Number Plate Recognition is to actually read the number plate information from the segmented image. We will use the by tesseract package to read characters from images.

text = pytesseract.image_to_string(Cropped, config='--psm 11')

10. Successful Detection of Number Plate

--

--