如何使用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方法来读取车牌号,该方法利用先进的OCR技术处理车辆图像,并提供车牌文本和置信度分数。
使用IronOCR进行车牌识别需要哪些条件?
要使用IronOCR进行车牌识别,您需要安装IronOCR库及其IronOcr.Extensions.AdvancedScan包,然后使用ReadLicensePlate方法从图像中提取车牌号码。
使用IronOCR进行车牌识别有什么好处?
使用IronOCR进行车牌识别既高效又准确,与手动处理相比节省时间。它通过提供高精度的文本提取来自动化停车管理和收费等任务。
如何优化IronOCR以提高车牌识别效果?
您可以通过配置字符白名单以匹配典型车牌字符,并使用图像预处理过滤器如Deskew、DeNoise和EnhanceResolution来提高输入图像质量,以优化IronOCR。
IronOCR在车牌识别中支持哪些语言?
IronOCR的车牌识别目前支持英语、中文、日语、韩语和拉丁字母脚本。
IronOCR能否在批量处理时处理多个车牌图像?
是的,IronOCR可以通过配置ReadLicensePlate方法以优化性能,在批量操作中异步处理每个图像。
如何使用IronOCR检索车牌位置坐标?
使用IronOCR,您可以通过访问OcrLicensePlateResult的Licenseplate属性检索车牌位置坐标,该属性提供图片中车牌区域的像素坐标和尺寸。
使用IronOCR进行车牌识别的常见应用场景有哪些?
常见的应用场景包括停车管理、收费、安保监控、车队管理和执法等 - 这些都受益于IronOCR的高精度和实时处理能力。
什么类型的图像为IronOCR提供最佳效果?
当使用IronOCR进行车牌识别时,车牌清晰可见、光照条件良好、角度变形小且分辨率足够的图像能提供最佳效果。
如何在车牌识别过程中监控IronOCR的性能?
您可以通过订阅其OcrProgress事件,从而在处理大批量车牌图像时获取实时进度信息来监控IronOCR的性能。
Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。