如何在 C# 中使用计算机视觉查找文本
IronOCR 使用 OpenCV 计算机视觉技术在 OCR 处理之前自动检测图像中的文本区域。 通过将 Tesseract 识别功能仅集中在已识别的文本区域,提高了对嘈杂、多区域或扭曲文本的准确性,与处理整幅图像相比,提取效果显著增强。
快速开始:检测并OCR主要文本区域
此示例演示了即时文本提取:加载一张图片,使用 IronOCR 的计算机视觉功能通过 FindTextRegion() 自动检测主要文本区域,然后运行 .Read(...) 提取一行文本。
-如何在 C# 中进行车牌 OCR 识别(教程) -如何在 C# 教程中从发票中获取文本 -如何使用 C# 从屏幕截图中提取文本 -如何在 C# 中进行 OCR 字幕识别(教程)
最小工作流程(5 个步骤)
- 下载 C# 库,以便在计算机视觉中使用 OCR。
- 利用
FindTextRegion方法自动检测文本区域 - 检查使用
StampCropRectangleAndSaveAs方法检测到的文本区域。 - 使用计算机视觉,通过
FindMultipleTextRegions方法,基于文本区域将原始图像分割成多个图像。 - 使用
GetTextRegions方法获取检测到文本的裁剪区域列表。
如何通过 NuGet 软件包安装 IronOcr.ComputerVision?
IronOCR 中用于执行计算机视觉的 OpenCV 方法在常规的 IronOCR NuGet 包中可见。 有关详细的安装指导,请参阅我们的 NuGet 安装指南。
为什么 IronOCR 需要单独的计算机视觉软件包?
使用这些方法需要通过 NuGet 将 IronOcr.ComputerVision 安装到解决方案中。 如果您尚未安装该软件,系统会提示您下载。 计算机视觉功能利用 OpenCV 算法大大提高了文本检测的准确性,这与我们的 牌照识别和 护照扫描功能中使用的技术类似。
我应该安装哪个特定平台的软件包?
- Windows:
IronOcr.ComputerVision.Windows- 请参阅我们的 Windows 设置指南 - Linux:
IronOcr.ComputerVision.Linux- 请查看我们的 Linux 安装教程 - macOS:
IronOcr.ComputerVision.MacOS- 请查阅我们的 macOS 设置指南 - macOS ARM:
IronOcr.ComputerVision.MacOS.ARM
如何使用软件包管理器控制台进行安装?
使用 NuGet 程序包管理器安装,或者将以下内容粘贴到程序包管理器控制台中:
Install-Package IronOcr.ComputerVision.Windows
这提供了使用 IronOCR 计算机视觉与我们的模型文件所需的程序集。
IronOCR 中提供了哪些计算机视觉方法?
本教程后面还包含代码示例。 以下是目前可用方法的总体概述:
| 方法 | 解释 |
|---|---|
FindTextRegion |
检测包含文本元素的区域,并指示 Tesseract 只在检测到文本的区域内搜索文本。 |
FindMultipleTextRegions |
检测包含文本元素的区域,并根据文本区域将页面分割成单独的图像。 |
GetTextRegions |
Scans the image and returns a list of text regions as List. |
如何使用 FindTextRegion 检测文本区域?
FindTextRegion 利用计算机视觉技术,检测 OcrInput 对象中每一页包含文本元素的区域。 在处理带有零散文本的图像时,或者需要通过只关注包含文本的区域来提高性能时,这种方法尤其有用。
FindTextRegion 的基本用法是什么?
: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
IronOcr 2025.6.x 中已弃用,且不接受自定义参数。如何自定义 FindTextRegion 参数?
使用自定义参数调用此方法可对文本检测进行微调。 这些参数的作用类似于我们的 图像过滤器配置:
: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
FindTextRegion 在实践中是什么样的?
在本例中,我使用下图来说明一种需要裁剪包含文本区域的方法,但输入图像的文本位置可能会有所不同。 我使用 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)
如何调试和验证文本区域检测?
该代码有两个输出。 第一个是由 .png 保存的 StampCropRectangleAndSaveAs 文件,用于调试。 我们的调试高亮文本指南也涵盖了这一技术。 我们可以看到 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 用于多个文本区域?
FindMultipleTextRegions 会获取 OcrInput 对象的所有页面,并利用计算机视觉技术检测包含文本元素的区域,随后根据文本区域将输入内容分割为独立的图像。 这对于处理具有多个不同文本区域的文档特别有用,类似于我们的在文档中读取表格功能:
FindMultipleTextRegions 的基本用法是什么?
: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
FindMultipleTextRegions 方法不再支持自定义参数。如何自定义 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
如何使用 FindMultipleTextRegions 处理单个页面?
FindMultipleTextRegions 的另一个重载方法接受一个 OCR 页面作为参数,并返回一个 OCR 页面列表,其中每个页面对应该页面上的一个文本区域。 这种方法有助于处理复杂的布局,类似于我们的多页 TIFF 处理指南中描述的技术:
: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()
如何使用 GetTextRegions 获取文本区域坐标?
GetTextRegions 返回页面上检测到文本的裁剪区域列表。 当您需要文本区域的坐标以便进一步处理或实施自定义 OCR 工作流程时,这种方法尤其有用。 有关使用结果的更多详细信息,请参阅我们的 OcrResult 类文档:
何时应使用 GetTextRegions 而不是 FindTextRegion?
/* :path=/static-assets/ocr/content-code-examples/how-to/computer-vision-gettextregions.cs */
using IronOcr;
using IronSoftware.Drawing;
using System;
using System.Collections.Generic;
using System.Linq;
// Create a new IronTesseract object for OCR
var ocr = new IronTesseract();
// Load an image into OcrInput
using var input = new OcrInput();
input.LoadImage("/path/file.png");
// Get the first page from the input
var firstPage = input.GetPages().First();
// Get all text regions detected on this page
List<Rectangle> textRegions = firstPage.GetTextRegions();
// Display information about each detected region
Console.WriteLine($"Found {textRegions.Count} text regions:");
foreach (var region in textRegions)
{
Console.WriteLine($"Region at X:{region.X}, Y:{region.Y}, Width:{region.Width}, Height:{region.Height}");
}
// You can also process each region individually
foreach (var region in textRegions)
{
var regionResult = ocr.Read(input, region);
Console.WriteLine($"Text in region: {regionResult.Text}");
}
/* :path=/static-assets/ocr/content-code-examples/how-to/computer-vision-gettextregions.cs */
using IronOcr;
using IronSoftware.Drawing;
using System;
using System.Collections.Generic;
using System.Linq;
// Create a new IronTesseract object for OCR
var ocr = new IronTesseract();
// Load an image into OcrInput
using var input = new OcrInput();
input.LoadImage("/path/file.png");
// Get the first page from the input
var firstPage = input.GetPages().First();
// Get all text regions detected on this page
List<Rectangle> textRegions = firstPage.GetTextRegions();
// Display information about each detected region
Console.WriteLine($"Found {textRegions.Count} text regions:");
foreach (var region in textRegions)
{
Console.WriteLine($"Region at X:{region.X}, Y:{region.Y}, Width:{region.Width}, Height:{region.Height}");
}
// You can also process each region individually
foreach (var region in textRegions)
{
var regionResult = ocr.Read(input, region);
Console.WriteLine($"Text in region: {regionResult.Text}");
}
Imports IronOcr
Imports IronSoftware.Drawing
Imports System
Imports System.Collections.Generic
Imports System.Linq
' Create a new IronTesseract object for OCR
Dim ocr As New IronTesseract()
' Load an image into OcrInput
Using input As New OcrInput()
input.LoadImage("/path/file.png")
' Get the first page from the input
Dim firstPage = input.GetPages().First()
' Get all text regions detected on this page
Dim textRegions As List(Of Rectangle) = firstPage.GetTextRegions()
' Display information about each detected region
Console.WriteLine($"Found {textRegions.Count} text regions:")
For Each region In textRegions
Console.WriteLine($"Region at X:{region.X}, Y:{region.Y}, Width:{region.Width}, Height:{region.Height}")
Next
' You can also process each region individually
For Each region In textRegions
Dim regionResult = ocr.Read(input, region)
Console.WriteLine($"Text in region: {regionResult.Text}")
Next
End Using
计算机视觉在 OCR 中的常见用例有哪些?
计算机视觉可在具有挑战性的场景中大大提高 OCR 的准确性。 以下是实际应用:
1.文档布局分析:自动识别和处理复杂文档的不同部分。 尤其适用于 扫描文件。 2.多栏文本:为报纸或杂志分栏并独立阅读。 使用多线程处理以获得更快的结果。 3.混合内容:区分文档中的文本区域和图形。 在处理内嵌文本的照片时有帮助。 4.性能优化:只对包含文本的区域进行 OCR 处理。 请参阅我们的快速 OCR 配置指南。 5.质量控制:在完全 OCR 处理之前验证文本检测。 我们的进度跟踪功能可监控每个阶段。
通过正确的设置和输入文件,OCR 可以实现接近人类的阅读能力。 为获得最佳效果,请将计算机视觉与我们的图像优化过滤器相结合,以实现最佳的 OCR 准确性。 在处理低质量图像时,我们的修复低质量扫描指南提供了宝贵的预处理技术。
高级计算机视觉技术
对于希望提高 OCR 精确度的开发人员来说,可以考虑这些先进的方法:
- 定制培训:使用我们的定制语言文件指南为专用字体培训 OCR 引擎。
- 多语言支持:使用我们的多语言功能处理多语言文档
- 条形码集成:使用我们的具有条形码阅读功能的OCR,将文本识别与条形码阅读结合起来
常见问题解答
什么是 OCR 中的计算机视觉?
IronOCR 中的计算机视觉使用 OpenCV 算法在 OCR 处理之前自动检测图像中的文本区域。这样,Tesseract 只对识别出的文本区域进行识别,而不是处理整个图像,从而大大提高了噪声、多区域或扭曲文本的准确性。
如何在 C# 中快速实现计算机视觉 OCR?
IronOCR 只需一行代码即可实现计算机视觉 OCR:使用 IronTesseract 的 FindTextRegion() 方法自动检测主要文本区域,然后运行 .Read() 立即提取文本。
为什么需要安装单独的计算机视觉软件包?
IronOCR 需要单独的 IronOcr.ComputerVision NuGet 包,因为计算机视觉功能利用了 OpenCV 算法。这些算法大大提高了文本检测的准确性,对于车牌识别和护照扫描等功能至关重要。
我应该安装哪个特定平台的计算机视觉软件包?
IronOCR 提供特定平台的软件包:适用于 Windows 系统的 IronOcr.ComputerVision.Windows、适用于 Linux 发行版的 IronOcr.ComputerVision.Linux,以及适用于 macOS 环境的 IronOcr.ComputerVision.MacOS。
如何检测图像中的多个文本区域?
IronOCR 提供了 FindMultipleTextRegions 方法,可根据检测到的文本区域将原始图像分离成多个图像。您还可以使用 GetTextRegions 方法检索检测到文本的裁剪区域列表。
我能否在处理前验证已检测到哪些文本区域?
是的,IronOCR 包含 StampCropRectangleAndSaveAs 方法,允许您在运行实际 OCR 流程之前检查计算机视觉算法检测到了哪些文本区域。
IronOCR可以集成到现有应用程序中吗?
IronOCR设计为易于使用C#集成到现有应用程序中,允许开发人员以最小的努力为他们的软件添加OCR功能。
使用IronOCR进行文档管理有什么好处?
使用IronOCR进行文档管理可以通过将扫描的文档转换为可搜索和可编辑文本来简化工作流程,减少手动数据输入的需要,提高文档可访问性。
IronOCR如何提高数据准确性?
IronOCR通过其高级识别算法和图像校正功能提高数据准确性,确保文本提取过程既可靠又精确。
IronOCR 有免费试用版吗?
是的,Iron Software 提供IronOCR 的免费试用,使用户在做出购买决定之前可以测试其功能和能力。

