IRONSOFTWAREHOME

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

Curtis Chau
Curtis Chau
Updated: 2026年6月4日

IronOCR使用OpenCV計算機視覺在OCR處理之前自動檢測圖像中的文字區域。 這可以改善對嘈雜、多區域或扭曲文字的準確性,因為Tesseract識別僅針對識別出的文字區域,大大增強了提取結果,相較於處理整個圖像。

快速入門:檢測並OCR主要文字區域

此範例演示即時文字提取:載入圖像,使用IronOCR的計算機視覺自動檢測主文字區域,然後執行.Read(...)在一行中提取文字。

  1. 1Install IronOCR with NuGet Package Manager

    PM > Install-Package IronOcr

  2. 2複製並運行這段程式碼片段。

    using var result = new IronTesseract().Read(new OcrInput().LoadImage("image.png").FindTextRegion());
    C#
  3. 3部署以在您的實時環境中測試

    今天就開始在您的專案中使用IronOCR,透過免費試用
    arrow pointer

如何通過NuGet Package安裝IronOcr.ComputerVision?

執行IronOCR中的OpenCV方法可以在常規IronOCR NuGet包中看到。 詳細的安裝指南請參閱我們的NuGet安裝指南

為什麼IronOCR需要一個單獨的計算機視覺包?

使用這些方法需要在解決方案中安裝IronOcr.ComputerVision的NuGet包。 如果未安裝,系統將提示您下載。 計算機視覺功能利用了OpenCV算法,可以大幅度提高文字檢測的準確性,類似於我們在車牌識別護照掃描功能中使用的技術。

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

如何使用Package Manager Console進行安裝?

使用NuGet Package Manager安裝或在Package Manager Console中粘貼以下內容:

PM > Install-Package IronOcr.ComputerVision.Windows

這提供了必要的程式集以便IronOCR計算機視覺與我們的模型文件配合使用。

IronOCR中有哪些計算機視覺方法?

本教程的後面部分包含了範例程式碼。 以下是當前可用方法的概覽:

方法解釋
FindTextRegion檢測包含文字元素的區域,並指示Tesseract僅在檢測到的文字區域內查找文字。
FindMultipleTextRegions檢測包含文字元素的區域,並根據文字區域將頁面劃分為單獨的圖像。
GetTextRegionsScans the image and returns a list of text regions as List<CropRectangle>.

如何使用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;

IronOcr 2025.6.x中已被棄用,並且不接受自定義參數。

如何自定義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;

FindTextRegion在實際使用中是什麼樣的?

在此範例中,我使用以下圖像作為需要裁剪到包含文字區域的方法,但輸入圖像的文字位置可能不同。 我使用FindTextRegion將掃描範圍縮小到計算機視覺檢測到文字的區域。 這種方法類似於我們在內容區域和裁剪區域教程中使用的技術。 這是一個範例圖像:

Iron Software 2022公司統計,顯示開發者指標和業務績效資料
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);

如何除錯和驗證文字區域檢測?

此程式碼有兩個輸出。 第一個是由.png文件。 此技術也在我們的除錯指南中的突出顯示文字技術指南中涵蓋。 我們可以看到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
Text

如何使用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;
小心: 自IronOCR v2025.6.x開始,FindMultipleTextRegions方法不再支持自定義參數。

如何自定義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;

如何使用FindMultipleTextRegions處理個別頁面?

FindMultipleTextRegions的另一個重載方法接收一個OCR頁面,並返回一個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();

如何使用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}");
}

計算機視覺在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.ComputerVision NuGet 套件,因為電腦視覺功能利用了 OpenCV 演算法。這些演算法顯著提高文字偵測準確性,並對車牌辨識和護照掃描等功能至關重要。

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

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

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

IronOCR 提供 FindMultipleTextRegions 方法以根據偵測到的文字區域將原始影像分割成多個影像。您也可以使用 GetTextRegions 檢索偵測到文字的裁剪區域清單。

我可以在處理之前驗證哪些文字區域已被偵測嗎?

可以,IronOCR 包含 StampCropRectangleAndSaveAs 方法,允許您檢查哪些文字區域在執行實際 OCR 過程前已被電腦視覺演算法偵測到。

What is the use of the GetTextRegions method in IronOCR?

The GetTextRegions method in IronOCR provides a list of detected text regions as coordinates, useful for further processing or implementing custom OCR workflows.

How can IronOCR assist in processing documents with multiple text areas?

IronOCR uses the FindMultipleTextRegions method to detect and process documents with multiple distinct text areas by automatically separating them into individual sections for better OCR performance.

Why might IronOCR require a separate Computer Vision package?

The separate Computer Vision package for IronOCR, available via NuGet, is necessary to utilize advanced text detection features powered by OpenCV algorithms, enhancing OCR accuracy beyond standard capabilities.

What common uses does computer vision in IronOCR support?

Computer vision in IronOCR can be used for document layout analysis, multi-column text recognition, distinguishing mixed content, performance optimizations, and quality control in OCR processes.

Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

...
閱讀更多

準備開始了嗎?

Nuget Downloads 6,236,385版本:2026.9剛剛發布

立即獲取免費

立即獲取 30天試用金鑰

bullet_checked無需信用卡或註冊帳號
bullet_test在生產
環境中進行測試,且不顯示浮水印
bullet_calendar30 天全
功能產品
bullet_support試用期間提供 24/5 技術
支援
立即獲取您的30天試用金鑰
無需信用卡或帳戶建立
C# PDF的NuGet程式庫
使用NuGet安裝

版本: 2026.9

PM > Install-Package IronOcr
nuget.org/packages/IronOcr/
  1. 在解決方案資源管理器中,右鍵點擊參考,管理NuGet包
  2. 選擇瀏覽並搜尋"IronOCR"
  3. 選擇包並安裝
C# PDF DLL
下載 DLL

版本: 2026.9

這裡下載Windows安裝程式。

  1. 下載並解壓IronOCR至您的方案目錄下的~/Libs等位置
  2. 在Visual Studio解決方案資源管理器中,右鍵點擊參考。選擇瀏覽,"IronOCR.dll"

授權從$999

有問題嗎?聯絡我們的開發團隊。

Key in blue circle

立即免費取得 30 天試用金鑰

Your trial license will be sent to your email address

無任何限制。100% 解鎖。無需信用卡。

bullet_checked無需信用卡或建立帳號無任何限制。100% 解鎖。無需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
獲取您的無義務諮詢
填寫以下表格或發送電子郵件至sales@ironsoftware.com
您的詳細資訊將始終保密。
被全球數百萬工程師信任
Iron Software的客戶標誌
立即獲取您的30天試用金鑰
無需信用卡或帳戶建立