如何使用電腦視覺在 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(...) 將文字擷取為單行。

  1. using NuGet 套件管理員安裝 https://www.nuget.org/packages/IronOcr

    PM > Install-Package IronOcr
  2. 請複製並執行此程式碼片段。

    using var result = new IronTesseract().Read(new OcrInput().LoadImage("image.png").FindTextRegion());
  3. 部署至您的生產環境進行測試

    立即透過免費試用,在您的專案中開始使用 IronOCR

    arrow pointer

如何透過 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
$vbLabelText   $csharpLabel

注意事項 此方法重載目前在 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 來將掃描範圍限定在電腦視覺已偵測到文字的區域。 此方法與我們在"內容區域與裁切區域"教學中使用的技巧相似。 此為範例圖片:

Iron Software 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

如何除錯並驗證文字區域偵測功能?

此程式碼有兩個輸出結果。 第一個是 .png 透過 StampCropRectangleAndSaveAs 儲存的檔案,用於除錯。 此技術亦收錄於我們的《除錯指南重點說明中。 我們可以看到 IronCV(電腦視覺)偵測到文字的位置:

Iron Software 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. 文件版面配置分析:自動識別並處理複雜文件中的不同區段。 對於掃描文件特別有用。
  2. 多欄位文字:將報紙或雜誌的各欄位分開,以便獨立閱讀。 使用多執行緒處理以獲得更快的結果。
  3. 混合內容:區分文件中的文字區域與圖形。 在處理內嵌文字的照片時相當實用。
  4. 效能優化:僅針對包含文字的區域進行 OCR 處理。 請參閱我們的快速 OCR 設定指南
  5. 品質控制:在進行完整的 OCR 處理前,先驗證文字偵測結果。 我們的進度追蹤功能會監控每個階段。

透過適當的設定與輸入檔案,OCR 技術可達到近乎人類的閱讀能力。 為獲得最佳效果,請將電腦視覺技術與我們的影像優化濾鏡結合使用,以實現最高的 OCR 辨識準確度。 若需處理低畫質影像,我們的《修復低畫質掃描檔指南》提供了實用的預處理技巧。

進階電腦視覺技術

對於希望突破 OCR 精準度極限的開發者,請考慮以下進階方法:

常見問題

在 OCR 中,何謂電腦視覺?它如何提升文字擷取效能?

IronOCR 中的電腦視覺功能採用 OpenCV 演算法,在 OCR 處理前自動偵測影像中的文字區域。此機制能顯著提升雜訊、多區域或變形文字的辨識準確度,因為它會將 Tesseract 的辨識範圍僅限於已識別的文字區域,而非處理整張影像。

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

IronOCR 讓您僅需一行程式碼即可實作電腦視覺 OCR:使用 IronTesseract 搭配 FindTextRegion() 方法自動偵測主要文字區域,接著執行 .Read() 即可立即擷取文字。

為什麼我需要安裝獨立的電腦視覺套件?

IronOCR 需要額外安裝 IronOcr.Com/puterVision NuGet 套件,因為其電腦視覺功能採用 OpenCV 演算法。這些演算法能顯著提升文字偵測的準確度,對於車牌辨識和護照掃描等功能至關重要。

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

IronOCR 提供針對特定平台的套件:適用於 Windows 系統的 IronOcr.Com/puterVision.Windows、適用於 Linux 發行版的 IronOcr.Com/puterVision.Linux,以及適用於 macOS 環境的 IronOcr.Com/puterVision.MacOs。

如何在圖片中偵測多個文字區域?

IronOCR 提供 FindMultipleTextRegions 方法,可根據偵測到的文字區域將原始影像分割為多個影像。您亦可使用 GetTextRegions 取得偵測到文字的裁切區域清單。

在處理前,我可以確認系統偵測到了哪些文字區域嗎?

是的,IronOCR 包含 StampCropRectangleAndSaveAs 方法,讓您能在執行實際 OCR 流程前,先確認電腦視覺演算法偵測到的文字區域。

IronOCR 能否整合至現有應用程式中?

IronOCR 設計上可輕鬆透過 C# 整合至現有應用程式中,讓開發人員能以最少的努力,為其軟體增添 OCR 功能。

使用 IronOCR 進行文件管理有哪些好處?

使用 IronOCR 進行文件管理,可將掃描文件轉換為可搜尋且可編輯的文字,從而簡化工作流程,減少人工資料輸入的需求,並提升文件的可存取性。

IronOCR 如何提升資料準確性?

IronOCR 透過其先進的辨識演算法與影像校正功能來提升資料準確性,確保文字擷取過程既可靠又精確。

IronOCR 是否有提供免費試用版?

是的,Iron Software 提供 IronOCR 的免費試用版,讓使用者能在決定購買前測試其功能與效能。

Curtis Chau
技術撰稿人

Curtis Chau 擁有卡爾頓大學(Carleton University)的電腦科學學士學位,專精於前端開發,並精通 Node.js、TypeScript、JavaScript 及 React。他熱衷於打造直觀且美觀的用戶介面,喜歡運用現代框架,並創建結構完善、視覺上吸引人的手冊。

除了開發工作之外,Curtis 對物聯網(IoT)抱有濃厚興趣,致力於探索整合硬體與軟體的創新方法。閒暇時,他喜歡玩遊戲和開發 Discord 機器人,將對科技的熱愛與創意相結合。

準備開始了嗎?
Nuget 下載 5,896,332 | 版本: 2026.5 just released
Still Scrolling Icon

還在往下捲動嗎?

想要快速確認成果嗎? PM > Install-Package IronOcr
執行範例 觀看您的圖片轉為可搜尋文字。