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

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

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

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

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

此範例演示即時文字提取:載入圖像,使用IronOCR的計算機視覺自動檢測主文字區域,然後執行.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

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

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

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

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

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

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

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

Install-Package IronOcr.ComputerVision.Windows

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

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

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

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

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

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文件。 此技術也在我們的除錯指南中的突出顯示文字技術指南中涵蓋。 我們可以看到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檢測多文字區域?

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();
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

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

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

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

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

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

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

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

IronOCR能整合到現有的應用程式中嗎?

IronOCR被設計成可以輕鬆地整合到現有應用程式中,使用C#允許開發人員以最小的努力為其軟體新增OCR功能。

使用IronOCR進行文件管理的好處是什麼?

使用IronOCR進行文件管理通過將掃描的文件轉換為可搜索和可編輯的文字來簡化工作流程,減少手動資料輸入的需求並提高文件的可存取性。

IronOCR如何提高資料精確性?

IronOCR通過其先進的識別算法和影像校正功能提高資料精確性,確保文字提取過程既可靠又精確。

IronOCR有免費試用版嗎?

有的,Iron Software提供IronOCR的免費試用版,允許使用者在做出購買決定前測試其功能和能力。

Curtis Chau
技術作家

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

除了開發,Curtis對物聯網(IoT)有濃厚的興趣,探索創新的方法來整合硬體和軟體。在空閒時間,他喜歡玩遊戲和建立Discord機器人,結合他對技術的熱愛與創造力。

準備開始了嗎?
Nuget 下載 6,136,090 | 版本: 2026.7 剛剛發布
Still Scrolling Icon

還在滾動?

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