OCR 配置(用于高级阅读)

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronOCR 提供了 ReadLicensePlateReadPhoto 等先进的扫描识别方法,其功能超越了标准 OCR。 这些方法由 IronOcr.Extensions.AdvancedScan 包提供支持。 为了精细调整这些方法处理文本的方式,IronOCR 提供了 TesseractConfiguration 类,使开发者能够完全控制字符白名单、黑名单、BarCode 检测、数据表读取等功能。

本文介绍了可供深入研读的 TesseractConfiguration 属性,并提供了在实际场景中配置 OCR 的实用示例。

快速入门:将 OCR 输出限制为字符白名单

在调用 Read 之前,请先在 TesseractConfiguration 上设置 WhiteListCharacters。 白名单中未包含的任何字符都会被静默移除,从而在无需任何后处理的情况下消除冗余信息。

  1. 使用 NuGet 包管理器安装 https://www.nuget.org/packages/IronOcr

    PM > Install-Package IronOcr
  2. 复制并运行这段代码。

    var result = new IronTesseract() { Configuration = new TesseractConfiguration { WhiteListCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789- " } }.Read(new OcrInput("image.png")); Console.WriteLine(result.Text);
  3. 部署到您的生产环境中进行测试

    通过免费试用立即在您的项目中开始使用IronOCR

    arrow pointer


TesseractConfiguration 属性

TesseractConfiguration 类提供了以下属性,用于自定义 OCR 行为。 这些内容通过 IronTesseract.Co/nfiguration 进行设置。

属性 类型 描述
白名单字符 string OCR 输出中仅识别此字符串中包含的字符。所有其他字符均被排除。
BlackListCharacters string 此字符串中的字符将被主动忽略并从 OCR 输出中移除。
ReadBarCodes bool 在 OCR 处理过程中启用或禁用文档内的 BarCode 检测。
ReadDataTables bool 启用或禁用文档内通过 Tesseract 检测表格结构的功能。
PageSegmentationMode TesseractPageSegmentationMode 决定 Tesseract 如何对输入图像进行分段。选项包括 AutoOsd, Auto, SingleBlock, SingleLine, SingleWord、以及更多选项。
RenderSearchablePdf bool 启用该功能后,OCR 输出可保存为带隐形文本层的可搜索 PDF。
RenderHocr bool 启用后,OCR 输出将包含 hOCR 数据,以便进行进一步处理或导出。
TesseractVariables Dictionary<string, object> 提供对 Tesseract 低级配置变量的直接访问,以实现精细控制。

TesseractVariables 词典的功能更为全面,提供了数百个底层 Tesseract 引擎参数,以应对仅靠高级属性无法满足需求的情况。

下面的示例演示了每个属性组,首先是字符白名单。

为车牌号设置字符白名单

WhiteListCharacters 的常见用例是将 OCR 输出限制为仅包含车牌上可能出现的字符:大写字母、数字、连字符和空格。 通过告知引擎忽略预期字符集之外的内容,可消除干扰并提高准确性。

输入

以下车辆登记记录包含大写文本、小写文本及特殊符号(@, $, #, |, *),以及标点符号。

用于OCR白名单演示的含混合字符的车辆登记记录

BlackListCharacters 通过主动排除已知的干扰符号(如 , and*`。

:path=/static-assets/ocr/content-code-examples/how-to/ocr-configurations-for-advanced-reading.cs
using IronOcr;

// Initialize the Tesseract OCR engine
IronTesseract ocr = new IronTesseract();

ocr.Configuration = new TesseractConfiguration
{
    // Whitelist only characters that appear on license plates
    WhiteListCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789- ",

    // Blacklist common noise characters
    BlackListCharacters = "`~@#$%&*",
};

var ocrInput = new OcrInput();
// Load the input image
ocrInput.LoadImage("advanced-input.png");
// Perform OCR on the input image with ReadPhoto method
var results = ocr.ReadPhoto(ocrInput);

// Print the filtered text result to the console
Console.WriteLine(results.Text);
Imports IronOcr

' Initialize the Tesseract OCR engine
Dim ocr As New IronTesseract()

ocr.Configuration = New TesseractConfiguration With {
    ' Whitelist only characters that appear on license plates
    .WhiteListCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789- ",
    
    ' Blacklist common noise characters
    .BlackListCharacters = "`~@#$%&*"
}

Dim ocrInput As New OcrInput()
' Load the input image
ocrInput.LoadImage("advanced-input.png")
' Perform OCR on the input image with ReadPhoto method
Dim results = ocr.ReadPhoto(ocrInput)

' Print the filtered text result to the console
Console.WriteLine(results.Text)
$vbLabelText   $csharpLabel

输出

仅显示白名单中车牌字符的OCR输出

白名单过滤的效果在结果中清晰可见:

  • "Plate: ABC-1234" 应译为 "P ABC-1234"。 小写单词"late:"被省略,而车牌号码则完全保留。
  • "VIN: 1HGBH41JXMN109186" 应译为 "VIN 1HGBH41JXMN109186"。 冒号被省略,但 VIN 的大写形式和完整编号予以保留。
  • "所有者:john.doe@email.com"应译为"O"。 已移除全小写字母及所有标点符号。
  • "地区:CA-90210 | "Zone #5"应译为"R CA-90210 Z 5"。 竖线(|) and hash (#) 被移除,而大写字母和数字则保留。
  • "费用:125.00 美元 + 税*"应译为"12500 美元"。 美元符号、小数点、加号以及小写"tax"均已删除。
  • "Ref: ~record_v2^final" 译为 "R 2"。 波浪线 (~)、下划线、插入符 (^) 以及所有小写字符均被移除。

WhiteListCharactersBlackListCharacters 这种方法适用于任何类型的文档,而不仅仅是车牌。 下一节将演示如何扩展读取功能,以在同一轮处理中检测BARCODE和表格结构。

配置BarCode和数据表读取

IronOCR 不仅能识别文档中的文本,还能检测其中的 BARCODE 和结构化表格。 这些功能通过 TesseractConfiguration 进行控制:

IronTesseract ocr = new IronTesseract();

ocr.Co/nfiguration = new TesseractConfiguration
{
    // Enable barcode detection within documents
    ReadBarCodes = true,

    // Enable table structure detection
    ReadDataTables = true,
};
IronTesseract ocr = new IronTesseract();

ocr.Co/nfiguration = new TesseractConfiguration
{
    // Enable barcode detection within documents
    ReadBarCodes = true,

    // Enable table structure detection
    ReadDataTables = true,
};
Dim ocr As New IronTesseract()

ocr.Configuration = New TesseractConfiguration With {
    .ReadBarCodes = True,
    .ReadDataTables = True
}
$vbLabelText   $csharpLabel
  • ReadBarCodes:当设置为 true 时,IronOCR 除了文本外,还会扫描文档中的 BarCode。 当预期不包含 BarCode 时,请设置为 false 以跳过 BarCode 检测并加快处理速度。
  • ReadDataTables:当设置为 true 时,Tesseract 会尝试检测并保留文档中的表格结构。 这适用于发票、报告及其他表格文档。

这些选项可与 WhiteListCharactersBlackListCharacters 结合使用,以便精确控制从复杂文档中提取的内容。

虽然过滤和检测功能控制着提取的内容,但版式解析则是另一个需要关注的问题。 下一节将介绍如何根据文档类型选择合适的 PageSegmentationMode

控制页面分段模式

PageSegmentationMode 用于告知 Tesseract 在识别前如何对输入图像进行分段。 若针对特定版式选择了错误的模式,会导致引擎误读或完全跳过文本。

模式 使用场景
AutoOsd 支持方向和脚本检测的自动版式分析
Auto 不使用 OSD 的自动布局分析(默认)
SingleColumn 假设图片为单栏文本
SingleBlock 假设图片是一个单一的、统一的文本块
SingleLine 假设图片仅包含一行文本
SparseText 按任意顺序查找尽可能多的文本

对于仅含单行内容的标签或横幅,SingleLine 可避免多块分析,从而同时提升速度和准确性。

输入

single-line-label.png 是一个窄版运输标签,上面仅有一行粗体 Courier 字体文本:SHIPPING LABEL: TRK-2024-XR9-001

适用于 OCR SingleLine 分割模式的单行运单标签
IronTesseract ocr = new IronTesseract();

ocr.Co/nfiguration = new TesseractConfiguration
{
    PageSegmentationMode = TesseractPageSegmentationMode.SingleLine,
};

using OcrInput input = new OcrInput();
input.LoadImage("single-line-label.png");

OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
IronTesseract ocr = new IronTesseract();

ocr.Co/nfiguration = new TesseractConfiguration
{
    PageSegmentationMode = TesseractPageSegmentationMode.SingleLine,
};

using OcrInput input = new OcrInput();
input.LoadImage("single-line-label.png");

OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
Imports IronOcr

Dim ocr As New IronTesseract()

ocr.Configuration = New TesseractConfiguration With {
    .PageSegmentationMode = TesseractPageSegmentationMode.SingleLine
}

Using input As New OcrInput()
    input.LoadImage("single-line-label.png")

    Dim result As OcrResult = ocr.Read(input)
    Console.WriteLine(result.Text)
End Using
$vbLabelText   $csharpLabel

对于文本布局不规则的扫描页面,SparseText 恢复的内容比 Auto 更多。

输入

receipt-scan.png 是一张 Corner Market 的热敏收据,包含四项商品(咖啡、松饼、果汁、格兰诺拉能量棒)、一条虚线分隔符、小计、税费和总计。 在这种布局中,固定块分段会导致不同水平位置的条目被遗漏。

适用于 OCR SparseText 分段模式的热敏收据
IronTesseract ocr = new IronTesseract();

ocr.Co/nfiguration = new TesseractConfiguration
{
    PageSegmentationMode = TesseractPageSegmentationMode.SparseText,
};

using OcrInput input = new OcrInput();
input.LoadImage("receipt-scan.png");

OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
IronTesseract ocr = new IronTesseract();

ocr.Co/nfiguration = new TesseractConfiguration
{
    PageSegmentationMode = TesseractPageSegmentationMode.SparseText,
};

using OcrInput input = new OcrInput();
input.LoadImage("receipt-scan.png");

OcrResult result = ocr.Read(input);
Console.WriteLine(result.Text);
Imports IronOcr

Dim ocr As New IronTesseract()

ocr.Configuration = New TesseractConfiguration With {
    .PageSegmentationMode = TesseractPageSegmentationMode.SparseText
}

Using input As New OcrInput()
    input.LoadImage("receipt-scan.png")

    Dim result As OcrResult = ocr.Read(input)
    Console.WriteLine(result.Text)
End Using
$vbLabelText   $csharpLabel

在根据文档类型调整布局分段后,下一步是控制输出格式以满足后续处理需求。

生成可搜索的 PDF 文件和 hOCR 输出

RenderSearchablePdfRenderHocr 控制 IronOCR 在生成纯文本结果时所采用的输出格式。

RenderSearchablePdf 在原始图像上叠加一层不可见的文本层,生成PDF文件,用户既可在其中搜索和复制文本,又能同时查看扫描图像。 这是文档归档工作流的标准输出格式。

输入

scanned-document.pdf 是 IronOCR Solutions Ltd. 的一封单页商业信函(日期为 2024 年 3 月 15 日,参考编号 DOC-2024-OCR-0315)。 翻译结果将保存为 searchable-output.pdf

IronTesseract ocr = new IronTesseract();

ocr.Co/nfiguration = new TesseractConfiguration
{
    RenderSearchablePdf = true,
};

using OcrInput input = new OcrInput();
input.LoadPdf("scanned-document.pdf");

OcrResult result = ocr.Read(input);
result.SaveAsSearchablePdf("searchable-output.pdf");
IronTesseract ocr = new IronTesseract();

ocr.Co/nfiguration = new TesseractConfiguration
{
    RenderSearchablePdf = true,
};

using OcrInput input = new OcrInput();
input.LoadPdf("scanned-document.pdf");

OcrResult result = ocr.Read(input);
result.SaveAsSearchablePdf("searchable-output.pdf");
Imports IronTesseract

Dim ocr As New IronTesseract()

ocr.Configuration = New TesseractConfiguration With {
    .RenderSearchablePdf = True
}

Using input As New OcrInput()
    input.LoadPdf("scanned-document.pdf")

    Dim result As OcrResult = ocr.Read(input)
    result.SaveAsSearchablePdf("searchable-output.pdf")
End Using
$vbLabelText   $csharpLabel

输出

输出结果为一份与输入内容外观完全一致的 PDF 文件,但其中包含一个隐藏的文本层。 打开 searchable-output.pdf 并使用 Ctrl+F 验证嵌入的文本是否可搜索和可复制。

RenderHocr 生成一个 hOCR 文档,即一个 HTML 文件,其中包含文本内容以及每个 WORD 的边界框坐标。 这在下游工具需要精确的WORD定位时非常有用,例如信息遮蔽引擎或文档布局分析。

输入

document-page.png 是一份文档页面,标题为"2024年第一季度总结",包含两段财务数据,涵盖收入、运营成本及增长驱动因素。 翻译结果将保存为 output.html

文档页面输入,用于生成 hOCR 边界框输出
IronTesseract ocr = new IronTesseract();

ocr.Co/nfiguration = new TesseractConfiguration
{
    RenderHocr = true,
};

using OcrInput input = new OcrInput();
input.LoadImage("document-page.png");

OcrResult result = ocr.Read(input);
result.SaveAsHocrFile("output.html");
IronTesseract ocr = new IronTesseract();

ocr.Co/nfiguration = new TesseractConfiguration
{
    RenderHocr = true,
};

using OcrInput input = new OcrInput();
input.LoadImage("document-page.png");

OcrResult result = ocr.Read(input);
result.SaveAsHocrFile("output.html");
Imports IronTesseract

Dim ocr As New IronTesseract()

ocr.Configuration = New TesseractConfiguration With {
    .RenderHocr = True
}

Using input As New OcrInput()
    input.LoadImage("document-page.png")

    Dim result As OcrResult = ocr.Read(input)
    result.SaveAsHocrFile("output.html")
End Using
$vbLabelText   $csharpLabel

输出

output.html 会为每个识别出的WORD编码其边界框坐标。 在浏览器中打开文件以查看 hOCR 结构,或将其传递给下游工具进行版式分析或信息遮盖。

如果您需要通过单次读取调用同时获取三种输出格式(纯文本、可搜索PDF和hOCR),可以同时启用这两个标志。

这些输出标志与所读语言无关,包括非拉丁字符集。下一节将展示如何对日语文本应用字符过滤。

国际文档的 Unicode 字符过滤

对于中文、日语或韩语的国际文档,WhiteListCharactersBlackListCharacters 属性支持 Unicode 字符。 这允许您将输出限制为特定字符集,例如仅输出日语中的平假名和片假名。

请注意 请确保已安装相应的语言包(例如 IronOcr.Languages.Japanese)后再继续。

输入

文档包含一个标题(テスト)、一个混合了平假名和片假名并带有浊音标记变体(プ, で)的日语句子、一条包含黑名单噪音符号(★, ■)及汉字(価格),以及包含另一个被屏蔽符号(§)、更多汉字(購入)、额外有声标记变体(プ, デ)和基础片假名(メモ, ール)的备注行。 白名单仅允许通过基础平假名、基础片假名、数字以及常见的日语标点符号; 三个噪音符号已被明确列入黑名单。

OCR 高级配置 日语输入

平假名和片假名的 Unicode 字符范围作为字符串字面量传递在 WhiteListCharacters 中,噪声符号列在 BlackListCharacters 中。

警告 控制台可能不支持显示 Unicode 字符。 将输出重定向到 .txt 文件是处理此类字符时验证结果的可靠方法。)}]

:path=/static-assets/ocr/content-code-examples/how-to/ocr-configurations-for-advanced-reading-jp.cs
using IronOcr;
using System.IO;

IronTesseract ocr = new IronTesseract();

ocr.Configuration = new TesseractConfiguration
{
    // Whitelist only Hiragana, Katakana, numbers, and common Japanese punctuation
    WhiteListCharacters = "あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもやゆよらりるれろわをん" +
                            "アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヲン" +
                            "0123456789、。?!()¥ー",

    // Blacklist common noise/symbols you want to ignore
    BlackListCharacters = "★■§",
};

var ocrInput = new OcrInput();

// Load Japanese input image
ocrInput.LoadImage("jp.png");

// Perform OCR on the input image with ReadPhoto method
var results = ocr.ReadPhoto(ocrInput);

// Write the text result directly to a file named "output.txt"
File.WriteAllText("output.txt", results.Text);

// You can add this line to confirm the file was saved:
Console.WriteLine("OCR results saved to output.txt");
Imports IronOcr
Imports System.IO

Dim ocr As New IronTesseract()

ocr.Configuration = New TesseractConfiguration With {
    .WhiteListCharacters = "あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほまみむめもやゆよらりるれろわをん" &
                           "アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヲン" &
                           "0123456789、。?!()¥ー",
    .BlackListCharacters = "★■§"
}

Dim ocrInput As New OcrInput()

' Load Japanese input image
ocrInput.LoadImage("jp.png")

' Perform OCR on the input image with ReadPhoto method
Dim results = ocr.ReadPhoto(ocrInput)

' Write the text result directly to a file named "output.txt"
File.WriteAllText("output.txt", results.Text)

' You can add this line to confirm the file was saved:
Console.WriteLine("OCR results saved to output.txt")
$vbLabelText   $csharpLabel

输出

OCR 高级配置 日语输出

完整的过滤后输出已保存为文本文件:jp-output.txt

由于白名单仅包含基础平假名和片假名字符,因此诸如 プ (pu) 和 デ (de) 等带声调标记的衍生变体将被省略。 此外,由于"価格"(价格)和"購入"(购买)等汉字不在白名单字符集范围内,因此也应予以排除。 无论白名单如何设置,系统都会主动移除 § 等黑名单符号。

接下来该去哪里?

既然您已了解如何配置 IronOCR 以应对高级阅读场景,请继续探索:

若用于生产环境,请务必获取许可证以去除水印并使用全部功能。

常见问题解答

IronOCR 中的 TesseractConfiguration 是什么?

IronOCR 中的 TesseractConfiguration 允许用户自定义 OCR 设置,从而实现字符白名单、BarCode 识别和多语言支持等高级识别功能。

如何在 IronOCR 中设置字符白名单?

在 IronOCR 中,您可以通过 TesseractConfiguration 设置字符白名单,从而指定 OCR 引擎应识别的字符,这对于读取车牌号等任务非常有用。

IronOCR 能读取 BarCode 和数据表吗?

是的,IronOCR 可以通过调整 TesseractConfiguration 属性中的特定设置,配置为读取 BarCode 和数据表,从而实现精确的 OCR 数据提取。

IronOCR 是否支持中文、日语和韩语等国际语言?

IronOCR 通过其多语言 TesseractConfiguration 选项支持多种国际语言,包括中文、日语和韩语。

在 IronOCR 中使用高级 OCR 配置有哪些优势?

利用 IronOCR 中的高级 OCR 配置,可实现更准确、更高效的文本识别,并支持特定语言的文本识别和结构化数据提取等专业任务。

能否针对特定的 OCR 任务对 IronOCR 进行优化?

是的,IronOCR 可以通过配置字符白名单、启用 BARCODE 或表格识别等设置,针对特定的 OCR 任务进行优化,从而提升针对特定应用的性能。

如何在 IronOCR 中启用多语言支持?

要在 IronOCR 中启用多语言支持,您可以在 TesseractConfiguration 中调整语言设置,从而使 OCR 引擎能够识别多种语言的文本。

什么是字符白名单,它们在 IronOCR 中如何使用?

IronOCR 中的字符白名单是 OCR 引擎配置为识别的特定字符列表,非常适合读取数字或特定文本模式等针对性任务。

IronOCR 能否用于读取结构化数据格式?

是的,IronOCR 可以配置为读取和处理 BARCODE 和表格等结构化数据格式,为各种数据提取需求提供多功能的 OCR 能力。

IronOCR 提供了哪些用于高级文本识别的配置选项?

IronOCR 提供字符白名单、多语言支持和 BarCode 识别等配置选项,以增强高级文本识别能力,满足特定需求。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

准备开始了吗?
Nuget 下载 5,896,332 | 版本: 2026.5 just released
Still Scrolling Icon

还在滚动吗?

想快速获得证据? PM > Install-Package IronOcr
运行示例 观看您的图像变成可搜索文本。