How to detect license plates using OpenCV Python?

License plate detection is a crucial component in automated vehicle recognition systems. We can detect license plates in images using OpenCV's Haar cascade classifier, which is a machine learning-based object detection method.

What is Haar Cascade Classifier?

A Haar cascade classifier uses machine learning to detect objects in images. To train a license plate classifier, the algorithm needs many positive images (with license plates) and negative images (without license plates). Once trained, it can detect license plates in new images.

Downloading the Haarcascade File

You can download pre-trained Haar cascades from the OpenCV GitHub repository:

https://github.com/opencv/opencv/tree/master/data/haarcascades

For license plate detection, download the haarcascade_russian_plate_number.xml file. Open it in raw format, right-click and save it to your project directory.

Implementation Steps

To detect license plates in an image, follow these steps:

  • Import the required OpenCV library

  • Read the input image and convert it to grayscale

  • Load the Haar cascade classifier for plate detection

  • Use detectMultiScale() to find license plates

  • Draw bounding rectangles around detected plates

  • Display or save the results

Complete Example

Here's a complete program that detects license plates in an image:

import cv2
import numpy as np

# Read input image
img = cv2.imread("car_image.jpg")

# Convert to grayscale (fix: should be BGR2GRAY, not BGR2RGB)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Load haarcascade for license plate detection
cascade = cv2.CascadeClassifier('haarcascade_russian_plate_number.xml')

# Detect license plates
# Parameters: image, scaleFactor, minNeighbors
plates = cascade.detectMultiScale(gray, 1.2, 5)
print('Number of detected license plates:', len(plates))

# Draw rectangles around detected plates
for (x, y, w, h) in plates:
    # Draw green rectangle around license plate
    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
    
    # Extract the license plate region
    plate_region = gray[y:y + h, x:x + w]
    
    # Save the extracted plate
    cv2.imwrite('detected_plate.jpg', plate_region)
    
    # Display the plate region
    cv2.imshow('Detected License Plate', plate_region)

# Display the original image with bounding boxes
cv2.imshow('License Plate Detection', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Key Parameters

The detectMultiScale() method accepts these important parameters:

  • scaleFactor (1.2): How much the image size is reduced at each scale

  • minNeighbors (5): How many neighbors each candidate rectangle should retain

  • minSize: Minimum possible object size; smaller objects are ignored

  • maxSize: Maximum possible object size

Output

The program will output:

Number of detected license plates: 1

The detected license plate will be highlighted with a green rectangle, and the extracted plate region will be saved as a separate image file.

Common Issues and Solutions

  • No plates detected: Adjust scaleFactor (try 1.1 to 1.3) or minNeighbors (try 3-7)

  • Too many false positives: Increase minNeighbors or add minSize parameter

  • Poor detection on different angles: Try multiple cascade files or preprocess images

Conclusion

OpenCV's Haar cascade classifier provides an effective way to detect license plates in images. While it works well for standard license plates, modern approaches like deep learning models offer better accuracy for complex scenarios.

Updated on: 2026-03-26T23:06:34+05:30

15K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements