如何在C#中使用計算機視覺尋找文字
IronOCR使用OpenCV計算機視覺在OCR處理之前自動檢測圖像中的文字區域。 這可以改善對嘈雜、多區域或扭曲文字的準確性,因為Tesseract識別僅針對識別出的文字區域,大大增強了提取結果,相較於處理整個圖像。
快速入門:檢測並OCR主要文字區域
此範例演示即時文字提取:載入圖像,使用IronOCR的計算機視覺自動檢測主文字區域,然後執行.Read(...)在一行中提取文字。
最小工作流程(5步)
- 下載C#程式庫以使用計算機視覺進行OCR
- 使用
FindTextRegion方法自動檢測文字區域 - 使用
StampCropRectangleAndSaveAs方法檢查檢測到的文字區域 - 使用計算機視覺將原始圖像根據文字區域分割成多個圖像,使用
FindMultipleTextRegions方法 - 使用
GetTextRegions方法獲取檢測到文字的裁剪區域清單
如何通過NuGet Package安裝IronOcr.ComputerVision?
執行IronOCR中的OpenCV方法可以在常規IronOCR NuGet包中看到。 詳細的安裝指南請參閱我們的NuGet安裝指南。
為什麼IronOCR需要一個單獨的計算機視覺包?
使用這些方法需要在解決方案中安裝IronOcr.ComputerVision的NuGet包。 如果未安裝,系統將提示您下載。 計算機視覺功能利用了OpenCV算法,可以大幅度提高文字檢測的準確性,類似於我們在車牌識別和護照掃描功能中使用的技術。
我應該安裝哪個特定平台的包?
- Windows:
IronOcr.ComputerVision.Windows- 查看我們的Windows設置指南 - Linux:
IronOcr.ComputerVision.Linux- 查看我們的Linux安裝教程 - macOS:
IronOcr.ComputerVision.MacOS- 查看我們的macOS設置指南 - macOS ARM:
IronOcr.ComputerVision.MacOS.ARM
如何使用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
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
FindTextRegion在實際使用中是什麼樣的?
在此範例中,我使用以下圖像作為需要裁剪到包含文字區域的方法,但輸入圖像的文字位置可能不同。 我使用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)
如何除錯和驗證文字區域檢測?
此程式碼有兩個輸出。 第一個是由.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物件的所有頁面,使用計算機視覺檢測包含文字元素的區域,然後將輸入分割為根據文字區域的單獨圖像。 這特別適用於處理具有多個獨立文字區域的文件,類似於我們的文件中讀取表格功能:
什麼是基本的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
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
如何使用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()
如何使用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
計算機視覺在OCR中的常見用例有哪些?
在具挑戰性的場景中,計算機視覺可顯著提高OCR的準確性。 這裡是一些實際應用:
- 文件佈局分析:自動識別和處理複雜文件的不同部分。 特別對於掃描文件非常有用。
- 多列文字:獨立分離並讀取報紙或雜誌的欄目。 使用多執行緒處理以加速處理。
- 混合內容:在文件中區分文字區域和圖形。 當處理包含嵌入文字的照片時十分有幫助。
- 性能優化:僅將OCR處理集中在包含文字的區域。 請參考我們的快速OCR配置指南。
- 質量控制:在進行完整OCR處理前驗證文字檢測。 我們的進度跟蹤功能監控每個階段。
透過正確的設置和輸入文件,OCR可達到接近人類的閱讀能力。 為了獲得最佳結果,將計算機視覺與我們的圖像優化過濾器相結合,以獲得最佳的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的免費試用版,允許使用者在做出購買決定前測試其功能和能力。

