如何在 C# 中定义图像特定区域进行 OCR识别与图片转文字
要在 C# 中从图像的特定区域提取图片文字,请使用 IronOCR 的 Rectangle 对象,通过指定 y 坐标、width 和 height 来定义确切区域,然后将其传递给 LoadImage 方法进行目标 OCR识别处理。
快速入门:从特定图像区域提取文本
-
使用 NuGet 包管理器安装 https://www.nuget.org/packages/IronOcr
PM > Install-Package IronOcr -
复制并运行这段代码。
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); -
部署到您的生产环境中进行测试
通过免费试用立即在您的项目中开始使用IronOCR
通常情况下,你只需要从图像的一小部分中提取文本,例如发票上的总金额或表单中的特定字段。 扫描整个文档效率低下,并且可能因捕获无关文本而引入错误。
IronOCR 允许您通过指定要扫描的精确矩形区域来提高精度、性能和准确性。 本指南提供了逐步演练,指导您如何定义特定的 OCR 区域、从中提取文本,并通过视觉方式验证您的坐标是否适用于您的 OCR 任务。
IronOCR 入门指南
如何定义图像中的特定OCR区域
- 下载用于定义 OCR 区域的 C# 库
- 实例化 OCR 引擎
- 用矩形框标出OCR区域。
- 使用`LoadImage`加载图像以及定义的矩形区域
- 访问 **`OcrResult`** 属性以查看和操作提取的数据
如何在特定区域执行 OCR?
要定义特定的 OCR 区域,您可以从 IronSoftware.Drawing 命名空间创建一个 Rectangle 对象。 此对象需要四个值:width 和 height,全部以像素为单位。 (y)坐标表示所需区域的左上角。
当您使用 LoadImage 加载图像时,您需要将此 Rectangle 作为第二个参数传递。 IronOCR 随后会将 OCR 过程限制在该边界框内的像素范围内。
区域 OCR 在处理结构化文档(如 发票、扫描表格或 身份文档)时特别有用,因为在这些文档中,特定信息总是出现在可预测的位置。 通过将 OCR 限制在相关区域,可以显著提高处理速度,减少无关文本的误报。
要找到您的 Rectangle 的坐标,您可以使用像 MS Paint 这样的简单图像编辑器。 打开输入图像,将鼠标悬停在指定区域的左上角和右下角,并记下(y)像素坐标。 然后您可以计算矩形的属性:(x1, y1, width, height),其中 width = x1,并且 height = y1。
测试时应使用什么图像?
我们将使用一张包含三个段落的示例图片。我们的目标是只提取第二个段落,忽略其余文本。 这是一个常见的场景,您需要从一个较大的文档中提取特定的字段或章节。
如何在代码中实现区域 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
对于更复杂的情况,您可以在同一图像中定义多个区域。 这在处理具有多个字段的表单或文档中的表格时尤其有用:
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
我可以期待什么结果?
从控制台输出可以看出,只有第二个段落被 OCR 处理了。 这种有针对性的方法可确保图像其他部分的无关文本不会干扰您的结果。
区域 OCR 的准确性取决于多个因素:
如何验证我的坐标是否正确?
为了确保您已为输入图像选择了正确的坐标,您可以可视化您定义的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
可视化看起来像什么?
浅蓝色矩形框表明我们已经正确地提取出第二个段落进行处理。
何时应使用区域 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 对于发票、扫描表格和身份证明文件等结构化文件特别有效,因为这些文件中的特定信息始终出现在相同的位置。

