如何使用IronOCR在 C# 進行車牌 OCR辨識與擷取圖片文字
IronOCR 的 ReadLicensePlate 方法使用先進的 OCR辨識技術,自動從車輛影像中擷取圖片文字與車牌號碼。 此單一方法呼叫可以高精度地處理車牌,為自動車輛管理系統返回車牌文字和置信度分數。
在管理大量車輛影像時,手動讀取車牌既費時又容易出錯。 使用IronOCR等工具實現此流程的自動化,可以提供更有效率、更準確的解決方案。 IronOCR 的 ReadLicensePlate 方法可以透過程式設計從影像中提取車牌號碼,從而節省大量時間並提高資料準確性。
在本指南中,我們將示範如何使用IronOCR進行車牌識別,透過範例和可自訂的配置,使整個過程無縫銜接。 透過利用這些方法,開發人員可以自動讀取車牌號碼,從而提高停車管理、收費或安全監控等任務的效率。
若要使用此功能,您還必須安裝IronOcr.Extension.AdvancedScan軟體套件。
快速入門:立即提取車牌號碼
使用 IronOCR 的 ReadLicensePlate 的單一方法調用,即可透過程式設計方式從任何圖像中提取車牌文字。 它已準備就緒——只需加載圖像,調用該方法,即可立即獲取車牌號碼和置信度。
最簡工作流程(5個步驟)
- 下載用於讀取車牌的 C# 庫
- 導入車牌影像進行處理
- 請確保文件中僅包含車牌圖像,不包含頁首或頁尾。
- 使用`ReadLicensePlate`方法從圖像中提取數據
- 存取**`OcrLicensePlateResult`**屬性以查看和操作提取的許可證數據
如何在C#中讀取車牌號碼?
要在IronOCR中讀取車牌,我們執行以下步驟:
- 我們使用
ReadLicensePlate方法,該方法接受OcrInput作為輸入參數。 此方法比庫中的標準Read對應方法更適合車牌辨識。 - (可選)我們可以設定IronOCR ,將車牌中可能存在的特定字元列入白名單,以加快車牌號碼的處理速度。
- 目前此方法僅適用於英文、中文、日文、韓文和拉丁字母文字。
- 在.NET Framework上使用進階掃描功能需要專案在 x64 架構上運作。
輸入的車牌號碼是什麼樣的?

如何配置車牌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)
我應該期待什麼樣的結果?

程式碼示範如何將圖像作為 OcrInput 導入,並將其與 ReadLicensePlate 方法一起使用,以從車牌中提取文字。 輸出結果顯示了與輸入圖像中顯示的車牌匹配的提取文本,以及指示 OCR 準確性的置信度。
文字:從 OCR 輸入中提取的文字。
置信度:一個 double 屬性,表示每個字元平均值的統計準確度置信度,1 為最高,0 為最低。
為了更精確地控制 OCR 過程,您可以探索進階配置選項來微調字元辨識設定。
如何從汽車圖片中擷取車牌號碼?
此方法對於包含車牌的汽車圖像也同樣有效。 程式碼與上面的程式碼相同,只是輸入圖像改變了。 您也可以提取車牌所在區域的像素座標。
哪種類型的汽車圖片效果最好?

為獲得最佳效果,請確保您的汽車圖片包含以下要素: 車牌清晰可見
- 良好的光線條件(避免眩光或陰影)
- 最小角度畸變
- 足夠的解析度(對於低解析度影像,請考慮調整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)
結果包含哪些資訊?

此範例展示如何將 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
對於大規模處理,請考慮實現 多執行緒功能以最大限度地提高效能。
如何提高車牌辨識準確率?
為了提高車牌辨識的準確率,可以考慮以下優化技術:
應用影像預處理濾波器
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
應對不同的光照條件
對於光照條件複雜的情況,請進行對應的校正:
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
如何監控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
如需詳細的效能監控,請探索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 結果的可信度。

