如何使用计算机视觉在 C# 中找到文本

如何在 C# 中使用计算机视觉查找文本

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

IronOCR 利用 OpenCV 的计算机视觉技术来检测图像中存在文本的区域。 这对于包含大量噪声的图像、文本分布在许多不同位置的图像以及文本扭曲的图像非常有用。 IronOCR 利用计算机视觉技术确定文本区域的位置,然后使用 Tesseract 尝试读取这些区域。

快速入门:检测和 OCR 主文本区域

这个例子展示了入门有多么容易:只需加载一张图片,让 IronOCR 的计算机视觉功能使用FindTextRegion()自动找到主要文本区域,然后立即运行.Read(...)来提取文本。 只需一行简单的代码即可将图像转换为 OCR 输出。

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

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

    PM > Install-Package IronOcr

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

    using var result = new IronTesseract().Read(new OcrInput().LoadImage("image.png").FindTextRegion());
  3. 部署到您的生产环境中进行测试

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

-如何在 C# 中进行车牌 OCR 识别(教程) -如何在 C# 教程中从发票中获取文本 -如何使用 C# 从屏幕截图中提取文本 -如何在 C# 中进行 OCR 字幕识别(教程)

IronOCR.计算机视觉通过 NuGet 包安装

IronOCR 中用于执行计算机视觉的 OpenCV 方法在常规的 IronOCR NuGet 包中可见。

使用这些方法需要将IronOcr.ComputerVision通过 NuGet 安装到解决方案中。 如果您尚未安装该软件,系统会提示您下载。

  • Windows: IronOcr.ComputerVision.Windows
  • Linux: IronOcr.ComputerVision.Linux
  • macOS: IronOcr.ComputerVision.MacOS
  • macOS ARM: IronOcr.ComputerVision.MacOS.ARM

使用 NuGet 程序包管理器安装,或者将以下内容粘贴到程序包管理器控制台中:

Install-Package IronOcr.ComputerVision.Windows

这将提供必要的组件,以便将 IronOCR 计算机视觉技术与我们的模型文件一起使用。

功能和 API

本教程后面会提供代码示例。 以下是目前可用方法的概述:

方法 解释
查找文本区域 检测包含文本元素的区域,并指示 Tesseract 只在检测到文本的区域内搜索文本。
查找多个文本区域 检测包含文本元素的区域,并根据文本区域将页面分割成单独的图像。
获取文本区域 Scans the image and returns a list of text regions as `List`.

查找文本区域

FindTextRegion函数将利用计算机视觉来检测 OcrInput 对象每一页上包含文本元素的区域。

:path=/static-assets/ocr/content-code-examples/how-to/computer-vision-findtextregion-1.cs
using IronOcr;

var ocr = new IronTesseract();
using var input = new OcrInput();
input.LoadImage("/path/file.png");

input.FindTextRegion();
OcrResult result = ocr.Read(input);
string resultText = result.Text;
Imports IronOcr

Private ocr = New IronTesseract()
Private input = New OcrInput()
input.LoadImage("/path/file.png")

input.FindTextRegion()
Dim result As OcrResult = ocr.Read(input)
Dim resultText As String = result.Text
$vbLabelText   $csharpLabel

注意 此方法重载在IronOcr 2025.6.x中已弃用,并且不再接受自定义参数。

可以选择性地使用自定义参数调用:

:path=/static-assets/ocr/content-code-examples/how-to/computer-vision-findtextregion-2.cs
using IronOcr;

var ocr = new IronTesseract();
using var input = new OcrInput();
input.LoadImage("/path/file.png");

input.FindTextRegion(Scale: 2.0, DilationAmount: 20, Binarize: true, Invert: true);
OcrResult result = ocr.Read(input);
string resultText = result.Text;
Imports IronOcr

Private ocr = New IronTesseract()
Private input = New OcrInput()
input.LoadImage("/path/file.png")

input.FindTextRegion(Scale:= 2.0, DilationAmount:= 20, Binarize:= True, Invert:= True)
Dim result As OcrResult = ocr.Read(input)
Dim resultText As String = result.Text
$vbLabelText   $csharpLabel

在这个例子中,我将使用以下图像来演示我正在编写的一个方法,该方法需要裁剪到包含文本的区域,但输入图像中的文本位置可能会有所不同。 在这种情况下,我可以使用 FindTextRegion 将扫描范围缩小到计算机视觉检测到文本的区域。 这是一张示例图片:

图片与文字
:path=/static-assets/ocr/content-code-examples/how-to/computer-vision-findtextregion-3.cs
using IronOcr;
using IronSoftware.Drawing;
using System;
using System.Linq;

var ocr = new IronTesseract();
using var input = new OcrInput();
input.LoadImage("wh-words-sign.jpg");

// Find the text region using Computer Vision
Rectangle textCropArea = input.GetPages().First().FindTextRegion();

// For debugging and demonstration purposes, lets see what region it found:
input.StampCropRectangleAndSaveAs(textCropArea, Color.Red, "image_text_area", AnyBitmap.ImageFormat.Png);

// Looks good, so let us apply this region to hasten the read:
var ocrResult = ocr.Read("wh-words-sign.jpg", textCropArea);
Console.WriteLine(ocrResult.Text);
Imports IronOcr
Imports IronSoftware.Drawing
Imports System
Imports System.Linq

Private ocr = New IronTesseract()
Private input = New OcrInput()
input.LoadImage("wh-words-sign.jpg")

' Find the text region using Computer Vision
Dim textCropArea As Rectangle = input.GetPages().First().FindTextRegion()

' For debugging and demonstration purposes, lets see what region it found:
input.StampCropRectangleAndSaveAs(textCropArea, Color.Red, "image_text_area", AnyBitmap.ImageFormat.Png)

' Looks good, so let us apply this region to hasten the read:
Dim ocrResult = ocr.Read("wh-words-sign.jpg", textCropArea)
Console.WriteLine(ocrResult.Text)
$vbLabelText   $csharpLabel

现在这段代码有两个输出,第一个输出是由StampCropRectangleAndSaveAs保存的.png文件,用于调试。 我们可以看到IronCV(计算机视觉)认为文本的位置:

图像中文本区域高亮显示

看起来不错。 第二个输出是文本本身,内容如下:

IRONSOFTWARE

50,000+

Developers in our active community

10,777,061 19,313
NuGet downloads Support tickets resolved
50%+ 80%+
Engineering Team growth Support Team growth
$25,000+

Raised with #TEAMSEAS to clean our beaches & waterways

查找多个文本区域

FindMultipleTextRegions函数会获取OcrInput对象的所有页面,并使用计算机视觉来检测包含文本元素的区域,然后根据文本区域将输入分割成单独的图像:

:path=/static-assets/ocr/content-code-examples/how-to/computer-vision-findmultipletextregions-1.cs
using IronOcr;

var ocr = new IronTesseract();
using var input = new OcrInput();
input.LoadImage("/path/file.png");

input.FindMultipleTextRegions();
OcrResult result = ocr.Read(input);
string resultText = result.Text;
Imports IronOcr

Private ocr = New IronTesseract()
Private input = New OcrInput()
input.LoadImage("/path/file.png")

input.FindMultipleTextRegions()
Dim result As OcrResult = ocr.Read(input)
Dim resultText As String = result.Text
$vbLabelText   $csharpLabel

从 IronOcr v2025.6.x 版本开始, FindMultipleTextRegions方法不再支持自定义参数。

可以选择性地使用自定义参数调用:

:path=/static-assets/ocr/content-code-examples/how-to/computer-vision-findmultipletextregions-2.cs
using IronOcr;

var ocr = new IronTesseract();
using var input = new OcrInput();
input.LoadImage("/path/file.png");

input.FindMultipleTextRegions(Scale: 2.0, DilationAmount: -1, Binarize: true, Invert: false);
OcrResult result = ocr.Read(input);
string resultText = result.Text;
Imports IronOcr

Private ocr = New IronTesseract()
Private input = New OcrInput()
input.LoadImage("/path/file.png")

input.FindMultipleTextRegions(Scale:= 2.0, DilationAmount:= -1, Binarize:= True, Invert:= False)
Dim result As OcrResult = ocr.Read(input)
Dim resultText As String = result.Text
$vbLabelText   $csharpLabel

FindMultipleTextRegions的另一个重载方法接受一个 OCR 页面,并返回一个 OCR 页面列表,每个页面对应页面上的一个文本区域:

:path=/static-assets/ocr/content-code-examples/how-to/computer-vision-findmultipletextregions-3.cs
using IronOcr;
using System.Collections.Generic;
using System.Linq;

int pageIndex = 0;
using var input = new OcrInput();
input.LoadImage("/path/file.png");

var selectedPage = input.GetPages().ElementAt(pageIndex);
List<OcrInputPage> textRegionsOnPage = selectedPage.FindMultipleTextRegions();
Imports IronOcr
Imports System.Collections.Generic
Imports System.Linq

Private pageIndex As Integer = 0
Private input = New OcrInput()
input.LoadImage("/path/file.png")

Dim selectedPage = input.GetPages().ElementAt(pageIndex)
Dim textRegionsOnPage As List(Of OcrInputPage) = selectedPage.FindMultipleTextRegions()
$vbLabelText   $csharpLabel

获取文本区域

GetTextRegions函数会返回页面中检测到文本的裁剪区域列表:

:path=/static-assets/ocr/content-code-examples/how-to/computer-vision-gettextregions.cs
using IronOcr;
using IronSoftware.Drawing;
using System.Collections.Generic;
using System.Linq;

int pageIndex = 0;
using var input = new OcrInput();
input.LoadImage("/path/file.png");

var selectedPage = input.GetPages().ElementAt(pageIndex);
// List<Rectangle> regions = selectedPage.GetTextRegions();
Imports IronOcr
Imports IronSoftware.Drawing
Imports System.Collections.Generic
Imports System.Linq

Private pageIndex As Integer = 0
Private input = New OcrInput()
input.LoadImage("/path/file.png")

Dim selectedPage = input.GetPages().ElementAt(pageIndex)
' List<Rectangle> regions = selectedPage.GetTextRegions();
$vbLabelText   $csharpLabel

具体用例指南

只要设置和输入文件合适,OCR 就能成为非常强大的工具。 它几乎可以完美地模仿人类的阅读能力。

常见问题解答

我如何利用计算机视觉进行图像中的文本检测?

IronOCR可以通过与OpenCV集成来增强图像中的文本检测。像FindTextRegionFindMultipleTextRegions这样的方法允许您有效地定位和操作文本区域。

在不同的操作系统上安装IronOCR的步骤是什么?

要在不同操作系统上使用IronOCR,可以通过NuGet安装IronOCR.ComputerVision包。对于Windows,使用命令Install-Package IronOcr.ComputerVision.Windows,类似的软件包也可以在Linux和macOS上使用。

FindTextRegion方法提供了什么功能?

IronOCR中的FindTextRegion方法识别图像中存在文本的区域,让Tesseract仅在这些指定区域内搜索文本,提高了OCR准确性。

自定义参数如何提高文本区域检测?

您可以通过FindTextRegion方法使用自定义参数来优化IronOCR的文本区域检测,例如设置最小文本高度和允许子区域,以更精确地标识图像中的文本区域。

检测图像中的多个文本区域有什么好处?

使用IronOCR中的FindMultipleTextRegions方法检测多个文本区域有助于根据文本将图像分成多个部分,这对于处理包含多个文本块的文档(如发票和字幕)尤其有用。

如何从图像中获取检测到的文本区域?

IronOCR中的GetTextRegions方法允许您检索以CropRectangle形式存在的列表,文本被检测到在图像内,从而能够进一步处理或操作这些文本区域。

IronOCR的计算机视觉功能的关键特点是什么?

IronOCR的计算机视觉功能包括通过FindTextRegionFindMultipleTextRegions方法进行文本区域检测,可定制的OCR设置,以及支持多个操作系统以提高文本检测准确性。

IronOCR如何处理具有多个文本区域的图像?

IronOCR使用FindMultipleTextRegions方法通过将图像分成基于检测到的文本区域的多个图像来处理图像,使每个文本区域的详细分析和操作成为可能。

在哪里可以找到使用IronOCR中的计算机视觉方法的代码示例?

教程提供了演示如何使用IronOCR中的FindTextRegionFindMultipleTextRegions方法的代码示例,说明了读取车牌或处理发票等实际应用。

使用C#的IronOCR的计算机视觉功能需要什么?

要在C#中使用IronOCR的计算机视觉功能,必须通过NuGet安装IronOcr.ComputerVision包,并在项目中包含必要的命名空间以访问文本检测方法。

Curtis Chau
技术作家

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

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

准备开始了吗?
Nuget 下载 5,167,857 | Version: 2025.11 刚刚发布