How to Detect License Plates Using OpenCV Python?
Have you ever thought about how traffic cameras or automated toll booths identify license plates? The answer lies in the use of computer vision and image processing techniques that allow the detection and recognition of license plates. In this article, we will learn how to use OpenCV library in Python to detect license plates in an image or video.
Background
OpenCV (Open Source Computer Vision Library) is an open-source computer vision and machine learning software library that allows developers to write real-time image and video processing applications. Its capabilities include object detection, tracking, image processing, and machine learning.
Prerequisites
Before we begin, you need a basic understanding of Python programming language, familiarity with OpenCV library, and the installation of required packages.
We require the following packages to perform the license plate detection:
- OpenCV
- imutils
- numpy
Installing Required Packages
To install these packages, open your terminal and use the following commands:
pip install opencv-python
pip install imutils
pip install numpy
Steps to Detect License Plates in an Image
The following are the steps required to detect license plates in an image:
Step 1: Read the Image
First, we need to read the input image using OpenCV’s imread() function.
import cv2
image = cv2.imread('car.jpg')
cv2.imshow('Input Image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Step 2: Preprocess the Image
Next, we need to preprocess the image by converting it to grayscale and applying Gaussian blur to reduce noise.
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow('Grayscale Image', gray)
cv2.waitKey(0)
blur = cv2.GaussianBlur(gray, (3, 3), 0)
cv2.imshow('Blurred Image', blur)
cv2.waitKey(0)
Step 3: Detect Edges
We use a Canny edge detection algorithm to detect the edges in the grayscale image.
edged = cv2.Canny(blur, 100, 200)
cv2.imshow('Edged Image', edged)
cv2.waitKey(0)
Step 4: Find Contours
Contours are the boundaries of continuous lines in an image. We use the findContours() function in OpenCV to find the contours in the edged image.
cnts, hierarchy = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
Step 5: Filter Contours
We filter the contours based on their shape and size to eliminate false positives. In this case, we are looking for rectangles that are wider than they are tall since the license plate is typically rectangular.
(rects,_) = imutils.object_detection.sort_contours(rects)
for rect in rects:
if cv2.contourArea(rect) < 200:
continue
peri = cv2.arcLength(rect, True)
approx = cv2.approxPolyDP(rect, 0.02 * peri, True)
if len(approx) == 4:
(x, y, w, h) = cv2.boundingRect(approx)
aspectRatio = w / float(h)
if aspectRatio > 1.5 and aspectRatio < 2.5:
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.imshow('Detected License Plate', image)
cv2.waitKey(0)
Step 6: Display Output
Finally, we display the output image with the license plate regions highlighted in green rectangles.
cv2.imshow('Detected License Plate', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Steps to Detect License Plates in a Video
The steps to detect license plates in a video are similar to the ones for an image with a few differences in how we read and display the video stream.
import cv2
import imutils
cap = cv2.VideoCapture('video.mp4')
while True:
ret, frame = cap.read()
if not ret:
break
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (3, 3), 0)
edged = cv2.Canny(blurred, 100, 200)
cnts, hierarchy = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
rects = [cv2.boundingRect(cnt) for cnt in cnts]
for rect in rects:
if cv2.contourArea(rect) < 200:
continue
peri = cv2.arcLength(rect, True)
approx = cv2.approxPolyDP(rect, 0.02 * peri, True)
if len(approx) == 4:
(x, y, w, h) = rect
aspectRatio = w / float(h)
if aspectRatio > 1.5 and aspectRatio < 2.5:
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.imshow('License Plate Detection', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Conclusion
In this article, we learned how to use OpenCV library in Python to detect license plates in an image or video. The process involves reading and preprocessing the input image or video, detecting edges, finding contours, filtering contours based on shape and size, and displaying the output. With these techniques, developers can create applications that automate license plate recognition in various scenarios, such as traffic cameras, toll booths, and parking lots.