How to OCR License Plate in C# (Tutorial)

In this tutorial, we will learn about programmatic license plate recognition. We will take some sample license plate images and extract license plate numbers. We will develop a C# .NET program to perform this automatic license plate recognition. This tutorial is simple and easy so is perfect even for a beginner C# programmer.

You may wonder why we might need to determine the license plate number from an image. The answer is straightforward: maybe you are developing an application that takes data from CCTV videos, and your program needs to read the license plate characters in order to identify the vehicle.

h2

IronOCR

We need an efficient Optical Character Recognition (OCR) engine that can process the image and return the license plate number accurately, irrespective of language or characters on the license plate.

There are many libraries available to perform OCR. Some of them are paid, some of them are difficult to use, and some of them are not efficient or accurate. IronOCR is free for development (providing a one-month free trial for commercial use) and is easy to use. It supports over 150 languages and provides better accuracy than most OCR libraries.

Features of IronOCR

  • Reading text from many formats such as images (jpg, png, gif, tiff, BMP), streams, and PDFs
  • Correcting low-quality scans and photos with a plethora of filters such as Deskew, Denoise, Binarize, Enhance Resolution, Dilate, and many more
  • Reading barcodes from over 20 barcode formats and QR code support
  • Utilizing the latest build of Tesseract OCR, tuned for performance
  • Exporting searchable PDFs, HTML, and image content text.

IronOCR uses a Tesseract OCR Engine to extract text from an image. IronOCR can be used with OpenCV (an open-source, market-leading object detection tool) to carry out Computer Vision. This allows the library to detect areas where text exists in an image. This is useful when only specific regions of the input image contains text (such as pictures of vehicles with license plate numbers in display).

Create a Visual Studio Project

We will develop an application that will use IronOCR to extract license plate numbers from images.

The first step is to create a Visual Studio project.

Open Visual Studio. Click on Create New Project, and select the Console Application Project Template. Click on the Next button, and name the project (for example, "License Plate OCR"). Click on the Next button, and select your target framework. Finally, click on the Create button to create the project. The project will be created as shown below.

How to Perform OCR on License Plates, Figure 1: License Plates OCR Project

Creating a Visual Studio Project for our License Plate OCR Project

Install IronOCR NuGet Package

Next, we need to install the IronOCR library. The easiest way is to install it via the NuGet Package Manager.

Click on Tools from the top menu bar, and select NuGet Package Manager > Manage NuGet Packages for Solution, as shown below.

How to Perform OCR on License Plates, Figure 2: NuGet Package Manager UI

Locating the NuGet Package Manager UI in Visual Studio

The following window will appear.

How to Perform OCR on License Plates, Figure 3: NuGet Package Manager UI

The NuGet Package Manager UI in Visual Studio

Click on Browse, and search for IronOCR. Select the IronOCR package and click on the Install button, as shown below.

How to Perform OCR on License Plates, Figure 3: Install the IronOCR library in NuGet Package Manager UI

Locate the IronOCR package in the Package Manager UI and click the Install button to add it to the project

Read License Plates using Optical Character Recognition

Let's write a program to read the license plate.

We will read the following number plate:

How to Perform OCR on License Plates, Figure 5: The License Plate

Image of a License Plate with license plate numbers clearly visible

Add the following namespace:

using IronOcr;
using IronOcr;
Imports IronOcr
VB   C#

Then, add the following lines of code:

var Ocr = new IronTesseract();
using (var Input = new OcrInput(@"D:\Liscence Plate\plate3.jpg"))
{
    Input.DeNoise(); // fixes digital noise and poor scanning
    Input.ToGrayScale();
    var Result = Ocr.Read(Input);
    Console.WriteLine(Result.Text);
}
var Ocr = new IronTesseract();
using (var Input = new OcrInput(@"D:\Liscence Plate\plate3.jpg"))
{
    Input.DeNoise(); // fixes digital noise and poor scanning
    Input.ToGrayScale();
    var Result = Ocr.Read(Input);
    Console.WriteLine(Result.Text);
}
Dim Ocr = New IronTesseract()
Using Input = New OcrInput("D:\Liscence Plate\plate3.jpg")
	Input.DeNoise() ' fixes digital noise and poor scanning
	Input.ToGrayScale()
	Dim Result = Ocr.Read(Input)
	Console.WriteLine(Result.Text)
End Using
VB   C#

This performs the following:

  • Initializes the IronTesseract object with default settings.
  • Creates a new OCRInput object populated with an input image file.
  • Invokes DeNoise, which removes digital noise. This filter should only be used where noise is expected; in our case, CCTV Videos or pictures are often full of noise.
  • Invokes the ToGrayScale filter, which turns every pixel into a shade of grey (this may improve speed).
  • Reads text from an OCRInput object via the Read method, which returns an OCRResult object. OCRInput is the preferred input type because it allows for OCR of multi-paged documents, and allows images to be enhanced prior to reading to obtain faster, more accurate results.
  • Result.Text returns the entire content extracted from the license plate.

We can see that the correct license number is extracted from the detected license plate.

How to Perform OCR on License Plates, Figure 6: Results of OCR Processing on the License Plate Image

IronOCR correctly detects and extracts the numbers from the license plate image

Scanning License Plate Numbers from a Car

Let's suppose, we don't have separate license plates, we have a picture of the complete car. Now, we need to scan the area of the number plate and perform text recognition.

We can use System.Drawing.Rectangle to specify a region in which we will read a license plate. The unit of measurement is always pixels.

We will use the following example image file.

detected license plates

How to Perform OCR on License Plates, Figure 7: Detected License Plates

Full, rearview image of a car with visible license plate

We will see that this provides speed improvements as well as avoiding reading unnecessary text.

var Ocr = new IronTesseract();
using (var Input = new OcrInput())
{
    // a 41% improvement on speed
    var ContentArea = new CropRectangle(x: 365, y: 240, height: 80, width: 29);
    Input.AddImage(@"D:\Liscence Plate\plate1.jpg", ContentArea);
    var Result = Ocr.Read(Input);
    Console.WriteLine(Result.Text);
}
var Ocr = new IronTesseract();
using (var Input = new OcrInput())
{
    // a 41% improvement on speed
    var ContentArea = new CropRectangle(x: 365, y: 240, height: 80, width: 29);
    Input.AddImage(@"D:\Liscence Plate\plate1.jpg", ContentArea);
    var Result = Ocr.Read(Input);
    Console.WriteLine(Result.Text);
}
Dim Ocr = New IronTesseract()
Using Input = New OcrInput()
	' a 41% improvement on speed
	Dim ContentArea = New CropRectangle(x:= 365, y:= 240, height:= 80, width:= 29)
	Input.AddImage("D:\Liscence Plate\plate1.jpg", ContentArea)
	Dim Result = Ocr.Read(Input)
	Console.WriteLine(Result.Text)
End Using
VB   C#

This yields a 41% speed increase - and allows us to be specific. We specify the starting coordinates for the region (x and y) and the width and height of the detection region.

ContentAreas (OCR cropping) is also supported when reading PDFs.

Automatic License Plate Recognition

IronOCR can automatically detect the regions in an image where text exists using its text detection model.

To use this model in our application, we will need to supplement the IronOcr package with additional assemblies.

Install IronOcr.ComputerVision.Windows package with the following NuGet Package Console command:

PM> Install-Package IronOcr.ComputerVision.Windows

Next, the following sample code will automatically detect the license plate from the car.

var ocr = new IronTesseract();
using (var input = new OcrInput(@"D:\Liscence Plate\plate1.jpg"))
{
    input.FindTextRegion();
    OcrResult result = ocr.Read(input);
    Console.WriteLine(result.Text);
}
var ocr = new IronTesseract();
using (var input = new OcrInput(@"D:\Liscence Plate\plate1.jpg"))
{
    input.FindTextRegion();
    OcrResult result = ocr.Read(input);
    Console.WriteLine(result.Text);
}
Dim ocr = New IronTesseract()
Using input = New OcrInput("D:\Liscence Plate\plate1.jpg")
	input.FindTextRegion()
	Dim result As OcrResult = ocr.Read(input)
	Console.WriteLine(result.Text)
End Using
VB   C#

Usage of FindTextRegion will use Computer Vision to detect regions that contain license plates on every image of an OcrInput object.

Summary

In this tutorial, we learned to use IronOCR to develop a simple program that reads cars' license plate numbers.

We also seen how easy IronOCR makes it for us to perform text detection activities. In summary, IronOCR provides:

  • OCR for C# to scan and read images and PDFs
  • .NET OCR library with 150+ global language packs
  • Output as text, structured data, or searchable PDFs
  • Supports .NET Core versions 5, 6 & 7 (along with .NET Standard and .NET Framework)

IronOCR is part of the Iron Suite. This suite contains other very useful libraries such as IronPDF for reading and writing PDFs, IronXL for manipulating Excel files, and IronWebScrapper for extracting data from websites. You can purchase Iron Suite for the cost of two library licenses.