如何使用IronOCR在C#中讀取車牌
IronOCR 的 ReadLicensePlate 方法利用先進的 OCR 技術,自動從車輛影像中提取車牌號碼。 這個單一的方法呼叫可以高準確度處理車牌,為自動化車輛管理系統傳回車牌文字和信心分數。
在管理大量車輛影像時,手動讀取車牌既費時又容易出錯。 使用 IronOCR 等工具實現此流程的自動化,可以提供更有效率、更準確的解決方案。 IronOCR 的 ReadLicensePlate 方法可以程式化地從影像中提取車牌號碼,節省大量時間並提高數據準確性。
在本指南中,我們將展示如何使用 IronOCR 進行車牌識別,透過範例和可自定義的配置來使流程更順暢。 透過利用這些方法,開發人員可以自動讀取車牌號碼,從而提高停車管理、收費或安全監控等任務的效率。
要使用此功能,您還必須安裝 IronOcr.Extensions.AdvancedScan 套件。
使用 IronOCR 的 ReadLicensePlate 單一方法呼叫,您可以程式化地從任何影像中提取車牌文字。 已準備就緒 - 只需載入圖像,呼叫方法,即可立即獲得車牌號碼和置信度。
-
1使用 IronOCR 安裝 NuGet Package Manager
-
2複製並運行這段程式碼。
var input = new OcrInput(); input.LoadImage("plate.jpg"); OcrLicensePlateResult result = new IronTesseract().ReadLicensePlate(input);Dim input = New OcrInput() input.LoadImage("plate.jpg") Dim result As OcrLicensePlateResult = New IronTesseract().ReadLicensePlate(input)Dim input = New OcrInput() input.LoadImage("plate.jpg") Dim result As OcrLicensePlateResult = New IronTesseract().ReadLicensePlate(input) -
3部署到您的生產環境進行測試
立即在您的項目中開始使用 IronOCR,並使用 免費試用
最小工作流程(5 個步驟)
- 下載 C# 程式庫以讀取車牌
- 導入車牌圖像以進行處理
- 確保文件僅包含車牌圖像,沒有標頭或頁腳
- 使用
ReadLicensePlate方法從圖像中提取資料 - 存取OcrLicensePlateResult屬性以查看和操作提取的許可證資料
如何在 C# 中讀取車牌?
要在 IronOCR 中讀取車牌,我們應用以下步驟:
- 我們使用
ReadLicensePlate方法,以OcrInput作為輸入參數。 此方法比程式庫的標準Read相對於車牌更優化。 - 我們可以選擇設定 IronOCR,將車牌中可能存在的特定字元列入白名單,以加快車牌號碼的處理速度。
輸入的車牌長什麼樣?

我如何配置車牌的 OCR?
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);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)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 設置)
我如何獲得車牌位置坐標?
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);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)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 = New IronTesseract()
' Configure for optimal performance
ocr.Configuration.WhiteListCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
ocr.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.SingleBlock
Dim tasks = imagePaths.Select(Async Function(path)
Using input = New OcrInput()
input.LoadImage(path)
Dim result = Await Task.Run(Function() ocr.ReadLicensePlate(input))
Return New With {
Key .FilePath = path,
Key .PlateNumber = result.Text,
Key .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 FunctionImports IronOcr
Imports System.IO
Imports System.Threading.Tasks
Public Async Function ProcessMultipleLicensePlates(imagePaths As String()) As Task
Dim ocr = New IronTesseract()
' Configure for optimal performance
ocr.Configuration.WhiteListCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
ocr.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.SingleBlock
Dim tasks = imagePaths.Select(Async Function(path)
Using input = New OcrInput()
input.LoadImage(path)
Dim result = Await Task.Run(Function() ocr.ReadLicensePlate(input))
Return New With {
Key .FilePath = path,
Key .PlateNumber = result.Text,
Key .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 UsingImports 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.5f); // Increase contrast
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.5f); // 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.5F) ' Increase contrast
input.Binarize() ' Convert to black and white for clarity
Dim result = ocr.ReadLicensePlate(input)
End UsingImports IronOcr
Dim ocr As New IronTesseract()
Using input As New OcrInput()
input.LoadImage("dark_plate.jpg")
input.Contrast(1.5F) ' Increase contrast
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 UsingImports 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#讀取車牌,此方法將使用先進的OCR技術處理車輛圖像,並提供車牌文字和信心水準。
使用IronOCR進行車牌識別需要什麼?
要使用IronOCR進行車牌識別,您需要安裝IronOCR程式庫及其IronOcr.Extensions.AdvancedScan套件,然後使用ReadLicensePlate方法來從圖像中擷取車牌號碼。
使用IronOCR進行車牌識別的好處是什麼?
使用IronOCR進行車牌識別既高效又準確,相比手動處理能節省時間。它透過在文本提取中提供高准確性來自動化例如停車管理和收費站收費等工作。
如何優化IronOCR以更好地識別車牌?
您可以通過配置字符白名單以匹配典型的車牌字符來優化IronOCR,並利用例如Deskew、DeNoise和EnhanceResolution等圖像預處理篩選器來提升輸入圖像的質量。
IronOCR支援哪些語言的車牌識別?
IronOCR的車牌識別目前支援英語、中文、日語、韓語和拉丁字母。
IronOCR可以批量處理多個車牌圖像嗎?
可以,IronOCR可以通過配置ReadLicensePlate方法以達到最佳效能來批量處理多個車輛圖像,使批量操作能夠異步處理每個圖像。
我如何使用IronOCR檢索車牌位置坐標?
使用IronOCR時,您可以透過訪問OcrLicensePlateResult的Licenseplate屬性來檢索車牌位置坐標,此屬性提供圖像中車牌區域的像素坐標和尺寸。
使用IronOCR進行車牌識別的常見使用案例有哪些?
常見的使用案例包括停車管理、收費站收費、安全監控、車隊管理和執法,這些都受益於IronOCR的高准確性和即時處理能力。
哪類型的圖像能為IronOCR提供最佳結果?
當使用IronOCR進行車牌識別時,顯示車牌清晰、光照條件良好、角度失真最小且解析度適中的圖像能提供最佳結果。
我如何監控IronOCR在車牌識別過程中的性能?
您可以透過訂閱OcrProgress事件來監控IronOCR的性能,該事件在處理大批量車牌圖像過程中提供即時進程信息。
Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。