如何在C#中定義圖像的特定OCR區域
要從C#中圖像的特定區域提取文字,使用IronOCR的LoadImage方法進行目標OCR處理。
快速入門:從特定圖像區域提取文字
-
使用NuGet套件管理器安裝https://www.nuget.org/packages/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區域,您需要從Rectangle物件。 此物件需要四個值:height,所有值均以像素為單位。 (x, y)坐標代表您所需區域的左上角。
當您使用Rectangle作為第二個參數傳遞。 IronOCR將只限制OCR過程在該凸邊框內的像素。
在處理如發票、掃描表單或身份文件等結構化文件時,區域性OCR特別有用,因為特定資訊總是出現在可預測的位置。 通過將OCR限制在相關區域,您可以大幅提高處理速度並減少因不相關文字產生的誤報。
要查找您的Rectangle坐標,您可以使用如MS Paint這樣的簡單圖片編輯器。 打開您的輸入圖像,將滑鼠懸停在指定區域的左上角和右下角,並記下(x, y)像素坐標。 然後您可以計算矩形的屬性:(x1, y1, width, width = height = 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
對於更複雜的情景,您可以在同一圖像中定義多個區域。 這在處理包含多個字段的表單或文件中的表格時特別有用:
:path=/static-assets/ocr/content-code-examples/how-to/ocr-region-of-an-image-3.cs
using IronOcr;
using IronSoftware.Drawing;
var ocr = new IronTesseract();
// 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 with a separate OcrInput per region
OcrResult nameResult;
using (var input = new OcrInput())
{
input.LoadImage("form.png", nameField);
nameResult = ocr.Read(input);
}
OcrResult dateResult;
using (var input = new OcrInput())
{
input.LoadImage("form.png", dateField);
dateResult = ocr.Read(input);
}
OcrResult amountResult;
using (var input = new OcrInput())
{
input.LoadImage("form.png", amountField);
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()
' 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 with a separate OcrInput per region
Dim nameResult As OcrResult
Using input As New OcrInput()
input.LoadImage("form.png", nameField)
nameResult = ocr.Read(input)
End Using
Dim dateResult As OcrResult
Using input As New OcrInput()
input.LoadImage("form.png", dateField)
dateResult = ocr.Read(input)
End Using
Dim amountResult As OcrResult
Using input As New OcrInput()
input.LoadImage("form.png", amountField)
amountResult = ocr.Read(input)
End Using
' Process each field separately
Console.WriteLine($"Name: {nameResult.Text}")
Console.WriteLine($"Date: {dateResult.Text}")
Console.WriteLine($"Amount: {amountResult.Text}")
我可以預期什麼結果?
正如您從控制台輸出中看到的,只有第二段落由OCR處理。 這種針對性的方法確保來自圖像其他部分的不相關文字不會干擾您的結果。
區域性OCR的準確性取決於多個因素:
我如何驗證我的坐標正確?
為了確保您選擇的輸入圖像坐標正確,您可以將所定義的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適用於幾個常見情景:
區域性OCR的最佳實踐
要用區域性OCR獲得最佳效果:
- 新增填充:在文字周圍包括一個小的緩衝區,以確保邊緣沒有字元被剪切。
- 使用樣本圖片測試:在處理大批量之前,始終用代表性的樣本驗證您的坐標。
- 處理變化:考慮到掃描文件中的輕微位置變化,使您的區域略大於必要。
- 優化性能:對於多執行緒處理,可並行處理不同區域。
- 監控置信度:檢查結果置信度分數以確保準確性。
通過將OCR處理集中在特定區域,您可以顯著提高文字提取任務的速度和準確性。 這種目標方法對於在.NET應用程式中構建高效文件處理工作流程至關重要。
常見問題
如何在C#中僅從圖像的特定部分提取文字?
使用IronOCR,您可以透過建立一個具有x/y座標、寬度和高度值的矩形物件從特定區域提取文字。將這個矩形作為第二個參數傳給LoadImage方法,IronOCR將其OCR處理限制於這個定義的區域。
定義OCR區域而不是掃描整個圖像的好處是什麼?
通過IronOCR定義特定的OCR區域,您可以提高處理速度,增加準確性,並減少因捕獲無關文字而導致的錯誤。這對於資訊出現在可預測位置的結構化文件特別有用。
為IronOCR的區域性OCR建立矩形需要哪些參數?
為了建立IronOCR區域性OCR的矩形,您需要四個像素值:x座標、y座標、寬度和高度。(x, y)座標表示您所需掃描區域的左上角。
OCR區域定義矩形物件位於哪個命名空間?
用於IronOCR中OCR區域定義的矩形物件位於IronSoftware.Drawing命名空間中。
哪些型別的文件最適用於區域性OCR處理?
IronOCR的區域性OCR對於如發票、掃描表單和身份文件等結構化文件特別有效,因為具體資訊在相同位置一致出現。
IronOCR支援多種語言嗎?
IronOCR支援多種語言,使其成為全球需要不同語言文字識別的應用程式的多功能工具。
IronOCR能整合到現有的應用程式中嗎?
IronOCR被設計成可以輕鬆地整合到現有應用程式中,使用C#允許開發人員以最小的努力為其軟體新增OCR功能。
使用IronOCR進行文件管理的好處是什麼?
使用IronOCR進行文件管理通過將掃描的文件轉換為可搜索和可編輯的文字來簡化工作流程,減少手動資料輸入的需求並提高文件的可存取性。
IronOCR如何提高資料精確性?
IronOCR通過其先進的識別算法和影像校正功能提高資料精確性,確保文字提取過程既可靠又精確。
IronOCR有免費試用版嗎?
有的,Iron Software提供IronOCR的免費試用版,允許使用者在做出購買決定前測試其功能和能力。

