如何在C#中定义图像的特定OCR区域
在 C# 中从图像的特定区域提取文本时,使用 IronOCR 的 Rectangle 对象,通过 x/y 坐标、width 和 height 指定区域,再将该对象传给 LoadImage 方法,仅对该区域执行 OCR。
-
1使用 IronOCR 安装 NuGet Package Manager
-
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);Imports IronOcr Imports IronSoftware.Drawing ' 1. Install IronOCR via NuGet: Install-Package IronOcr Dim ocr = New IronTesseract() Using input = New OcrInput() ' 2. Create a Rectangle with coordinates Dim region = New Rectangle(x:=215, y:=1250, width:=1335, height:=280) ' 3. Load image with region input.LoadImage("image.png", region) ' 4. Extract text Dim result = ocr.Read(input) Console.WriteLine(result.Text) End UsingImports IronOcr Imports IronSoftware.Drawing ' 1. Install IronOCR via NuGet: Install-Package IronOcr Dim ocr = New IronTesseract() Using input = New OcrInput() ' 2. Create a Rectangle with coordinates Dim region = New Rectangle(x:=215, y:=1250, width:=1335, height:=280) ' 3. Load image with region input.LoadImage("image.png", region) ' 4. Extract text Dim result = ocr.Read(input) Console.WriteLine(result.Text) End Using -
3部署到您的生产环境中进行测试
今天就用免费试用在您的项目中开始使用IronOCR
通常只需从图像的一小部分提取文本,例如发票总额或表单中的某个字段。扫描整个文档效率较低,还可能因识别到无关文本而引入错误。
在 IronOCR 中指定要扫描的矩形区域,有助于提高识别精度、处理性能和结果准确性。本指南逐步介绍如何定义 OCR 区域、提取其中的文本,并在图像上核对坐标。
IronOCR 入门指南
如何定义图像中的特定OCR区域
- 下载用于定义OCR区域的C#库
- 实例化 OCR 引擎
- 用矩形框标出OCR区域。
- 调用
LoadImage时传入图像和定义好的矩形区域 - 访问OcrResult属性以查看和操作提取的数据
如何在特定区域执行OCR?
要定义特定的OCR区域,您需要从IronSoftware.Drawing命名空间创建一个Rectangle对象。 此对象需要四个值:x坐标、y坐标、width和height,单位为像素。 (x, y)坐标代表您所需区域的左上角。
调用 LoadImage 加载图像时,将这个 Rectangle 作为第二个参数传入。IronOCR 只会识别边界框内的像素。
区域OCR在处理如发票、扫描表单或身份文件等结构化文档时特别有用,其中特定信息总是在可预测的位置出现。 通过将 OCR 限制在相关区域,可以显著提高处理速度,减少无关文本的误报。
要为您的Rectangle找到坐标,您可以使用简单的图像编辑器如MS Paint。 打开您的输入图像,将鼠标悬停在指定区域的左上角和右下角,并记录(x, y)像素坐标。 然后您可以计算矩形的属性:(x1, y1, width, height),其中width = x2-x1,height = y2-y1。
我应该使用什么图像进行测试?
我们将使用一张包含三个段落的示例图片。我们的目标是只提取第二个段落,忽略其余文本。 这是一个常见的场景,您需要从一个较大的文档中提取特定的字段或章节。

如何在代码中实现区域OCR?
创建一个 OcrInput 对象,然后在加载图像时指定矩形区域。此方法支持 JPG、PNG、GIF、TIFF 和 BMP 等图像格式。
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);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 UsingImports 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对于更复杂的情况,您可以在同一图像中定义多个区域。 在处理具有多个字段的表单或文档中的表格时尤其有用:
using IronOcr;
using IronSoftware.Drawing;
var ocr = new IronTesseract();
// 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 with a separate OcrInput per region
OcrResult nameResult;
using (var input = new OcrInput())
{
input.LoadImage("form.png", nameField);
nameResult = ocr.Read(input);
}
OcrResult dateResult;
using (var input = new OcrInput())
{
input.LoadImage("form.png", dateField);
dateResult = ocr.Read(input);
}
OcrResult amountResult;
using (var input = new OcrInput())
{
input.LoadImage("form.png", amountField);
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();
// 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 with a separate OcrInput per region
OcrResult nameResult;
using (var input = new OcrInput())
{
input.LoadImage("form.png", nameField);
nameResult = ocr.Read(input);
}
OcrResult dateResult;
using (var input = new OcrInput())
{
input.LoadImage("form.png", dateField);
dateResult = ocr.Read(input);
}
OcrResult amountResult;
using (var input = new OcrInput())
{
input.LoadImage("form.png", amountField);
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 = New IronTesseract()
' Define multiple regions for different form fields
Dim nameField = New Rectangle(x:=100, y:=200, width:=300, height:=50)
Dim dateField = New Rectangle(x:=100, y:=300, width:=200, height:=50)
Dim amountField = New Rectangle(x:=400, y:=500, width:=150, height:=50)
' Load the same image with a separate OcrInput per region
Dim nameResult As OcrResult
Using input = New OcrInput()
input.LoadImage("form.png", nameField)
nameResult = ocr.Read(input)
End Using
Dim dateResult As OcrResult
Using input = New OcrInput()
input.LoadImage("form.png", dateField)
dateResult = ocr.Read(input)
End Using
Dim amountResult As OcrResult
Using input = New OcrInput()
input.LoadImage("form.png", amountField)
amountResult = ocr.Read(input)
End Using
' 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 = New IronTesseract()
' Define multiple regions for different form fields
Dim nameField = New Rectangle(x:=100, y:=200, width:=300, height:=50)
Dim dateField = New Rectangle(x:=100, y:=300, width:=200, height:=50)
Dim amountField = New Rectangle(x:=400, y:=500, width:=150, height:=50)
' Load the same image with a separate OcrInput per region
Dim nameResult As OcrResult
Using input = New OcrInput()
input.LoadImage("form.png", nameField)
nameResult = ocr.Read(input)
End Using
Dim dateResult As OcrResult
Using input = New OcrInput()
input.LoadImage("form.png", dateField)
dateResult = ocr.Read(input)
End Using
Dim amountResult As OcrResult
Using input = New OcrInput()
input.LoadImage("form.png", amountField)
amountResult = ocr.Read(input)
End Using
' Process each field separately
Console.WriteLine($"Name: {nameResult.Text}")
Console.WriteLine($"Date: {dateResult.Text}")
Console.WriteLine($"Amount: {amountResult.Text}")识别结果是什么样的?
从控制台输出可以看出,只有第二个段落被 OCR 处理了。 这种有针对性的方法可确保图像其他部分的无关文本不会干扰您的结果。

区域 OCR 的准确性取决于多个因素:
如何验证我的坐标是正确的?
可以将定义好的 ContentArea 显示在输入图像上,以验证坐标是否正确。将矩形绘制在图像上,再使用 StampCropRectangleAndSaveAs 保存为新文件,便于调试和微调坐标,从而优化处理效果。
这种可视化技术在处理复杂布局或需要突出显示特定文字区域以确保质量时特别有用。
这是在上面的示例输入图像上绘制指定边界框后的输出图像。
如何可视化所选区域?
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");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 UsingImports 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标出区域后的图像是什么样的?

浅蓝色矩形框表明,选定的区域只包含要识别的第二段。
何时使用区域 OCR?
区域 OCR 是几种常见场景的理想选择:
- 表单处理:从字段位置固定的标准表单中提取指定字段。
- 发票处理:提取特定值如总额、日期或发票号而不处理整个文档。
- 车牌:在使用车牌识别时,仅关注车牌区域。
- 身份证件:从护照或身份证中提取特定字段。
- 截图:在截图中捕获特定UI元素的文本时。
区域OCR的最佳实践
使用区域 OCR 实现最佳效果:
- 预留边距:在文本周围留出少量空间,避免边缘的字符被截断。
- 使用示例图像进行测试:在处理大批量之前,始终使用代表性样本验证您的坐标。
- 应对位置偏移:适当扩大识别区域,以容纳扫描文档中文字位置的轻微偏移。
- 优化性能:对于多线程处理,请并行处理不同的区域。
- 监测置信度:检查识别结果的置信度分数以确认识别是否准确。
通过将 OCR 处理集中在特定区域,可以显著提高文本提取任务的速度和准确性。 这种有针对性的方法对于在您的 .NET 应用程序中构建高效的文档处理工作流至关重要。
常见问题
如何使用C#在图像中定义特定OCR区域?
要在C#中定义特定的OCR区域,可以通过指定x/y坐标、宽度和高度来使用IronOCR的Rectangle对象。然后将此矩形传递给LoadImage方法以进行有针对性的OCR处理。
为什么定义特定的OCR区域是有利的?
指定特定的OCR区域有助于提高精度和性能,专注于图像的相关部分,防止提取无关文本并减少错误。
在C#中执行特定图像区域的OCR需要什么库?
您将需要名为IronOCR和IronSoftware.Drawing的库。IronOCR通过NuGet安装,允许您在预定义的图像区域执行OCR。
如何在C#应用程序中指定OCR的矩形坐标?
要指定矩形坐标,请使用图像编辑器确定左上角的x/y坐标,并以像素为单位设置所需区域的宽度和高度。
可以使用IronOCR从图像中的多个区域提取文本吗?
是的,IronOCR允许您定义和处理同一图像中的多个矩形区域,非常适合处理带有多个字段的表单。
可以在输入图像上可视化OCR区域吗?
是的,您可以使用StampCropRectangleAndSaveAs方法在输入图像上绘制指定的矩形,从而实现OCR区域的可视化,帮助进行坐标验证。
哪些应用程序中特定区域的OCR特别有用?
区域OCR对于表单处理、发票扫描、车牌识别、身份文件分析、以及从截图中的UI元素提取文本非常有用。
在文档处理中使用区域OCR的一些最佳实践是什么?
最佳实践包括在文本周围添加填充、使用示例图像进行测试、处理文档的微小变化、使用多线程优化性能,以及监控结果的置信度评分。
如何提高IronOCR区域扫描的准确性?
通过使用高分辨率图像、确保正确的文本方向、应用对比度和清晰度滤镜以及可视化验证目标区域来提高准确性。
IronOCR可以与不同的图像格式一起使用吗?
可以,IronOCR支持各种图像格式,包括JPG、PNG、GIF、TIFF和BMP,使其能够适应不同的扫描和处理需求。
Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。