如何在 C# 中使用電腦視覺尋找文字
IronOCR 使用 OpenCV 電腦視覺技術,在進行 OCR 處理前自動偵測影像中的文字區域。 這可提高雜訊、多區域或扭曲文字的精確度,方法是只將 Tesseract 辨識功能集中在已辨識的文字區域,與處理整個影像相比,可大幅提升擷取結果。
快速入門:偵測主要文字區域並執行 OCR以下範例只用一行程式碼,即可載入影像、透過FindTextRegion()使用 IronOCR 的電腦視覺偵測主要文字區域,再呼叫.Read(...)擷取文字。
-
1使用 IronOCR 安裝 NuGet Package Manager
-
2複製並運行這段程式碼。
using var result = new IronTesseract().Read(new OcrInput().LoadImage("image.png").FindTextRegion());Using result = New IronTesseract().Read(New OcrInput().LoadImage("image.png").FindTextRegion())Using result = New IronTesseract().Read(New OcrInput().LoadImage("image.png").FindTextRegion()) -
3部署到您的生產環境進行測試
立即在您的項目中開始使用 IronOCR,並使用 免費試用
基本流程(5 個步驟)
- 下載C#程式庫以使用電腦視覺進行OCR
- 使用
FindTextRegion方法自動偵測文字區域 - 檢查使用
StampCropRectangleAndSaveAs方法偵測到的文字區域。 - 使用
FindMultipleTextRegions方法,以電腦視覺依文字區域將原始影像分割成多張影像。 - 使用
GetTextRegions方法取得偵測到文字的裁切區域清單。
如何透過 NuGet 套件安裝 IronOCR.ComputerVision?
IronOCR 常規 NuGet 套件會公開用於電腦視覺的 OpenCV 方法。如需詳細安裝步驟,請參閱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 套件管理員安裝,或將下列命令貼到套件管理員主控台:
這提供了使用 IronOCR Computer Vision 與我們的模型檔案所需的程式集。
IronOCR中有哪些電腦視覺方法可用?
程式碼範例包含在本教學的後面。 以下是目前可用方法的一般概述:
| 方法 | 說明 |
|---|---|
FindTextRegion | 偵測包含文字元素的區域,並指示 Tesseract 只在偵測到文字的區域內搜尋文字。 |
FindMultipleTextRegions | 偵測包含文字元素的區域,並根據文字區域將頁面分割成單獨的影像。 |
GetTextRegions | 掃描影像,並以List<Rectangle>傳回文字區域清單。 |
如何使用FindTextRegion偵測文字區域?
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;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.TextImports 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.TextIronOcr 2025.6.x中已被棄用,且不接收自訂參數。FindTextRegion 的實作範例
在此範例中,我使用下方的影像示範如何裁切出含有文字的區域。不同輸入影像的文字位置可能不同,因此我使用FindTextRegion將掃描範圍縮小至電腦視覺偵測到文字的區域。這與內容區域和裁切區域教學中的做法類似。以下是範例影像:

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);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)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)如何除錯及驗證文字區域偵測結果?
這段程式碼有兩項輸出。第一項是StampCropRectangleAndSaveAs儲存的.png檔案,可用於除錯。以醒目標示文字協助除錯的指南也介紹了這項技巧。從圖中可看到 IronCV (Computer Vision) 偵測到文字的位置:
偵測準確辨識文字區域。 第二個輸出是文字本身:
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使用方法是什麼?
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;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.TextImports 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.TextFindMultipleTextRegions 方法不再支持自訂參數。如何使用FindMultipleTextRegions處理個別頁面?
FindMultipleTextRegions的另一個多載接受一個 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();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()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()如何使用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("/path/file.png", 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("/path/file.png", 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 = New IronTesseract()
' Load an image into OcrInput
Using input = 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("/path/file.png", region)
Console.WriteLine($"Text in region: {regionResult.Text}")
Next
End UsingImports IronOcr
Imports IronSoftware.Drawing
Imports System
Imports System.Collections.Generic
Imports System.Linq
' Create a new IronTesseract object for OCR
Dim ocr = New IronTesseract()
' Load an image into OcrInput
Using input = 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("/path/file.png", region)
Console.WriteLine($"Text in region: {regionResult.Text}")
Next
End Using電腦視覺在 OCR 中有哪些常見用途?
電腦視覺可在具挑戰性的情況下大幅提升 OCR 精確度。 以下是實際應用:
- 文件版面配置分析: 自動辨識和處理複雜文件的不同部分。 特別適用於掃描文件。
- 多欄文字: 獨立分離和讀取報紙或雜誌的欄。 使用多執行緒處理以獲得更快的結果。
- 混合內容: 區分文件中的文字區域和圖形。 處理內嵌文字的照片時有幫助。
- 效能最佳化: 專注於僅僅含文字的區域進行OCR處理。 查看我們的快速 OCR 設定指南。
- 品質管控: 在全面的OCR處理之前驗證文字偵測。 我們的進度追蹤功能監控每個階段。
只要有正確的設定和輸入檔案,OCR 就能達到接近人類的辨識能力。 為了獲得最佳效果,將電腦視覺與我們的影像最佳化濾鏡結合使用,以達到最佳的OCR準確性。 處理低品質影像時,我們的修復低品質掃描指南提供了有價值的前處理技術。
進階電腦視覺技術
對於想要突破 OCR 精確度邊界的開發人員而言,請考慮這些先進的方法:
- 自訂訓練: 使用我們的語言檔案自訂指南為特定字體訓練OCR引擎
- 多語言支援: 使用我們的多語言功能處理多語言檔案
- 條碼整合: 使用我們的具備條碼辨識能力的OCR結合文字辨識與條碼閱讀
常見問題
IronOCR中的FindTextRegion方法是什麼?
IronOCR中的FindTextRegion方法使用電腦視覺來檢測圖像中包含文字元素的區域,允許Tesseract OCR僅專注於那些區域,從而提高從噪聲或多區域圖像中讀取文字的準確性。
IronOCR如何利用電腦視覺提高文字檢測的準確性?
IronOCR利用OpenCV電腦視覺算法自動檢測並專注於圖像中的文字區域,然後再進行OCR處理,從而顯著提高文字檢測準確性。
IronOCR可以進行多個文字區域檢測嗎?
是的,IronOCR可以使用FindMultipleTextRegions方法根據文字區域識別並分割輸入圖像,從而提高處理具有多個文字塊的文件的效率。
IronOCR在文字檢測過程中如何處理有噪聲或失真的圖像?
IronOCR使用FindTextRegion方法從有噪聲或失真的圖像中隔離文字區域,使Tesseract OCR能夠僅專注於這些特定區域,從而提高閱讀準確性和性能。
IronOCR的電腦視覺套件支援哪些平台?
IronOCR的電腦視覺套件支援Windows、Linux、macOS和macOS ARM,並為每個平台提供特定的安裝指南。
在使用IronOCR進行OCR任務時,使用電腦視覺有何好處?
電腦視覺通過允許IronOCR自動檢測並專注於圖像中包含文字的區域來增強OCR任務,從而提高準確性,特別是對於複雜或低品質的文件。
IronOCR中的GetTextRegions方法有何用途?
IronOCR中的GetTextRegions方法提供作為座標的檢測文字區域列表,對進一步處理或實施自定義OCR工作流程很有用。
IronOCR如何協助處理具有多個文字區域的文件?
IronOCR使用FindMultipleTextRegions方法來檢測和處理具有多個不同文字區域的文件,通過自動將其分割為獨立的部分來提高OCR性能。
IronOCR為何可能需要單獨的電腦視覺套件?
IronOCR的獨立電腦視覺套件,通過NuGet提供,對利用OpenCV算法增強的高級文字檢測功能是必要的,提升OCR精度超越標準能力。
IronOCR中的電腦視覺都支持哪些常見的用途?
IronOCR中的電腦視覺可用於文件佈局分析、多欄文字識別、識別混合內容、性能優化及OCR過程中的品質控制。
Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。