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

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

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

IronOCR 利用 OpenCV 的電腦視覺技術來偵測影像中存在文字的區域。 這對於包含大量雜訊的圖像、文字分佈在許多不同位置的圖像以及文字扭曲的圖像非常有用。 IronOCR 利用電腦視覺技術確定文字區域的位置,然後使用 Tesseract 嘗試讀取這些區域。

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

這個範例展示了入門有多麼容易:只需載入一張圖片,讓 IronOCR 的電腦視覺功能使用FindTextRegion()自動找到主要文字區域,然後立即運行.Read(...)來擷取文字。 只需一行簡單的程式碼即可將影像轉換為 OCR 輸出。

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

IronOCR.電腦視覺透過 NuGet 套件安裝

IronOCR 中用於執行電腦視覺的 OpenCV 方法在常規的 IronOCR NuGet 套件中可見。

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

  • 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

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

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

尋找文字區域

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

圖片與文字
: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(電腦視覺)認為文字的位置:

影像中文字區域高亮顯示

看起來不錯。 第二個輸出是文字本身,內容如下:

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函數會取得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函數會傳回頁面中偵測到文字的裁切區域清單:

: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

具體用例指南

只要設定和輸入檔案合適,OCR 就能成為非常強大的工具。 它幾乎可以完美地模仿人類的閱讀能力。

常見問題解答

如何利用計算機視覺進行圖像中的文字偵測?

IronOCR 可利用其與 OpenCV 的整合來增強影像中的文字偵測。FindTextRegionFindMultipleTextRegions 等方法可讓您有效地定位和處理文字區域。

在不同作業系統安裝 IronOCR for Computer Vision 需要哪些步驟?

若要在不同作業系統上使用 IronOCR,您可以透過 NuGet 安裝 IronOCR.ComputerVision 套件。使用 Install-Package IronOcr.ComputerVision.Windows 指令來安裝 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,167,857 | Version: 2025.11 剛發表