如何在C# OCR中讀取車牌

如何在C#中使用IronOCR讀取車牌

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只需一個方法調用,就可以程式化地從任何圖像中提取車牌文字。 它已經可以使用——只需載入圖像、調用方法,即刻獲取車牌號碼和信心值。

  1. 使用NuGet套件管理器安裝https://www.nuget.org/packages/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

應該期望什麼結果?

除錯控制台顯示車牌OCR結果:加利福尼亞州,車牌8TRS777,自信度95.87%

程式碼示範如何將一個圖像導入為ReadLicensePlate方法從車牌中提取文字。 輸出顯示與輸入圖像中車牌相匹配的提取文字,以及顯示OCR準確性的信心水準。

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

信心:一個double屬性,指示每個字元平均的統計準確性信心水準,1為最高,0為最低。

要對OCR過程進行更精確的控制,您可以探索高級配置選項來微調字元識別設定。


如何從汽車圖像中提取車牌?

該方法對於包含車牌的汽車圖像也表現良好。 程式碼與上述相同,只改變了輸入圖像。 您還可以提取圖像中車牌所在區域的像素座標。

什麼型別的汽車圖像效果最好?

深灰色斯柯達汽車顯示馬哈拉施特拉州車牌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

對於大規模處理,考慮實現多執行緒能力以最大化性能。

如何提高車牌識別的準確度?

為提高車牌檢測的準確度,考慮以下優化技術:

應用圖像預處理過濾器

:path=/static-assets/ocr/content-code-examples/how-to/read-license-plate-5.cs
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

了解更多可用的圖像過濾器圖像校正技術以優化您的輸入圖像。

處理不同的光照條件

針對挑戰性的照明場景,應用適當的校正:

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

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

input.LoadImage("dark_plate.jpg");
input.Contrast(1.5); // Increase contrast
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.Binarize() ' Convert to black and white for clarity

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

如何監控OCR性能?

當處理大量車牌時,跟踪進度有助於管理系統資源:

:path=/static-assets/ocr/content-code-examples/how-to/read-license-plate-7.cs
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實例並調用ReadLicensePlate,其中包括您的車牌圖像的OcrInput。該方法返回包含提取出的車牌文字和置信度分數的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上的車牌識別系統需求是什麼?

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

車牌識別返回什麼資訊?

IronOCR的ReadLicensePlate方法返回一個OcrLicensePlateResult物件,內含提取出的車牌文字和置信度分數。這使您可以檢索車牌號碼並評估OCR結果的可靠性。

使用IronOCR進行文件管理的好處是什麼?

使用IronOCR進行文件管理通過將掃描的文件轉換為可搜索和可編輯的文字來簡化工作流程,減少手動資料輸入的需求並提高文件的可存取性。

IronOCR如何提高資料精確性?

IronOCR通過其先進的識別算法和影像校正功能提高資料精確性,確保文字提取過程既可靠又精確。

IronOCR有免費試用版嗎?

有的,Iron Software提供IronOCR的免費試用版,允許使用者在做出購買決定前測試其功能和能力。

Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

除了開發,Curtis對物聯網(IoT)有濃厚的興趣,探索創新的方法來整合硬體和軟體。在空閒時間,他喜歡玩遊戲和建立Discord機器人,結合他對技術的熱愛與創造力。

準備開始了嗎?
Nuget 下載 6,136,090 | 版本: 2026.7 剛剛發布
Still Scrolling Icon

還在滾動?

想要快速證明? PM > Install-Package IronOcr
執行範例 觀看您的圖像轉變為可搜尋文字。