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 the license plate number. We will develop a C# and .NET program to perform automatic license plate recognition. This tutorial is simple and easy so is perfect even for a beginner C# programmer.

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. It can be very difficult to find a library that is free, efficient, easy to use, and provides accurate results. I found and used IronOCR; it's free for development, provides a 1 month free trial for commercial use, is easy to use, is efficient, provides multi-threading, supports 150+ languages, and on top of all this also provides better accuracy. It will perform all tasks from license plate detection to getting license plate numbers.

IronOCR

IronOCR is a library developed and maintained by Iron Software that helps C# Software Engineers to perform OCR, Barcode Scanning, and Text Extraction in .NET projects.

Features of IronOCR

  • Reading text from many formats such as images (JPEG, PNG, GIFF, 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.

Let's develop a demo application to read license plate numbers.

Create a Visual Studio Project

The first step is to create a Visual Studio project.

Open Visual Studio. Click on "Create New Project", and select Project Template (I have selected the Console Application template for this demo application, but you can select any as per your requirement or preference). Click on the "Next" button, and name the project (I have named it "License plate OCR", but you can name it anything). Click on the "Next" button, and select your target .NET Framework. Finally, click on the "Create" button to create the project. The project will be created as shown below.

How to OCR License Plate in C# (Tutorial), Figure 1: Creating a Visual Studio Project for our License Plate OCR Project Creating a Visual Studio Project for our License Plate OCR Project Now, we need to install the IronOCR library to use it in our project. The easiest way is to install it via the NuGet Package Manager.

Install IronOCR NuGet Package

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

How to OCR License Plate in C# (Tutorial), Figure 2: Locating the NuGet Package Manager UI in Visual Studio Locating the NuGet Package Manager UI in Visual Studio

The following window will appear.

How to OCR License Plate in C# (Tutorial), Figure 3: The NuGet Package Manager UI in Visual Studio 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 OCR License Plate in C# (Tutorial), Figure 4: Install the IronOCR library in NuGet Package Manager UI Install the IronOCR library in NuGet Package Manager UI

The IronOCR Library will be installed and ready to use.

Read License Plates using Optical Character Recognition

Let's write a program to read the license plate. IronOCR is a Tesseract OCR Engine that is used to extract text from an image, so if we need to implement computer vision to detect vehicles, we may need to use some other machine-learning libraries. IronOCR supports OpenCV, which is an open-source and market-leading detection model. IronOCR can be used with OpenCV to perform object detection such as car detection or detect license plates.

We will read the following number plate:

How to OCR License Plate in C# (Tutorial), Figure 5: Image of a License Plate with license plate numbers clearly visible Image of a License Plate with license plate numbers clearly visible

Now, we'll write the code.

Add the following namespace:

using IronOcr;
using IronOcr;
Imports IronOcr
VB   C#

Then add the following 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 before 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 OCR License Plate in C# (Tutorial), Figure 6: IronOCR correctly detects and extracts the numbers from the license plate image IronOCR correctly detects and extracts the numbers from the license plate image

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.

Scanning License Plate Numbers from a Car

We can use a 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.

How to OCR License Plate in C# (Tutorial), Figure 7: Full, rearview image of a car with visible license plate 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 utilizes OpenCV to use Computer Vision to detect areas where text exists in an image. It performs image processing techniques and uses a detection model for detecting license plates. The use of computer vision in IronOCR will determine where text regions exist and then use Tesseract to attempt to read those regions.

We need to install IronOcr.ComputerVision.Windows for using license plate detection model.

Use the following command in Package Manager Console.

PM> Install-Package IronOcr.ComputerVision.Windows

This will provide the necessary assemblies to use IronOCR Computer Vision with our model file.

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 have learned to use IronOCR and developed a straightforward program to read a car's license plate. We have seen that IronOCR enables us to read text from blurred or low-resolution images, is efficient, provides high accuracy, supports 127+ languages with full accuracy, is free for development, and has no restriction on production.

In summary, IronOCR provides:

  • OCR for C# to scan and read images and PDFs
  • .NET OCR library with 127+ global language packs
  • Output as text, structured data, or searchable PDFs
  • Supports .NET 6, 5, Core, Standard, 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.