如何用计算机视觉在C#中查找文本
IronOCR 使用 OpenCV 计算机视觉技术,在 OCR 处理之前自动检测图像中的文本区域。Tesseract 只识别检测到的区域。与处理整张图像相比,这样可以显著提高含噪点、具有多个文本区域或文字变形的图像的提取准确率。
快速入门:检测主要文本区域并执行 OCR此示例演示如何快速提取文本。加载图像后,使用 IronOCR 的计算机视觉方法 FindTextRegion() 自动检测主要文本区域,再调用 .Read(...),用一行代码提取文本。
-
1使用 IronOCR 安装 NuGet Package Manager
-
2复制并运行这段代码。
using var result = new IronTesseract().Read(new OcrInput().LoadImage("image.png").FindTextRegion());Using result = New IronTesseract().Read(New OcrInput().LoadImage("image.png").FindTextRegion())Using result = New IronTesseract().Read(New OcrInput().LoadImage("image.png").FindTextRegion()) -
3部署到您的生产环境中进行测试
今天就用免费试用在您的项目中开始使用IronOCR
最小工作流程(5 个步骤)
- 下载C#库以使用OCR和计算机视觉
- 利用
FindTextRegion方法自动检测文本区域 - 使用
StampCropRectangleAndSaveAs方法检查检测到的文本区域。 - 调用
FindMultipleTextRegions方法,利用计算机视觉按文本区域将原始图像拆分为多张图像。 - 使用
GetTextRegions方法获取检测到文本的裁剪区域列表。
如何通过NuGet包安装IronOCR.ComputerVision?
可以在标准的 IronOCR NuGet 包中找到用于计算机视觉的 OpenCV 方法。有关详细步骤,请参阅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 程序包管理器安装,或者将以下内容粘贴到程序包管理器控制台中:
安装后即可获得将 IronOCR 计算机视觉功能与模型文件配合使用所需的程序集。
IronOCR中有哪些计算机视觉方法可用?
本教程后面还包含代码示例。 以下是目前可用方法的总体概述:
| 方法 | 说明 |
|---|---|
FindTextRegion | 检测包含文本元素的区域,并指示 Tesseract 只在检测到文本的区域内搜索文本。 |
FindMultipleTextRegions | 检测包含文本元素的区域,并根据文本区域将页面分割成单独的图像。 |
GetTextRegions | 扫描图像并返回文本区域的列表,格式为List<Rectangle>。 |
如何使用FindTextRegion检测文本区域?
FindTextRegion 使用计算机视觉检测 OcrInput 对象每页的文本区域。图像中的文本分布较分散,或需要通过缩小识别范围提高性能时,此方法尤其有用。
FindTextRegion的基本用法是什么?
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;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.TextImports 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.TextIronOcr 2025.6.x中已弃用,并且不接受自定义参数。FindTextRegion 的实际用法是什么?
下面用一张示例图像演示如何裁剪文本区域。不同输入图像中的文字位置可能不同,因此可以调用 FindTextRegion,将扫描范围缩小到计算机视觉检测到文本的区域。这与内容区域和裁剪区域教程中介绍的方法类似。示例图像如下:

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);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)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)如何调试和验证文本区域检测?
代码生成两项输出。第一项是 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处理多个文本区域?
FindMultipleTextRegions 使用计算机视觉检测 OcrInput 对象所有页面中的文本区域,再按文本区域将输入拆分成多张图像。此方法尤其适合处理包含多个独立文本区域的文档,与读取文档中的表格所用的方法类似:
FindMultipleTextRegions的基本用法是什么?
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;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.TextImports 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.TextFindMultipleTextRegions方法不再支持自定义参数。如何使用FindMultipleTextRegions处理单个页面?
FindMultipleTextRegions 的另一个重载接受一个 OCR 页面,并返回一个 OCR 页面列表,其中每个页面对应原页面上的一个文本区域。此方法适合处理复杂版式,与多页 TIFF 处理指南中介绍的技术类似:
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();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()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("/path/file.png", 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("/path/file.png", 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 = New IronTesseract()
' Load an image into OcrInput
Using input = 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("/path/file.png", region)
Console.WriteLine($"Text in region: {regionResult.Text}")
Next
End UsingImports IronOcr
Imports IronSoftware.Drawing
Imports System
Imports System.Collections.Generic
Imports System.Linq
' Create a new IronTesseract object for OCR
Dim ocr = New IronTesseract()
' Load an image into OcrInput
Using input = 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("/path/file.png", region)
Console.WriteLine($"Text in region: {regionResult.Text}")
Next
End Using计算机视觉在OCR中的常见用例是什么?
计算机视觉可在具有挑战性的场景中大大提高 OCR 的准确性。 以下是实际应用:
- 文档布局分析:自动识别和处理复杂文档的不同部分。 在处理扫描文档时特别有用。
- 多列文本:分别识别报纸或杂志中的各栏文字。使用多线程处理可加快识别速度。
- 混合内容:区分文档中的文本区域和图形。 在处理嵌入文本的照片时很有帮助。
- 性能优化:仅对检测到文本的区域执行 OCR。请参阅快速 OCR 配置指南。
- 质量控制:在全面OCR处理之前验证文本检测。 我们的进度跟踪功能监控每个阶段。
使用合适的设置和输入文件时,OCR 的识别效果可以接近人工阅读水平。将计算机视觉与图像优化滤镜结合使用,可进一步提高 OCR 准确率。处理低质量图像时,可参阅改善低质量扫描件的指南,了解预处理方法。
高级计算机视觉技术
如需进一步提高 OCR 准确率,可采用以下方法:
- 自定义训练:使用我们的自定义语言文件指南为特定字体训练OCR引擎
- 多语言支持:使用我们的多语言功能处理多语言文档
- 条形码集成:借助 OCR 的条形码读取功能,同时识别文本和条形码
常见问题
IronOCR中的FindTextRegion方法是什么?
IronOCR中的FindTextRegion方法使用计算机视觉来检测图像中包含文本元素的区域,使Tesseract OCR仅关注这些区域,从而提高从噪声或多区域图像中读取文本的准确性。
IronOCR如何利用计算机视觉提高文本检测的准确性?
IronOCR利用OpenCV计算机视觉算法,在OCR处理之前自动检测并聚焦于图像中的文本区域,从而显著提高文本检测的准确性。
IronOCR能否执行多个文本区域检测?
可以,IronOCR可以使用FindMultipleTextRegions方法,根据文本区域识别并将输入图像划分为独立区域,增强多文本块文档的处理效率。
IronOCR在文本检测过程中如何处理噪声或失真图像?
IronOCR使用FindTextRegion方法隔离噪声或失真图像中的文本区域,使Tesseract OCR仅专注于这些特定区域,从而提高读取准确性和性能。
IronOCR的计算机视觉包支持哪些平台?
IronOCR的计算机视觉包支持Windows、Linux、macOS和macOS ARM,并为每个平台提供特定的安装指南。
在使用IronOCR处理OCR任务时,计算机视觉的优势是什么?
计算机视觉通过让IronOCR自动检测并聚焦于图像中的文本区域,增强了OCR任务的执行效果,尤其是在处理复杂或低质量文档时提高了准确性。
IronOCR中的GetTextRegions方法有什么用途?
IronOCR中的GetTextRegions方法提供了检测到的文本区域坐标列表,便于进一步处理或实现自定义OCR工作流程。
IronOCR如何帮助处理多文本区域的文档?
IronOCR使用FindMultipleTextRegions方法自动检测并处理多文本区域的文档,通过将其分隔为独立的部分来提高OCR性能。
为什么IronOCR可能需要单独的计算机视觉包?
IronOCR的单独计算机视觉包可通过NuGet获得,用于利用由OpenCV算法驱动的高级文本检测功能,从而在标准能力之外提高OCR的准确性。
IronOCR中的计算机视觉支持哪些常见用途?
IronOCR中的计算机视觉可用于文档布局分析、多列文本识别、区分混合内容、性能优化及OCR流程中的质量控制。
Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。