如何在 C# OCR 中閱讀車牌

如何使用 IronOCR 用 C# 讀取車牌。

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronOCR 的 ReadLicensePlate 方法使用先進的 OCR 技術自動從車輛影像中擷取車牌號碼。 這個單一的方法呼叫可以高準確度處理車牌,為自動化車輛管理系統傳回車牌文字和信心分數。

在管理大量車輛影像時,手動讀取車牌既費時又容易出錯。 使用 IronOCR 等工具實現此流程的自動化,可以提供更有效率、更準確的解決方案。 IronOCR 的 ReadLicensePlate 方法可以程式化地從影像中擷取車牌號碼,節省大量時間,同時提高資料精確度。

在本指南中,我們將示範如何使用 IronOCR 進行車牌辨識,並參考範例和可自訂的配置,讓整個過程天衣無縫。 透過利用這些方法,開發人員可以自動讀取車牌號碼,從而提高停車管理、收費或安全監控等任務的效率。

若要使用此功能,您還必須安裝IronOcr.Extension.AdvancedScan軟體套件。

快速入門:立即提取車牌號碼

只要使用 IronOCR 的 ReadLicensePlate 來呼叫一個方法,您就可以程式化地從任何影像中擷取車牌文字。 此軟體可立即使用 - 只需載入圖片、呼叫方法,即可立即取得車牌號碼和信心。

Nuget Icon立即開始使用 NuGet 建立 PDF 檔案:

  1. 使用 NuGet 套件管理器安裝 IronOCR

    PM > Install-Package IronOcr

  2. 複製並運行這段程式碼。

    OcrLicensePlateResult result = new IronTesseract().ReadLicensePlate(new OcrInput("plate.jpg"));
  3. 部署到您的生產環境進行測試

    立即開始在您的專案中使用 IronOCR,免費試用!
    arrow pointer

如何用 C# 閱讀車牌?

要在 IronOCR 中讀取車牌,我們應用以下步驟:

  • 我們利用 ReadLicensePlate 方法,該方法將 OcrInput 作為輸入的參數。 相較於函式庫的標準 Read 對應方式,此方法更能優化車牌。
  • 我們可以選擇設定 IronOCR,將車牌中可能存在的特定字元列入白名單,以加快車牌號碼的處理速度。

請注意@

  • 此方法目前僅適用於英文、中文、日文、韓文和拉丁字母文字。
  • 在 .NET Framework 上使用進階掃描功能需要專案在 x64 架構上運作。

輸入車牌長什麼樣子?

加州車牌顯示 '8TRS777' - 車牌讀取示範範例

如何設定車牌的 OCR?

:path=/static-assets/ocr/content-code-examples/how-to/read-license-plate-read-license-plate.cs
using IronOcr;
using System;

var ocr = new IronTesseract();
ocr.Configuration.WhiteListCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";

using var inputLicensePlate = new OcrInput();
inputLicensePlate.LoadImage("plate.jpeg");

// Read license plate
OcrLicensePlateResult result = ocr.ReadLicensePlate(inputLicensePlate);

// Retrieve license plate number and confidence value
string output = $"{result.Text}\nResult Confidence: {result.Confidence}";

Console.WriteLine(output);
Imports Microsoft.VisualBasic
Imports IronOcr
Imports System

Private ocr = New IronTesseract()
ocr.Configuration.WhiteListCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"

Dim inputLicensePlate = New OcrInput()
inputLicensePlate.LoadImage("plate.jpeg")

' Read license plate
Dim result As OcrLicensePlateResult = ocr.ReadLicensePlate(inputLicensePlate)

' Retrieve license plate number and confidence value
Dim output As String = $"{result.Text}" & vbLf & "Result Confidence: {result.Confidence}"

Console.WriteLine(output)
$vbLabelText   $csharpLabel

我應該期待什麼結果?

Debug 控制台顯示車牌 OCR 結果:加州,車牌號碼 8TRS777,置信度 95.87%

程式碼示範如何將圖像作為OcrInput匯入,並將其與ReadLicensePlate方法一起使用,以從車牌中提取文字。 輸出結果顯示了與輸入圖像中顯示的車牌匹配的提取文本,以及指示 OCR 準確性的置信度。

文字:從 OCR 輸入中提取的文字。

信心:一個 雙數屬性,表示每個字元平均值的統計準確度信賴度,1 代表最高,0 代表最低。

若要更精確地控制 OCR 過程,您可以探索 進階組態選項 來微調字元識別設定。


如何從汽車影像中萃取車牌?

此方法對於包含車牌的汽車圖像也同樣有效。 代碼與上面的相同,輸入的圖片有所改變。 您也可以提取車牌所在區域的像素座標。

哪種類型的汽車圖片效果最佳?

深灰色的 Skoda 汽車,顯示 Maharashtra 車牌 MH 20 EE 7602,停在磚造的人行道上

為了達到最佳效果,請確保您的汽車圖片具有:

  • 清晰可見的車牌
  • 良好的照明條件(避免眩光或陰影)
  • 最小化角度扭曲
  • 足夠的解析度(可考慮調整 DPI 設定,以適應低解析度的影像)

如何取得車牌位置座標?

:path=/static-assets/ocr/content-code-examples/how-to/read-license-plate-read-from-car.cs
using IronOcr;
using IronSoftware.Drawing;
using System;

var ocr = new IronTesseract();
using var inputLicensePlate = new OcrInput();
inputLicensePlate.LoadImage("car_license.jpg");

// Read license plate
OcrLicensePlateResult result = ocr.ReadLicensePlate(inputLicensePlate);

// Retrieve license plate coordinates
RectangleF rectangle = result.Licenseplate;

// Write license plate value and coordinates in a string
string output = $"License Plate Number:\n{result.Text}\n\n"
              + $"License Plate Area_\n"
              + $"Starting X: {rectangle.X}\n"
              + $"Starting Y: {rectangle.Y}\n"
              + $"Width: {rectangle.Width}\n"
              + $"Height: {rectangle.Height}";

Console.WriteLine(output);
Imports Microsoft.VisualBasic
Imports IronOcr
Imports IronSoftware.Drawing
Imports System

Private ocr = New IronTesseract()
Private inputLicensePlate = New OcrInput()
inputLicensePlate.LoadImage("car_license.jpg")

' Read license plate
Dim result As OcrLicensePlateResult = ocr.ReadLicensePlate(inputLicensePlate)

' Retrieve license plate coordinates
Dim rectangle As RectangleF = result.Licenseplate

' Write license plate value and coordinates in a string
Dim output As String = $"License Plate Number:" & vbLf & "{result.Text}" & vbLf & vbLf & $"License Plate Area_" & vbLf & $"Starting X: {rectangle.X}" & vbLf & $"Starting Y: {rectangle.Y}" & vbLf & $"Width: {rectangle.Width}" & vbLf & $"Height: {rectangle.Height}"

Console.WriteLine(output)
$vbLabelText   $csharpLabel

結果包含哪些資訊?

Visual Studio 除錯輸出會顯示偵測到的車牌 MH20EE7602 與邊界方塊座標和尺寸

此範例展示如何將ReadLicensePlate方法應用於汽車影像。 此方法也會傳回車牌在影像中所在的矩形座標。

此方法經過優化,僅用於尋找單一車牌,並且能夠在圖庫圖片中搜尋車牌。

如何處理多個車牌?

在處理多個車輛影像時,您可以使用批次作業有效率地處理它們:

using IronOcr;
using System.IO;
using System.Threading.Tasks;

public async Task ProcessMultipleLicensePlates(string[] imagePaths)
{
    var ocr = new IronTesseract();

    // Configure for optimal performance
    ocr.Configuration.WhiteListCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    ocr.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.SingleBlock;

    var tasks = imagePaths.Select(async path => 
    {
        using var input = new OcrInput();
        input.LoadImage(path);

        var result = await Task.Run(() => ocr.ReadLicensePlate(input));

        return new { 
            FilePath = path, 
            PlateNumber = result.Text, 
            Confidence = result.Confidence 
        };
    });

    var results = await Task.WhenAll(tasks);

    // Process results
    foreach (var result in results)
    {
        Console.WriteLine($"File: {result.FilePath}");
        Console.WriteLine($"Plate: {result.PlateNumber} (Confidence: {result.Confidence:P})");
    }
}
using IronOcr;
using System.IO;
using System.Threading.Tasks;

public async Task ProcessMultipleLicensePlates(string[] imagePaths)
{
    var ocr = new IronTesseract();

    // Configure for optimal performance
    ocr.Configuration.WhiteListCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    ocr.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.SingleBlock;

    var tasks = imagePaths.Select(async path => 
    {
        using var input = new OcrInput();
        input.LoadImage(path);

        var result = await Task.Run(() => ocr.ReadLicensePlate(input));

        return new { 
            FilePath = path, 
            PlateNumber = result.Text, 
            Confidence = result.Confidence 
        };
    });

    var results = await Task.WhenAll(tasks);

    // Process results
    foreach (var result in results)
    {
        Console.WriteLine($"File: {result.FilePath}");
        Console.WriteLine($"Plate: {result.PlateNumber} (Confidence: {result.Confidence:P})");
    }
}
Imports IronOcr
Imports System.IO
Imports System.Threading.Tasks

Public Async Function ProcessMultipleLicensePlates(imagePaths As String()) As Task
    Dim ocr As New IronTesseract()

    ' Configure for optimal performance
    ocr.Configuration.WhiteListCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
    ocr.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.SingleBlock

    Dim tasks = imagePaths.Select(Async Function(path)
                                      Using input As New OcrInput()
                                          input.LoadImage(path)

                                          Dim result = Await Task.Run(Function() ocr.ReadLicensePlate(input))

                                          Return New With {
                                              .FilePath = path,
                                              .PlateNumber = result.Text,
                                              .Confidence = result.Confidence
                                          }
                                      End Using
                                  End Function)

    Dim results = Await Task.WhenAll(tasks)

    ' Process results
    For Each result In results
        Console.WriteLine($"File: {result.FilePath}")
        Console.WriteLine($"Plate: {result.PlateNumber} (Confidence: {result.Confidence:P})")
    Next
End Function
$vbLabelText   $csharpLabel

若要進行大規模處理,請考慮實施 多執行緒功能,以將效能發揮到極致。

如何提高車牌辨識準確度?

若要提高車牌偵測的精確度,請考慮這些最佳化技巧:

應用影像預處理濾鏡

using IronOcr;

var ocr = new IronTesseract();
using var input = new OcrInput();

// Load and preprocess the image
input.LoadImage("blurry_plate.jpg");
input.Deskew();  // Correct image rotation
input.DeNoise(); // Remove background noise
input.EnhanceResolution(225); // Upscale for better clarity
input.Sharpen(); // Enhance edge definition

var result = ocr.ReadLicensePlate(input);
using IronOcr;

var ocr = new IronTesseract();
using var input = new OcrInput();

// Load and preprocess the image
input.LoadImage("blurry_plate.jpg");
input.Deskew();  // Correct image rotation
input.DeNoise(); // Remove background noise
input.EnhanceResolution(225); // Upscale for better clarity
input.Sharpen(); // Enhance edge definition

var result = ocr.ReadLicensePlate(input);
Imports IronOcr

Dim ocr As New IronTesseract()
Using input As New OcrInput()
    ' Load and preprocess the image
    input.LoadImage("blurry_plate.jpg")
    input.Deskew() ' Correct image rotation
    input.DeNoise() ' Remove background noise
    input.EnhanceResolution(225) ' Upscale for better clarity
    input.Sharpen() ' Enhance edge definition

    Dim result = ocr.ReadLicensePlate(input)
End Using
$vbLabelText   $csharpLabel

進一步瞭解可用的 影像濾鏡 影像修正技術,以最佳化輸入的影像。

處理不同的照明條件

對於具有挑戰性的照明場景,應用適當的修正:

using IronOcr;

var ocr = new IronTesseract();
using var input = new OcrInput();

input.LoadImage("dark_plate.jpg");
input.Contrast(1.5); // Increase contrast
input.Brightness(1.2); // Adjust brightness
input.Binarize(); // Convert to black and white for clarity

var result = ocr.ReadLicensePlate(input);
using IronOcr;

var ocr = new IronTesseract();
using var input = new OcrInput();

input.LoadImage("dark_plate.jpg");
input.Contrast(1.5); // Increase contrast
input.Brightness(1.2); // Adjust brightness
input.Binarize(); // Convert to black and white for clarity

var result = ocr.ReadLicensePlate(input);
Imports IronOcr

Dim ocr As New IronTesseract()
Using input As New OcrInput()
    input.LoadImage("dark_plate.jpg")
    input.Contrast(1.5) ' Increase contrast
    input.Brightness(1.2) ' Adjust brightness
    input.Binarize() ' Convert to black and white for clarity

    Dim result = ocr.ReadLicensePlate(input)
End Using
$vbLabelText   $csharpLabel

如何監控 OCR 效能?

在處理大量車牌時,追蹤進度有助於管理系統資源:

using IronOcr;

var ocr = new IronTesseract();

// Subscribe to progress events
ocr.OcrProgress += (sender, e) => 
{
    Console.WriteLine($"Processing: {e.ProgressPercent}% complete");
};

using var input = new OcrInput();
input.LoadImage("large_parking_lot.jpg");

var result = ocr.ReadLicensePlate(input);
using IronOcr;

var ocr = new IronTesseract();

// Subscribe to progress events
ocr.OcrProgress += (sender, e) => 
{
    Console.WriteLine($"Processing: {e.ProgressPercent}% complete");
};

using var input = new OcrInput();
input.LoadImage("large_parking_lot.jpg");

var result = ocr.ReadLicensePlate(input);
Imports IronOcr

Dim ocr As New IronTesseract()

' Subscribe to progress events
AddHandler ocr.OcrProgress, Sub(sender, e)
                                Console.WriteLine($"Processing: {e.ProgressPercent}% complete")
                            End Sub

Using input As New OcrInput()
    input.LoadImage("large_parking_lot.jpg")

    Dim result = ocr.ReadLicensePlate(input)
End Using
$vbLabelText   $csharpLabel

如需詳細的效能監控,請探索 IronOCR 中的 進度追蹤功能

與其他文件閱讀比較如何?

IronOCR 的專業文件閱讀能力不只限於車牌。 相同的 電腦視覺技術可應用於車牌偵測:

哪些是常見的使用案例?

使用 IronOCR 進行車牌辨識可實現各種應用程式:

  • 停車管理:自動化進出記錄與付款處理
  • 收費:加快收費站的車輛識別速度
  • 安全監控:追蹤禁區內的車輛行蹤
  • 車隊管理:監控公司車輛和物流
  • 執法:快速識別相關車輛

每個使用個案都受益於 IronOCR 的高準確度和即時處理影像的能力,使其同時適用於批次處理和即時應用程式。

常見問題解答

如何用 C# 從影像讀取車牌?

您可以使用 IronOCR 的 ReadLicensePlate 方法在 C# 中讀取車牌。只需建立一個 IronTesseract 範例,並使用包含車牌影像的 OcrInput 來呼叫 ReadLicensePlate。該方法會傳回一個 OcrLicensePlateResult,其中包含擷取的車牌文字和置信度分數。

車牌辨識需要安裝什麼套件?

要使用 IronOCR 中的車牌辨識功能,您需要從 NuGet 安裝 IronOCR 主套件和 IronOcr.Extension.AdvancedScan 套件。AdvancedScan 延伸套件提供了專門的 ReadLicensePlate 方法。

只需一行代碼就能提取車牌嗎?

是的,IronOCR 只需一行代碼即可讓您提取車牌文字:OcrLicensePlateResult result = new IronTesseract().ReadLicensePlate(new OcrInput("plate.jpg")); 這會立即回傳車牌號碼和置信度分數。

車牌辨識支援哪些語言?

IronOCR 的 ReadLicensePlate 方法目前支援英文、中文、日文、韓文和拉丁字母文字的車牌。本方法已針對這些字元集進行最佳化。

如何提高車牌讀取的精確度?

您可以透過下列方式提高 IronOCR 的精確度:將車牌中出現的特定字元列入白名單、確保影像只包含車牌而不包含車頭或車尾,以及使用高品質的影像。ReadLicensePlate 方法已針對車牌辨識進行了最佳化。

在 .NET Framework 上進行車牌辨識的系統需求為何?

在 .NET Framework 上使用 IronOCR 的進階掃描功能(包括 ReadLicensePlate)時,您的專案必須在 x64 架構上執行。此要求可確保車牌識別功能的最佳效能。

車牌辨識會回傳哪些資訊?

IronOCR 的 ReadLicensePlate 方法會返回一個 OcrLicensePlateResult 物件,其中包含擷取的車牌文字和置信度分數。這可讓您擷取車牌號碼,並評估 OCR 結果的可信度。

Curtis Chau
技術撰稿人

Curtis Chau 擁有電腦科學學士學位(卡爾頓大學),專長於前端開發,精通 Node.js、TypeScript、JavaScript 和 React。Curtis 對製作直覺且美觀的使用者介面充滿熱情,他喜歡使用現代化的架構,並製作結構良好且視覺上吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 也有濃厚的興趣,他喜歡探索整合硬體與軟體的創新方式。在空閒時間,他喜歡玩遊戲和建立 Discord bots,將他對技術的熱愛與創意結合。

準備好開始了嗎?
Nuget 下載 5,384,824 | 版本: 2026.2 剛剛發布