如何使用計算機視覺在 C# 中尋找文本

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

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

IronOCR 使用 OpenCV 電腦視覺技術,在進行 OCR 處理前自動偵測影像中的文字區域。 這可提高雜訊、多區域或扭曲文字的精確度,方法是只將 Tesseract 識別功能集中在已識別的文字區域,與處理整個影像相比,可大幅提升擷取結果。

快速入門:檢測和 OCR 主文本區域

本範例示範立即擷取文字:載入一張圖片,使用 IronOcr 的電腦視覺功能,以 FindTextRegion() 自動偵測主要文字區域,然後執行 .Read(...) 來擷取一行文字。

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 字幕辨識(教學)

如何透過 NuGet 套件安裝 IronOCR.ComputerVision?

IronOCR 中用於執行電腦視覺的 OpenCV 方法在常規的 IronOCR NuGet 套件中可見。 如需詳細的安裝指南,請參閱我們的 NuGet 安裝指南

為什麼 IronOCR 需要獨立的電腦視覺套件?

使用這些方法需要在解決方案中安裝 IronOcr.ComputerVision 的 NuGet。 如果您尚未安裝軟體,系統會提示您下載。 電腦視覺功能利用 OpenCV 演算法,可大幅提升文字偵測的準確度,類似於我們的 牌照識別 護照掃描 功能中使用的技術。

我應該安裝哪個特定平台的套件?

  • 視窗: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 Computer Vision 與我們的模型檔案所需的程式集。

IronOCR 提供哪些電腦視覺方法?

程式碼範例包含在本教學的後面。 以下是目前可用方法的一般概述:

方法 解釋
尋找文字區域 偵測包含文字元素的區域,並指示 Tesseract 只在偵測到文字的區域內搜尋文字。
尋找多個文字區域 偵測包含文字元素的區域,並根據文字區域將頁面分割成單獨的圖像。
取得文字區域 Scans the image and returns a list of text regions as `List`.

如何使用 FindTextRegion 檢測文字區域?

<! -- 截圖示範如何使用 findtextregion 檢測文字區域? in IronPDF-->譯文 <!--說明:顯示逐步過程的截圖 -->

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
$vbLabelText   $csharpLabel

小心 這個方法重載目前在 IronOcr 2025.6.x 中已被廢棄,並且不接受自訂參數。
這個方法重載目前在 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
$vbLabelText   $csharpLabel

FindTextRegion 在實務中是什麼樣子?

在這個範例中,我使用下圖的方法,需要裁剪到包含文字的區域,但輸入的圖片在文字位置上可能會有所不同。 我使用 FindTextRegion 將掃描範圍縮小到 Computer Vision 已偵測到文字的區域。 此方法類似於我們的 內容區域與裁切區域教學中所使用的技巧。 這是一張範例圖片:

IronSoftware 2022 公司統計資料,顯示開發人員指標和業務績效資料
: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 檔案,用於除錯。 我們的 highlight texts for debugging 指南也涵蓋了這項技術。 我們可以看到 IronCV (Computer Vision) 檢測到文字的位置:

IronSoftware 2022 統計資料,紅色邊框顯示 FindTextRegion 文字偵測功能

偵測準確辨識文字區域。 第二個輸出是文字本身:

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
$vbLabelText   $csharpLabel

從 IronOcr v2025.6.x 版本開始, 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
$vbLabelText   $csharpLabel

如何使用 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()
$vbLabelText   $csharpLabel

如何使用 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
$vbLabelText   $csharpLabel

電腦視覺在 OCR 中的常見使用案例有哪些?

電腦視覺可在具挑戰性的情況下大幅提升 OCR 精確度。 以下是實際應用:

1.Document Layout Analysis:自動識別和處理複雜文件的不同部分。 尤其適用於 掃描文件

2.多欄文字:將報紙或雜誌的欄目分開並獨立閱讀。 使用多執行緒處理以獲得更快的結果。

3.混合內容:區分文件中的文字區域與圖形。 在處理內嵌文字的照片時有幫助。

4.效能最佳化:僅將 OCR 處理的重點放在包含文字的區域。 請參閱我們的 快速 OCR 配置指南

5.品質控制:在進行完整的 OCR 處理之前,先驗證文字偵測。 我們的進度追蹤功能會監控每個階段。

只要有正確的設定和輸入檔案,OCR 就能達到接近人類的閱讀能力。 為達到最佳效果,請結合電腦視覺與我們的 影像最佳化篩選器,以達到最佳的 OCR 精確度。 在處理低品質影像時,我們的 修正低品質掃描指南提供了寶貴的預處理技巧。

進階電腦視覺技術

對於想要突破 OCR 精確度邊界的開發人員而言,請考慮這些先進的方法:

常見問題解答

什麼是 OCR 中的電腦視覺,以及它如何改善文字擷取?

IronOCR 中的電腦視覺使用 OpenCV 演算法,在進行 OCR 處理前自動偵測影像中的文字區域。這可將 Tesseract 識別功能僅集中於已識別的文字區域,而非處理整個影像,因此可顯著提高雜訊、多區域或扭曲文字的準確度。

如何在 C# 中快速實作電腦視覺 OCR?

IronOCR 只需一行程式碼即可實現電腦視覺 OCR:使用 IronTesseract 的 FindTextRegion() 方法自動偵測主要文字區域,然後執行 .Read() 即可立即擷取文字。

為什麼我需要安裝獨立的 Computer Vision 套件?

IronOCR 需要獨立的 IronOcr.ComputerVision NuGet 套件,因為電腦視覺功能利用了 OpenCV 演算法。這些演算法可大幅提升文字偵測的精確度,對於車牌辨識和護照掃描等功能來說是不可或缺的。

我應該安裝哪個特定平台的電腦視覺套件?

IronOCR 提供特定平台的套件:適用於 Windows 系統的 IronOcr.ComputerVision.Windows、適用於 Linux 發行版的 IronOcr.ComputerVision.Linux,以及適用於 macOS 環境的 IronOcr.ComputerVision.MacOS。

如何偵測影像中的多個文字區域?

IronOcr 提供了 FindMultipleTextRegions 方法,可根據偵測到的文字區域將原圖分成多張圖片。您也可以使用 GetTextRegions 來擷取偵測到文字的裁切區域清單。

我可以在處理前確認哪些文字區域已被偵測到嗎?

是的,IronOCR 包含 StampCropRectangleAndSaveAs 方法,可讓您在執行實際 OCR 過程之前,檢查哪些文字區域是由電腦視覺演算法偵測出來的。

Curtis Chau
技術撰稿人

Curtis Chau 擁有電腦科學學士學位(卡爾頓大學),專長於前端開發,精通 Node.js、TypeScript、JavaScript 和 React。Curtis 對製作直覺且美觀的使用者介面充滿熱情,他喜歡使用現代化的架構,並製作結構良好且視覺上吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 也有濃厚的興趣,他喜歡探索整合硬體與軟體的創新方式。在空閒時間,他喜歡玩遊戲和建立 Discord bots,將他對技術的熱愛與創意結合。

準備好開始了嗎?
Nuget 下載 5,384,824 | 版本: 2026.2 剛剛發布