使用 IRONOCR

如何在C#中OCR车牌(教程)

Kannaopat Udonpant
坎那帕·乌东攀
2023年二月26日
更新 2024年十一月12日
分享:

介绍

车牌识别已成为许多行业中不可或缺的工具,从交通管理和停车系统到执法和收费解决方案。 通过利用光学字符识别(OCR)技术,开发人员可以高效从图像中提取文本,自动化识别车牌的过程。 在本教程中,我们将演示如何使用IronOCR,一个强大的 C# OCR 库,准确地读取图像中的车牌。 IronOCR通过与OpenCV源码的无缝集成,用于计算机视觉任务,提供了一个强大的解决方案,即使从复杂或噪声较大的图像来源中也能识别文本。 无论您是在处理干净的车牌图像,还是完整的车辆照片,本指南将指导您通过现代光学字符识别(OCR)技术构建可靠的车牌识别系统的步骤。

如何使用车牌识别 C

  1. 安装 C# 库以使用车牌识别功能

  2. 将车牌图像导入到新的OcrImageInput实例中

  3. 在C#中应用图像滤镜以改善文本提取

  4. 通过在照片中指定车牌区域来提高识别速度

  5. 使用 OcrLicensePlateResult 实例打印提取的文本

IronOCR 入门

IronOCR 是一个基于 Tesseract OCR 引擎的 C# OCR 库,专为在 .NET 应用程序中为文本识别项目提供高精度和高效能而设计。 适用于处理噪声或低质量图像,IronOCR包含强大的图像预处理功能,如自动降噪和灰度转换,以增强文本提取的清晰度。

IronOCR 的一些显著功能包括:

  • 高 OCR 准确率:IronOCR 针对多种语言和字体进行了优化, 即使在处理复杂或扭曲文本时也能表现出色。
  • 图像和PDF支持:它可以从多种图像格式和PDF文件中读取文本,这使其对不同的文档类型具有多功能性。
  • 与OpenCV的集成:通过OpenCV支持,IronOCR可以执行计算机视觉任务,例如检测图像中的特定文本区域,这对于车牌识别特别有用。
  • 高级预处理:包括用于灰度转换、旋转、纠偏和对比度增强的滤镜,以提高识别质量。
  • 灵活的输入选项:支持多页文档和可调整区域,使开发者能够将OCR处理集中在选定的区域,以获得更快和更有针对性的结果。

    凭借这些功能,IronOCR 是一个强大的解决方案,适用于构建对准确性、灵活性和与其他计算机视觉工具的易于集成性有要求的 OCR 应用程序。

创建 Visual Studio 项目

首先打开 Visual Studio,并选择“创建新项目”。 这将带您到一个页面,在那里您可以选择要构建的项目类型(在我们的例子中,我们将创建一个控制台应用程序)。 选择所需的应用类型,然后点击“下一步”。

License Plate Ocr Csharp Tutorial 1 related to 创建 Visual Studio 项目

现在,为您的项目命名并选择保存位置。

License Plate Ocr Csharp Tutorial 2 related to 创建 Visual Studio 项目

最后,选择你的目标 .NET 框架,并点击“创建”按钮。 这将创建项目,如下所示。

License Plate Ocr Csharp Tutorial 3 related to 创建 Visual Studio 项目

接下来的步骤是安装IronOCR库,以便我们开始处理车牌。

安装 IronOCR

要在您的C#项目中开始使用IronOCR,您需要从NuGet安装IronOCR包。 IronOCR兼容.NET Framework和.NET Core,使其易于集成到各种.NET应用程序中。

步骤 1:打开包管理器控制台

在 Visual Studio 中,导航到工具 > NuGet 包管理器 > 包管理器控制台

步骤 2:安装 IronOCR 包

在包管理器控制台中输入以下命令:

Install-Package IronOcr
Install-Package IronOcr
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'Install-Package IronOcr
$vbLabelText   $csharpLabel

此命令安装IronOCR库,包括运行项目中OCR功能所需的所有依赖项。 由于我们的应用程序需要使用计算机视觉进行车牌检测等高级功能,您还可以通过以下方式安装可选的IronOcr.ComputerVision.Windows包:

Install-Package IronOcr.ComputerVision.Windows
Install-Package IronOcr.ComputerVision.Windows
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'Install-Package IronOcr.ComputerVision.Windows
$vbLabelText   $csharpLabel

并确保您安装了IronOcr.Extensions.AdvancedScan扩展,以便您可以利用其强大的ReadLicensePlate方法:

Install-Package IronOcr.Extensions.AdvancedScan
Install-Package IronOcr.Extensions.AdvancedScan
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'Install-Package IronOcr.Extensions.AdvancedScan
$vbLabelText   $csharpLabel

另外,您可以通过工具 > NuGet 包管理器 > 为解决方案管理 NuGet 包来安装包,然后搜索您需要的包:

License Plate Ocr Csharp Tutorial 4 related to 步骤 2:安装 IronOCR 包

设置代码

最后,我们必须在代码的顶部添加必要的导入和 using 语句:

using IronOcr;
using IronOcr;
Imports IronOcr
$vbLabelText   $csharpLabel

使用光学字符识别读取车牌号码

在本节中,我们将创建一个程序来使用IronOCR读取车牌,这是一款在从图像中提取文本方面表现出色的Tesseract OCR引擎。 为了实现车辆检测,我们还可以结合其他机器学习库。 值得注意的是,IronOCR 与 OpenCV 集成,这是一个领先的开源计算机视觉库,使我们能够执行物体检测任务,例如识别车辆和车牌。

示例车牌图像

我们将使用以下车牌:

License Plate Ocr Csharp Tutorial 5 related to 示例车牌图像

接下来,添加以下代码对车牌进行OCR:

using IronOcr;

var ocr = new IronTesseract();

using (var input = new OcrImageInput("licensePlate.jpeg"))
{
    // Fixing the digital noise and making the image easier to read
    input.DeNoise();
    input.ToGrayScale();

    // Using the OcrLicensePlateResult and ReadLicensePlate methods to read the license plate information and store it for further use
    OcrLicensePlateResult result = ocr.ReadLicensePlate(input);

    // Saving the license plate text to a string variable
    string output = result.Text;

    // Outputting the license plate text to the console
    Console.WriteLine(output);
}
using IronOcr;

var ocr = new IronTesseract();

using (var input = new OcrImageInput("licensePlate.jpeg"))
{
    // Fixing the digital noise and making the image easier to read
    input.DeNoise();
    input.ToGrayScale();

    // Using the OcrLicensePlateResult and ReadLicensePlate methods to read the license plate information and store it for further use
    OcrLicensePlateResult result = ocr.ReadLicensePlate(input);

    // Saving the license plate text to a string variable
    string output = result.Text;

    // Outputting the license plate text to the console
    Console.WriteLine(output);
}
Imports IronOcr

Private ocr = New IronTesseract()

Using input = New OcrImageInput("licensePlate.jpeg")
	' Fixing the digital noise and making the image easier to read
	input.DeNoise()
	input.ToGrayScale()

	' Using the OcrLicensePlateResult and ReadLicensePlate methods to read the license plate information and store it for further use
	Dim result As OcrLicensePlateResult = ocr.ReadLicensePlate(input)

	' Saving the license plate text to a string variable
	Dim output As String = result.Text

	' Outputting the license plate text to the console
	Console.WriteLine(output)
End Using
$vbLabelText   $csharpLabel

License Plate Ocr Csharp Tutorial 6 related to 如何在C#中OCR车牌(教程)

代码解析:

  • 初始化:该行 var ocr = new IronTesseract(); 创建一个新的IronTesseract类的实例,该类提供用于光学字符识别(OCR)的方法。
  • 图像输入:using 语句创建一个新的 OcrImageInput 对象,指定的图像文件为 "licensePlate.jpeg"。 该对象用于存储用于OCR处理的图像数据。
  • 图像预处理

    • input.DeNoise(); 应用数字降噪滤波器以提升图像质量,使OCR引擎更容易读取文本。
  • input.ToGrayScale(); 将图像转换为灰度图像,可以提高识别精度和处理速度。
  • 车牌识别:代码行 OcrLicensePlateResult result = ocr.ReadLicensePlate(input); 使用ReadLicensePlate方法分析处理后的图像并提取检测到的任何车牌信息,将结果存储在OcrLicensePlateResult对象中。
  • 输出存储:车牌文本通过访问result.Text存储在字符串变量output中,其中包含从车牌中识别的文本。
  • 控制台输出:最后,Console.WriteLine(output); 将提取的车牌文字打印到控制台以进行验证。

扫描车牌号码

如果我们有一整辆汽车的图像,而不仅仅是车牌,我们可以指定一个矩形区域来专注于车牌区域。 我们可以使用System.Drawing.Rectangle以像素为单位定义此区域。

原始图片

我们将使用以下图像文件作为示例:

License Plate Ocr Csharp Tutorial 7 related to 原始图片

通过指定感兴趣的区域,我们提高了处理速度,并避免提取不必要的文本。

实现代码

using IronOcr;
using System.Drawing;

var ocr = new IronTesseract();

using (var input = new OcrInput())
{
    var contentArea = new Rectangle(x: 252, y: 282, width: 148, height: 47);
    input.LoadImage("CarPlate.jpeg", contentArea);
    OcrLicensePlateResult result = ocr.ReadLicensePlate(input);
    Console.WriteLine(result.Text);
}
using IronOcr;
using System.Drawing;

var ocr = new IronTesseract();

using (var input = new OcrInput())
{
    var contentArea = new Rectangle(x: 252, y: 282, width: 148, height: 47);
    input.LoadImage("CarPlate.jpeg", contentArea);
    OcrLicensePlateResult result = ocr.ReadLicensePlate(input);
    Console.WriteLine(result.Text);
}
Imports IronOcr
Imports System.Drawing

Private ocr = New IronTesseract()

Using input = New OcrInput()
	Dim contentArea = New Rectangle(x:= 252, y:= 282, width:= 148, height:= 47)
	input.LoadImage("CarPlate.jpeg", contentArea)
	Dim result As OcrLicensePlateResult = ocr.ReadLicensePlate(input)
	Console.WriteLine(result.Text)
End Using
$vbLabelText   $csharpLabel

License Plate Ocr Csharp Tutorial 8 related to 实现代码

代码解析:

  • 初始化:var ocr = new IronTesseract(); 创建一个新的IronTesseract类实例,该实例负责执行OCR操作。
  • OCR输入:using (var input = new OcrInput()) 语句创建了一个新的 OcrInput 对象,该对象将用于加载和处理用于OCR的图像。
  • 定义感兴趣的区域:var contentArea = new Rectangle(x: 252, y: 282, width: 148, height: 47); 在图像中定义一个矩形区域(contentArea)。 此矩形指定了预期车牌所在的坐标和尺寸(宽度和高度)。
  • 加载图像: input.LoadImage("CarPlate.jpeg", contentArea); 加载指定的图像文件("CarPlate.jpeg"),并聚焦于定义的矩形区域(contentArea),以将OCR处理限制在该特定区域。
  • 识别车牌号码:OcrLicensePlateResult result = ocr.ReadLicensePlate(input); 调用 ReadLicensePlate 方法,该方法分析输入图像中的车牌字符并返回一个包含提取文本的 OcrLicensePlateResult 对象。
  • 输出:Console.WriteLine(result.Text); 将车牌识别的文字打印到控制台。

自动车牌识别

IronOCR利用OpenCV识别图像中的文本区域,并采用各种图像处理技术。 此功能使程序能够通过定位图像中的文本区域来检测车牌,然后利用Tesseract读取这些区域。

安装

要启用车牌检测模型,请通过包管理控制台安装所需的包:

var ocr = new IronTesseract();

using (var input = new OcrImageInput("CarPlate.jpeg"))
{
    input.FindTextRegion();
    OcrLicensePlateResult result = ocr.ReadLicensePlate(input);
    Console.WriteLine(result.Text);
}
var ocr = new IronTesseract();

using (var input = new OcrImageInput("CarPlate.jpeg"))
{
    input.FindTextRegion();
    OcrLicensePlateResult result = ocr.ReadLicensePlate(input);
    Console.WriteLine(result.Text);
}
Dim ocr = New IronTesseract()

Using input = New OcrImageInput("CarPlate.jpeg")
	input.FindTextRegion()
	Dim result As OcrLicensePlateResult = ocr.ReadLicensePlate(input)
	Console.WriteLine(result.Text)
End Using
$vbLabelText   $csharpLabel

License Plate Ocr Csharp Tutorial 9 related to 安装

代码解析:

  • 初始化:创建一个 IronTesseract 类的实例,该实例将用于与 Tesseract 引擎进行光学字符识别(OCR)。
  • 图像输入: 使用指定的图像文件("CarPlate.jpeg")实例化一个新的 OcrImageInput 对象。 该对象将作为OCR过程的输入,并被包含在using语句中以确保适当的资源管理。
  • 文本区域检测:在输入对象上调用FindTextRegion方法。 该方法采用计算机视觉技术自动识别图像中可能包含文本的区域,特别是针对车牌。
  • 车牌识别:调用 ReadLicensePlate 方法分析检测到的文本区域并提取车牌号。 结果存储在OcrLicensePlateResult对象中,该对象包含识别的文本和任何相关的元数据。
  • 输出:检测到的车牌文本将被打印到控制台,允许用户查看提取的车牌号。

IronOCR 许可

License Plate Ocr Csharp Tutorial 10 related to IronOCR 许可

对于想要亲自试用IronOCR的人,IronOCR提供了一个免费试用,允许您访问所有的工具,这意味着您可以在购买许可证之前在自己的项目中试用这些工具。 一旦您的免费试用期结束,IronOCR 许可起价仅为$749购买 Lite License。 它还提供可选的附加组件,需支付额外费用,例如免版税的再分发覆盖、不间断支持和持续的产品更新。

此外,如果您发现自己需要使用更多的IronSoftware产品,不仅仅是IronOCR,例如使用IronPDF进行PDF相关任务或使用IronWord处理Word文档,那么IronSoftware还提供Iron Suite,这是以优惠价格获取整个工具系列的好方法。

结论

在本指南中,我们探讨了如何使用IronOCR在C#中构建一个可靠的车牌识别系统。 凭借其强大的文本提取能力和与OpenCV的集成,IronOCR为需要从车辆图像中进行准确文本识别的应用程序提供了一种高效、易于使用的解决方案。 从图像预处理到设置特定检测区域,IronOCR使用专为嘈杂或复杂图像(如交通和监控录像中的车牌)设计的工具简化了OCR流程。

无论您是在开发交通监控、停车执法,还是任何需要自动车牌识别的应用程序,IronOCR 都提供了一个能够无缝集成到 .NET 环境中的综合库。 通过遵循这些步骤,您将具备部署 OCR 驱动的解决方案的能力,从而在各种实际场景中提高效率和准确性。 IronOCR通过区域选择和降噪等附加功能,确保您的车牌识别任务获得最佳结果优化。

Kannaopat Udonpant
坎那帕·乌东攀
软件工程师
在成为软件工程师之前,Kannapat 从日本北海道大学完成了环境资源博士学位。在攻读学位期间,Kannapat 还成为了生物生产工程系车辆机器人实验室的成员。2022年,他利用自己的 C# 技能加入了 Iron Software 的工程团队,专注于 IronPDF。Kannapat 珍视他的工作,因为他能直接向编写 IronPDF 大部分代码的开发者学习。除了同伴学习,Kannapat 还享受在 Iron Software 工作的社交方面。不写代码或文档时,Kannapat 通常在 PS5 上玩游戏或重看《最后生还者》。
< 前一页
如何在C#中OCR 驾照
下一步 >
如何在 C# 教程中从发票获取文本