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

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

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

IronOCR使用 OpenCV 電腦視覺技術在 OCR 處理之前自動偵測影像中的文字區域。 這種方法透過僅將 Tesseract 識別集中在已識別的文字區域,提高了對雜訊、多區域或扭曲文字的識別準確率,與處理整個影像相比,顯著提高了擷取結果。

快速入門:偵測並辨識主文字區域

此範例示範了即時文字擷取:載入影像,使用 IronOCR 的電腦視覺自動偵測主要文字區域(FindTextRegion()),然後執行 .Read(...) 擷取一行文字。

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

-如何在 C# 中進行車牌 OCR 辨識(教學) -如何在 C# 教程中從發票中獲取文本 -如何使用 C# 從螢幕截圖中提取文本 -如何在 C# 中進行 OCR 字幕辨識(教學)

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

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

為什麼IronOCR需要單獨的電腦視覺軟體包?

使用這些方法需要NuGetIronOcr.ComputerVision 安裝到解決方案中。 如果您尚未安裝軟體,系統會提示您下載。 電腦視覺功能利用 OpenCV 演算法顯著提高了文字偵測精度,類似於我們在車牌辨識護照掃描功能中使用的技術。

我應該安裝哪個平台特定軟體包?

如何使用軟體套件管理器控制台進行安裝?

使用NuGet套件管理器安裝,或將以下內容貼到套件管理器控制台中:

Install-Package IronOcr.ComputerVision.Windows

這提供了將IronOCR電腦視覺與我們的模型檔案配合使用所需的元件。

IronOCR中有哪些電腦視覺方法?

本教學後面會提供程式碼範例。 以下是目前可用方法的概述:

方法 解釋
尋找文字區域 偵測包含文字元素的區域,並指示 Tesseract 只在偵測到文字的區域內搜尋文字。
尋找多個文字區域 偵測包含文字元素的區域,並根據文字區域將頁面分割成單獨的圖像。
取得文字區域 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;
$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;
$vbLabelText   $csharpLabel

FindTextRegion 在實際應用上是什麼樣的?

在這個例子中,我使用以下圖像來演示一種需要裁剪到包含文字區域的方法,但輸入圖像的文字位置可能會有所不同。 我使用 FindTextRegion 將掃描範圍縮小到電腦視覺偵測到文字的區域。 這種方法與我們在內容領域和作物區域教程中使用的技術類似。 這是一張範例圖片:

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

如何調試和驗證文字區域檢測?

這段程式碼有兩個輸出。 第一個是 .png 文件,由 StampCropRectangleAndSaveAs 保存,用於調試。 該技術也在我們的調試指南重點文本中有所介紹。 我們可以看到IronCV(計算機視覺)偵測到文字的位置:

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

如何使用 FindMultipleTextRegions 處理單一頁面?

FindMultipleTextRegions 的另一個重載方法接受一個 OCR 頁面,並傳回一個 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();
$vbLabelText   $csharpLabel

如何使用 GetTextRegions 取得文字區域座標?

GetTextRegions 傳回頁面上偵測到文字的裁切區域清單。 當您需要文字區域的座標以進行進一步處理或在實施自訂 OCR 工作流程時,此方法特別有用。 有關處理結果的更多詳細信息,請參閱我們的OcrResult 類別文件

何時應該使用 GetTextRegions 而不是 FindTextRegions?

/* :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}");
}
$vbLabelText   $csharpLabel

電腦視覺在光學字元辨識 (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() 即可立即擷取文字。

為什麼我需要安裝獨立的 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 機器人,結合科技與創意的樂趣。

準備好開始了嗎?
Nuget 下載 5,525,971 | 版本: 2026.3 剛剛發布
Still Scrolling Icon

還在捲動嗎?

想要快速證明? PM > Install-Package IronOcr
執行範例 觀看您的圖片變成可搜尋的文字。