License Plate Recognition (LPR)
License Plate Recognition (LPR) is a technology that uses optical character recognition (OCR) to read vehicle registration plates. LPR systems are commonly used for various applications, including traffic management, toll collection, and parking systems. In this topic, we will explore the principles of LPR, how it works, and how to implement it using OpenCV.
Overview of License Plate Recognition
LPR involves several steps to accurately detect and recognize license plates: 1. Image Acquisition: Capture images of vehicles that include the license plate. 2. Preprocessing: Enhance the image quality to make the license plate more detectable. 3. License Plate Detection: Identify the location of the license plate within the image. 4. Character Segmentation: Isolate individual characters on the license plate. 5. Character Recognition: Convert the isolated characters into machine-readable text.
Applications of LPR
- Traffic Monitoring: Automatically read license plates for law enforcement purposes. - Toll Systems: Charge vehicles automatically as they pass through toll booths. - Parking Management: Monitor and control parking spaces based on vehicle entry and exit times.Implementing LPR with OpenCV
Step 1: Image Acquisition
To start, you need to acquire images of vehicles. In real-world applications, this might involve using cameras placed in strategic locations. For our example, we’ll use a sample image that has a visible license plate.Step 2: Preprocessing
Preprocessing involves converting the image to grayscale and applying Gaussian blur to reduce noise. Here’s a code snippet:`
python
import cv2
Load the image
image = cv2.imread('car.jpg')Convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)Apply Gaussian blur
blurred = cv2.GaussianBlur(gray, (5, 5), 0)`
Step 3: License Plate Detection
Using edge detection and contour finding, we can locate the license plate. The following code uses the Canny edge detector and finds contours:`
python
Apply Canny edge detector
edges = cv2.Canny(blurred, 30, 150)Find contours in the edged image
contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)Loop through contours to find potential license plates
for contour in contours: x, y, w, h = cv2.boundingRect(contour) aspect_ratio = w / float(h) if 2 < aspect_ratio < 5:A common aspect ratio for license plates
plate = gray[y:y+h, x:x+w] break`
Step 4: Character Segmentation
Once the license plate area is detected, we can segment the characters. This involves thresholding and finding contours:`
python
Apply thresholding
_, thresh = cv2.threshold(plate, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)Find contours again for character segmentation
char_contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)Sort contours from left to right
char_contours = sorted(char_contours, key=lambda c: cv2.boundingRect(c)[0])characters = []
for char_contour in char_contours:
(x, y, w, h) = cv2.boundingRect(char_contour)
char = plate[y:y+h, x:x+w]
characters.append(char)
`
Step 5: Character Recognition
To recognize characters, you can use an OCR library such as Tesseract. Here’s how:`
python
import pytesseract
Recognize characters using Tesseract
recognized_text = '' for char in characters: text = pytesseract.image_to_string(char, config='--psm 8')PSM 8 for single word recognition
recognized_text += text.strip()print('Recognized License Plate:', recognized_text)
`