如何在 C# 中定义图像的特定 OCR 区域;

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

要在 C# 中从图像的特定区域提取文本,请使用 IronOCR 的 Rectangle 对象,通过指定 x/y 坐标、widthheight 来定义精确区域,然后将其传递给 LoadImage 方法,以便进行有针对性的 OCR 处理。

as-heading:2(快速入门:从特定图像区域提取文本)

Nuget Icon立即开始使用 NuGet 创建 PDF 文件:

  1. 使用 NuGet 包管理器安装 IronOCR

    PM > Install-Package IronOcr

  2. 复制并运行这段代码。

    using IronOcr;
    using IronSoftware.Drawing;
    
    // 1. Install IronOCR via NuGet: Install-Package IronOcr
    var ocr = new IronTesseract();
    using var input = new OcrInput();
    
    // 2. Create a Rectangle with coordinates
    var region = new Rectangle(x: 215, y: 1250, width: 1335, height: 280);
    
    // 3. Load image with region
    input.LoadImage("image.png", region);
    
    // 4. Extract text
    var result = ocr.Read(input);
    Console.WriteLine(result.Text);
  3. 部署到您的生产环境中进行测试

    立即开始在您的项目中使用 IronOCR,免费试用!
    arrow pointer

通常情况下,你只需要从图像的一小部分中提取文本,例如发票上的总金额或表单中的特定字段。 扫描整个文档效率低下,并且可能因捕获无关文本而引入错误。

IronOCR 允许您通过指定要扫描的精确矩形区域来提高精度、性能和准确性。 本指南提供了逐步演练,指导您如何定义特定的 OCR 区域、从中提取文本,并通过视觉方式验证您的坐标是否适用于您的 OCR 任务。

IronOCR 入门指南


如何在特定区域执行 OCR?

要定义特定的 OCR 区域,您可以从 Iron Software.Drawing 命名空间创建一个Rectangle对象。 该对象需要四个值:x坐标、y坐标、widthheight,均以像素为单位。 (x, y) 坐标代表您所需区域的左上角。

当您使用LoadImage加载图像时,您需要将此Rectangle作为第二个参数传递。 IronOCR 随后会将 OCR 过程限制在该边界框内的像素范围内。

区域 OCR 在处理结构化文档(如 发票扫描表格身份文档)时特别有用,因为在这些文档中,特定信息总是出现在可预测的位置。 通过将 OCR 限制在相关区域,可以显著提高处理速度,减少无关文本的误报。

要找到 Rectangle 的坐标,您可以使用 MS Paint 等简单的图像编辑器。 打开输入图像,将鼠标悬停在指定区域的左上角和右下角,并记下 (x, y) 像素坐标。 然后,您可以计算矩形的属性:(x1, y1, width, height ),其中 width = x2-x1height = y2-y1

测试时应使用什么图像?

我们将使用一张包含三个段落的示例图片。我们的目标是只提取第二个段落,忽略其余文本。 这是一个常见的场景,您需要从一个较大的文档中提取特定的字段或章节。

显示 OCR 结果的终端窗口,带有'Hello World'标题和关于书店的提取文本

如何在代码中实现区域 OCR?

实现过程包括创建一个 OcrInput 对象,并用指定的矩形区域加载您的图像。 这种方法适用于各种图像格式,包括 JPG、PNG、GIF、TIFF 和 BMP

:path=/static-assets/ocr/content-code-examples/how-to/ocr-region-of-an-image.cs
using IronOcr;
using IronSoftware.Drawing;
using System;

var ocrTesseract = new IronTesseract();
using var ocrInput = new OcrInput();

// Define the specific region as a Rectangle
// (x, y) is the top-left corner.
var ContentArea = new Rectangle(x: 215, y: 1250, width: 1335, height: 280);

ocrInput.LoadImage("region-input.png", ContentArea);

var ocrResult = ocrTesseract.Read(ocrInput);

// Print the extracted text
Console.WriteLine(ocrResult.Text);
Imports IronOcr
Imports IronSoftware.Drawing
Imports System

Dim ocrTesseract As New IronTesseract()
Using ocrInput As New OcrInput()

    ' Define the specific region as a Rectangle
    ' (x, y) is the top-left corner.
    Dim ContentArea As New Rectangle(x:=215, y:=1250, width:=1335, height:=280)

    ocrInput.LoadImage("region-input.png", ContentArea)

    Dim ocrResult = ocrTesseract.Read(ocrInput)

    ' Print the extracted text
    Console.WriteLine(ocrResult.Text)

End Using
$vbLabelText   $csharpLabel

对于更复杂的情况,您可以在同一图像中定义多个区域。 这在处理具有多个字段的表单或文档中的表格时尤其有用:

using IronOcr;
using IronSoftware.Drawing;

var ocr = new IronTesseract();
using var input = new OcrInput();

// Define multiple regions for different form fields
var nameField = new Rectangle(x: 100, y: 200, width: 300, height: 50);
var dateField = new Rectangle(x: 100, y: 300, width: 200, height: 50);
var amountField = new Rectangle(x: 400, y: 500, width: 150, height: 50);

// Load the same image multiple times with different regions
input.LoadImage("form.png", nameField);
var nameResult = ocr.Read(input);

input.Clear();
input.LoadImage("form.png", dateField);
var dateResult = ocr.Read(input);

input.Clear();
input.LoadImage("form.png", amountField);
var amountResult = ocr.Read(input);

// Process each field separately
Console.WriteLine($"Name: {nameResult.Text}");
Console.WriteLine($"Date: {dateResult.Text}");
Console.WriteLine($"Amount: {amountResult.Text}");
using IronOcr;
using IronSoftware.Drawing;

var ocr = new IronTesseract();
using var input = new OcrInput();

// Define multiple regions for different form fields
var nameField = new Rectangle(x: 100, y: 200, width: 300, height: 50);
var dateField = new Rectangle(x: 100, y: 300, width: 200, height: 50);
var amountField = new Rectangle(x: 400, y: 500, width: 150, height: 50);

// Load the same image multiple times with different regions
input.LoadImage("form.png", nameField);
var nameResult = ocr.Read(input);

input.Clear();
input.LoadImage("form.png", dateField);
var dateResult = ocr.Read(input);

input.Clear();
input.LoadImage("form.png", amountField);
var amountResult = ocr.Read(input);

// Process each field separately
Console.WriteLine($"Name: {nameResult.Text}");
Console.WriteLine($"Date: {dateResult.Text}");
Console.WriteLine($"Amount: {amountResult.Text}");
Imports IronOcr
Imports IronSoftware.Drawing

Dim ocr As New IronTesseract()
Using input As New OcrInput()

    ' Define multiple regions for different form fields
    Dim nameField As New Rectangle(x:=100, y:=200, width:=300, height:=50)
    Dim dateField As New Rectangle(x:=100, y:=300, width:=200, height:=50)
    Dim amountField As New Rectangle(x:=400, y:=500, width:=150, height:=50)

    ' Load the same image multiple times with different regions
    input.LoadImage("form.png", nameField)
    Dim nameResult = ocr.Read(input)

    input.Clear()
    input.LoadImage("form.png", dateField)
    Dim dateResult = ocr.Read(input)

    input.Clear()
    input.LoadImage("form.png", amountField)
    Dim amountResult = ocr.Read(input)

    ' Process each field separately
    Console.WriteLine($"Name: {nameResult.Text}")
    Console.WriteLine($"Date: {dateResult.Text}")
    Console.WriteLine($"Amount: {amountResult.Text}")

End Using
$vbLabelText   $csharpLabel

我可以期待什么结果?

从控制台输出可以看出,只有第二个段落被 OCR 处理了。 这种有针对性的方法可确保图像其他部分的无关文本不会干扰您的结果。

OCR 输出

区域 OCR 的准确性取决于多个因素:

  • 图像质量:分辨率较高的图像通常能产生更好的效果。 考虑使用 DPI 设置来优化您的图片。
  • 文本方向:确保文本正确对齐。 如有需要,请使用页面旋转检测
  • 对比度和清晰度:应用图像校正过滤器来增强文本的可读性。

如何验证我的坐标是否正确?

为了确保您已为输入图像选择了正确的坐标,您可以可视化您定义的ContentArea 。 一个简单的方法是在输入图像上绘制矩形,然后使用StampCropRectangleAndSaveAs将其另存为新文件。 这有助于您调试和微调坐标,以获得最佳性能。

在处理 复杂布局或需要突出显示特定文本区域以保证质量时,这种可视化技术尤其有用。

这是在上面的示例输入图像上绘制指定边界框后的输出图像。

如何可视化所选区域?

:path=/static-assets/ocr/content-code-examples/how-to/ocr-region-of-an-image-highlighted.cs
using IronOcr;
using IronSoftware.Drawing;

var ocrTesseract = new IronTesseract();
using var ocrInput = new OcrInput();

// Define the specific rectangular area to scan within the image.
// The coordinates are in pixels: (x, y) is the top-left corner of the rectangle.
var ContentArea = new Rectangle(x: 4, y: 59, width: 365, height: 26);

ocrInput.LoadImage("region-input.png", ContentArea);

var ocrResult = ocrTesseract.Read(ocrInput);

// Draws the rectangle from above in a blue bounding box on the image for visualization.
ocrInput.StampCropRectangleAndSaveAs(ContentArea, Color.Aqua, "region-input.png");
Imports IronOcr
Imports IronSoftware.Drawing

Dim ocrTesseract = New IronTesseract()
Using ocrInput As New OcrInput()

    ' Define the specific rectangular area to scan within the image.
    ' The coordinates are in pixels: (x, y) is the top-left corner of the rectangle.
    Dim ContentArea As New Rectangle(x:=4, y:=59, width:=365, height:=26)

    ocrInput.LoadImage("region-input.png", ContentArea)

    Dim ocrResult = ocrTesseract.Read(ocrInput)

    ' Draws the rectangle from above in a blue bounding box on the image for visualization.
    ocrInput.StampCropRectangleAndSaveAs(ContentArea, Color.Aqua, "region-input.png")
End Using
$vbLabelText   $csharpLabel

可视化看起来像什么?

OCR 高亮输出

浅蓝色矩形框表明我们已经正确地提取出第二个段落进行处理。

何时应使用区域 OCR?

区域 OCR 是几种常见场景的理想选择:

1.表单处理:从数据出现在一致位置的标准化表单中提取特定字段时。 2.发票处理:提取特定值,如总额、日期或发票号码,而无需处理整个文档。 3.牌照:使用牌照识别时,请仅关注牌照区域。 4.身份证件:从护照或身份证中提取特定字段。 5.屏幕截图:在 屏幕截图中捕捉特定 UI 元素的文本时。

区域 OCR 的最佳实践

使用区域 OCR 实现最佳效果:

1.添加衬垫:在文本周围添加一个小缓冲区,以确保没有字符在边缘被切断。 2.使用样本图像进行测试:在大批量处理之前,始终使用具有代表性的样本验证您的坐标。 3.处理差异:考虑到扫描文档中的细微定位变化,将您的区域设置得比必要的稍大一些。 4.优化性能:对于多线程处理,请并行处理不同的区域。 5.监控信心:检查结果置信度得分以确保准确性。

通过将 OCR 处理集中在特定区域,可以显著提高文本提取任务的速度和准确性。 这种有针对性的方法对于在您的 .NET 应用程序中构建高效的文档处理工作流至关重要。

常见问题解答

如何用 C# 仅从图像的特定部分提取文本?

使用 IronOCR,您可以通过创建一个具有 x/y 坐标、宽度和高度值的 Rectangle 对象,从特定区域提取文本。将此 Rectangle 作为第二个参数传递给 LoadImage 方法,IronOCR 就会将其 OCR 处理限制在该定义区域内。

定义 OCR 区域而不是扫描整个图像有什么好处?

通过使用 IronOCR 定义特定的 OCR 区域,可以提高处理速度、增加准确性并减少因捕获无关文本而产生的错误。这对于信息出现在可预测位置的结构化文档尤其有用。

为区域 OCR 创建矩形需要哪些参数?

要为 IronOCR 的区域 OCR 创建一个矩形,需要四个像素值:x 坐标、y 坐标、宽度和高度。x、y)坐标代表所需扫描区域的左上角。

哪个命名空间包含用于 OCR 区域的矩形对象?

IronOCR 中用于定义 OCR 区域的矩形对象位于 IronSoftware.Drawing 命名空间中。

哪些类型的文档最受益于区域 OCR 处理?

IronOCR 的区域 OCR 对于发票、扫描表格和身份证明文件等结构化文件特别有效,因为这些文件中的特定信息始终出现在相同的位置。

Curtis Chau
技术作家

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

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

准备开始了吗?
Nuget 下载 5,384,824 | 版本: 2026.2 刚刚发布