如何使用電腦視覺在 C# 中尋找文本

How to use Computer Vision to Find Text

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

IronOCR 利用 OpenCV 使用计算机视觉检测图像中存在文本的区域。 这对于包含大量噪音的图像、文本分布在多个位置的图像以及文本扭曲的图像非常有用。 在 IronOCR 中使用電腦視覺將確定文字區域所在位置,然後使用 Tesseract 嘗試讀取這些區域。

快速開始:檢測並光學字符識別主要文字區域

此範例顯示了您可以多容易地入門:只需載入一張圖片,讓 IronOCR 的電腦視覺自動尋找主要文字區域使用 FindTextRegion(),然後立即執行 .Read(...) 提取文字。 從圖片到光學字符識別輸出只需要一行簡單的代碼。

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronOCR with NuGet Package Manager

    PM > Install-Package IronOcr

  2. Copy and run this code snippet.

    using var result = new IronTesseract().Read(new OcrInput().LoadImage("image.png").FindTextRegion());
  3. Deploy to test on your live environment

    Start using IronOCR in your project today with a free trial
    arrow pointer
class="hsg-featured-snippet">

最小化工作流程(5 步)

  1. 下載 C# 庫以使用電腦視覺進行光學字符識別
  2. 利用 FindTextRegion 方法自動檢測文字區域
  3. 檢查哪個文字區域被 StampCropRectangleAndSaveAs 方法檢測到
  4. 使用電腦視覺根據文字區域將原始圖片分為多張圖片,使用 FindMultipleTextRegions 方法
  5. 使用 GetTextRegions 方法獲取檢測到文字的裁剪區域列表

透過 NuGet 套件安裝 IronOCR.ComputerVision

在常規的 IronOCR NuGet 套件中可以看到執行電腦視覺的 OpenCV 方法。

使用這些方法需要將 IronOcr.ComputerVision 安裝到解決方案中。 如果未安裝,系統將提示您下載。

  • 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

此教程的下方包含程式碼範例。 這裡是目前可用方法的概述:

方法 說明
FindTextRegion 檢測包含文字元素的區域並指示 Tesseract 只在檢測到的區域內搜尋文字。
FindMultipleTextRegions 檢測包含文字元素的區域並根據文字區域將頁面分成多張圖片。
GetTextRegions 掃描圖片並以 `List` 的形式返回文字區域列表。

FindTextRegion

使用 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 將掃描範圍縮小至電腦視覺檢測到文字的區域。 這是一個示例圖片:

class="content-img-align-center">
class="center-image-wrapper"> Image with Text
: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(電腦視覺)認為文字的位置:

class="content-img-align-center">
class="center-image-wrapper"> Image with Text Area Highlighted

看起來不錯。 現在第二個輸出是文字本身,它是:

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 物件的所有頁面運用電腦視覺來檢測包含文字的區域,並根據文字區域將輸入拆分為多張圖片:

: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

使用 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

特定使用案例指南

使用適當的設置和輸入文件,光學字符識別可以是一個非常強大的工具。 它幾乎可以完美地模仿人類的閱讀能力。

常見問題解答

如何利用電腦視覺技術進行圖像中的文字檢測?

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 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

準備好開始了嗎?
Nuget 下載 5,044,537 | 版本: 2025.11 剛剛發布