如何在 C# 中定義影像的特定 OCR辨識區域以精準擷取圖片文字
要在 C# 中從圖像的特定區域進行圖片轉文字處理,請使用 IronOCR 的 Rectangle 對象,透過指定 y 座標、width 和 @@--CODE-316-----CODE-316--DE@CO方法進行目標 OCR辨識處理。
快速入門:從特定影像區域擷取文字
-
使用NuGet套件管理器安裝https://www.nuget.org/packages/IronOcr
PM > Install-Package IronOcr -
複製並運行這段程式碼。
using IronOcr; using IronSoftware.Drawing; // 1. Install IronOCR via NuGet: Install-Package IronOcr var ocr = new IronTesseract(); using var input = new OcrInput(); // 2. Create a Rectangle with coordinates var region = new Rectangle(x: 215, y: 1250, width: 1335, height: 280); // 3. Load image with region input.LoadImage("image.png", region); // 4. Extract text var result = ocr.Read(input); Console.WriteLine(result.Text); -
部署到您的生產環境進行測試
今天就在您的專案中開始使用免費試用IronOCR
通常情況下,你只需要從圖像的一小部分中提取文本,例如發票上的總金額或表單中的特定欄位。 掃描整個文件效率低下,並且可能因為捕獲無關文字而導致錯誤。
IronOCR可讓您透過指定要掃描的精確矩形區域來提高精確度、效能和準確性。 本指南提供了逐步演練,指導您如何定義特定的 OCR 區域、從中提取文本,並透過視覺方式驗證您的座標是否適用於您的 OCR 任務。
IronOCR入門指南
如何定義影像中的特定OCR區域
- 下載用於定義 OCR 區域的 C# 庫
- 實例化 OCR 引擎
- 以矩形框標示OCR區域。
- 使用`LoadImage`載入圖像以及定義的矩形區域
- 存取**`OcrResult`**屬性以查看和操作提取的數據
如何對特定區域進行OCR識別?
若要定義特定的 OCR 區域,您可以從 IronSoftware.Drawing 命名空間建立一個 Rectangle 物件。 此物件需要四個值:width 和 height,全部以像素為單位。 (y)座標表示所需區域的左上角。
當您使用 LoadImage 載入映像時,您需要將此 Rectangle 作為第二個參數傳遞。 IronOCR隨後會將 OCR 過程限制在該邊界框內的像素範圍內。
區域 OCR 在處理結構化文件(如發票、掃描表格或身分證件)時特別有用,因為特定資訊總是出現在可預測的位置。 透過將 OCR 限制在相關區域,可以顯著提高處理速度並減少無關文字的誤報。
要找到您的 Rectangle 的座標,您可以使用像 MS Paint 這樣的簡單圖像編輯器。 開啟輸入影像,將滑鼠懸停在指定區域的左上角和右下角,並記下(y)像素座標。 然後您可以計算矩形的屬性:(x1, y1, width, height),其中 width y1。
測試時該使用哪張圖片?
我們將使用一張包含三個段落的範例圖片。我們的目標是只提取第二個段落,忽略其餘文本。 這展示了一個常見場景,即需要從較大的文件中提取特定欄位或部分。
如何在程式碼中實現區域 OCR?
此實作方式包括建立一個OcrInput 對象,並載入具有指定矩形區域的圖像。 這種方法適用於各種影像格式,包括JPG、PNG、GIF、TIFF 和 BMP 。
:path=/static-assets/ocr/content-code-examples/how-to/ocr-region-of-an-image.cs
using IronOcr;
using IronSoftware.Drawing;
using System;
var ocrTesseract = new IronTesseract();
using var ocrInput = new OcrInput();
// Define the specific region as a Rectangle
// (x, y) is the top-left corner.
var ContentArea = new Rectangle(x: 215, y: 1250, width: 1335, height: 280);
ocrInput.LoadImage("region-input.png", ContentArea);
var ocrResult = ocrTesseract.Read(ocrInput);
// Print the extracted text
Console.WriteLine(ocrResult.Text);
Imports IronOcr
Imports IronSoftware.Drawing
Imports System
Dim ocrTesseract As New IronTesseract()
Using ocrInput As New OcrInput()
' Define the specific region as a Rectangle
' (x, y) is the top-left corner.
Dim ContentArea As New Rectangle(x:=215, y:=1250, width:=1335, height:=280)
ocrInput.LoadImage("region-input.png", ContentArea)
Dim ocrResult = ocrTesseract.Read(ocrInput)
' Print the extracted text
Console.WriteLine(ocrResult.Text)
End Using
對於更複雜的場景,您可以在同一幅影像中定義多個區域。 這在處理包含多個欄位的表單或文件中的表格時尤其有用:
using IronOcr;
using IronSoftware.Drawing;
var ocr = new IronTesseract();
using var input = new OcrInput();
// Define multiple regions for different form fields
var nameField = new Rectangle(x: 100, y: 200, width: 300, height: 50);
var dateField = new Rectangle(x: 100, y: 300, width: 200, height: 50);
var amountField = new Rectangle(x: 400, y: 500, width: 150, height: 50);
// Load the same image multiple times with different regions
input.LoadImage("form.png", nameField);
var nameResult = ocr.Read(input);
input.Clear();
input.LoadImage("form.png", dateField);
var dateResult = ocr.Read(input);
input.Clear();
input.LoadImage("form.png", amountField);
var amountResult = ocr.Read(input);
// Process each field separately
Console.WriteLine($"Name: {nameResult.Text}");
Console.WriteLine($"Date: {dateResult.Text}");
Console.WriteLine($"Amount: {amountResult.Text}");
using IronOcr;
using IronSoftware.Drawing;
var ocr = new IronTesseract();
using var input = new OcrInput();
// Define multiple regions for different form fields
var nameField = new Rectangle(x: 100, y: 200, width: 300, height: 50);
var dateField = new Rectangle(x: 100, y: 300, width: 200, height: 50);
var amountField = new Rectangle(x: 400, y: 500, width: 150, height: 50);
// Load the same image multiple times with different regions
input.LoadImage("form.png", nameField);
var nameResult = ocr.Read(input);
input.Clear();
input.LoadImage("form.png", dateField);
var dateResult = ocr.Read(input);
input.Clear();
input.LoadImage("form.png", amountField);
var amountResult = ocr.Read(input);
// Process each field separately
Console.WriteLine($"Name: {nameResult.Text}");
Console.WriteLine($"Date: {dateResult.Text}");
Console.WriteLine($"Amount: {amountResult.Text}");
Imports IronOcr
Imports IronSoftware.Drawing
Dim ocr As New IronTesseract()
Using input As New OcrInput()
' Define multiple regions for different form fields
Dim nameField As New Rectangle(x:=100, y:=200, width:=300, height:=50)
Dim dateField As New Rectangle(x:=100, y:=300, width:=200, height:=50)
Dim amountField As New Rectangle(x:=400, y:=500, width:=150, height:=50)
' Load the same image multiple times with different regions
input.LoadImage("form.png", nameField)
Dim nameResult = ocr.Read(input)
input.Clear()
input.LoadImage("form.png", dateField)
Dim dateResult = ocr.Read(input)
input.Clear()
input.LoadImage("form.png", amountField)
Dim amountResult = ocr.Read(input)
' Process each field separately
Console.WriteLine($"Name: {nameResult.Text}")
Console.WriteLine($"Date: {dateResult.Text}")
Console.WriteLine($"Amount: {amountResult.Text}")
End Using
我可以期待什麼樣的結果?
從控制台輸出可以看出,只有第二個段落被 OCR 處理了。 這種有針對性的方法可以確保圖像其他部分的無關文字不會幹擾您的結果。
區域OCR的準確性取決於以下幾個因素: -影像品質:解析度越高的影像通常效果越好。 考慮使用DPI設定來優化影像。 -文字方向:確保文字正確對齊。 必要時使用頁面旋轉檢測功能。 -對比度和清晰度:應用影像校正濾鏡以增強文字可讀性。
如何驗證我的座標是否正確?
為了確保您已為輸入影像選擇了正確的座標,您可以視覺化您定義的ContentArea 。 一個簡單的方法是在輸入圖像上繪製矩形,並將其另存為新文件,文件名稱為 StampCropRectangleAndSaveAs。 這有助於您調試和微調坐標,以獲得最佳性能。
這種視覺化技術在處理複雜版面或需要突出顯示特定文字區域以進行品質保證時特別有用。
這是在上面的範例輸入影像上繪製指定邊界框後的輸出影像。
如何視覺化選定區域?
:path=/static-assets/ocr/content-code-examples/how-to/ocr-region-of-an-image-highlighted.cs
using IronOcr;
using IronSoftware.Drawing;
var ocrTesseract = new IronTesseract();
using var ocrInput = new OcrInput();
// Define the specific rectangular area to scan within the image.
// The coordinates are in pixels: (x, y) is the top-left corner of the rectangle.
var ContentArea = new Rectangle(x: 4, y: 59, width: 365, height: 26);
ocrInput.LoadImage("region-input.png", ContentArea);
var ocrResult = ocrTesseract.Read(ocrInput);
// Draws the rectangle from above in a blue bounding box on the image for visualization.
ocrInput.StampCropRectangleAndSaveAs(ContentArea, Color.Aqua, "region-input.png");
Imports IronOcr
Imports IronSoftware.Drawing
Dim ocrTesseract = New IronTesseract()
Using ocrInput As New OcrInput()
' Define the specific rectangular area to scan within the image.
' The coordinates are in pixels: (x, y) is the top-left corner of the rectangle.
Dim ContentArea As New Rectangle(x:=4, y:=59, width:=365, height:=26)
ocrInput.LoadImage("region-input.png", ContentArea)
Dim ocrResult = ocrTesseract.Read(ocrInput)
' Draws the rectangle from above in a blue bounding box on the image for visualization.
ocrInput.StampCropRectangleAndSaveAs(ContentArea, Color.Aqua, "region-input.png")
End Using
可視化效果是什麼樣的?
淺藍色矩形框表示我們已經正確地提取第二個段落進行處理。
何時應該使用區域 OCR?
區域 OCR 非常適合以下幾種常見場景:
1.表單處理:從資料位置一致的標準化表單中提取特定欄位。 2.發票處理:提取特定值,如總計、日期或發票編號,而無需處理整份文件。 3.車牌:使用車牌辨識時,只需專注於車牌區域。 4.身分證明文件:從護照或身分證中提取特定欄位。 5.螢幕截圖:在螢幕截圖中擷取特定 UI 元素中的文字。
區域 OCR 最佳實踐
為了達到區域 OCR 的最佳效果:
1.新增內邊距:在文字周圍新增一個小的緩衝區,以確保沒有字元在邊緣被截斷。 2.使用樣本影像進行測試:在處理大量資料之前,請務必使用代表性樣本驗證座標。 3.處理偏差:為了應對掃描文件中輕微的位置偏差,請將區域做得比實際需要的稍大。 4.最佳化效能:對於多執行緒處理,並行處理不同的區域。 5.監控置信度:檢查結果置信度得分以確保準確度。
透過將 OCR 處理集中在特定區域,可以顯著提高文字擷取任務的速度和準確性。 這種有針對性的方法對於在.NET應用程式中建立高效的文件處理工作流程至關重要。
常見問題解答
如何在 C# 中僅從圖像的特定部分擷取文字?
使用 IronOCR,您可以透過建立一個具有 x/y 座標、寬度和高度值的 Rectangle 物件,從特定區域擷取文字。將此 Rectangle 作為 LoadImage 方法的第二個參數傳送給 IronOCR,IronOCR 就會將其 OCR 處理限制在僅限於該定義的區域。
定義 OCR 區域而非掃描整個影像有什麼好處?
透過 IronOCR 定義特定的 OCR 區域,您可以提高處理速度、增加精確度,並減少擷取無關文字所造成的錯誤。這對於資訊出現在可預測位置的結構化文件尤其有用。
我需要哪些參數才能建立區域 OCR 的矩形?
要為 IronOCR 的區域 OCR 建立一個矩形,您需要四個像素值:x 座標、y 座標、寬度和高度。(x, y) 座標代表您所需掃描區域的左上角。
哪個命名空間包含用於 OCR 區域的 Rectangle 物件?
IronOCR 中用於定義 OCR 區域的矩形物件位於 IronSoftware.Drawing 命名空間中。
哪些類型的文件最受益於區域 OCR 處理?
IronOCR 的區域 OCR 對於結構化文件尤其有效,例如發票、掃描表格和身份文件,這些文件中的特定資訊會持續出現在相同的位置。

