如何在 C# 中定義影像的特定 OCR 區域

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

若要在 C# 中從圖片的特定區域擷取文字,請使用 IronOCR 的 Rectangle 物件,透過指定 y 座標、widthheight 來定義精確區域,然後將其傳遞給 LoadImage 方法進行針對性的 OCR 處理。

快速入門:從特定影像區域擷取文字

  1. using NuGet 套件管理員安裝 https://www.nuget.org/packages/IronOcr

    PM > Install-Package IronOcr
  2. 請複製並執行此程式碼片段。

    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);
  3. 部署至您的生產環境進行測試

    立即透過免費試用,在您的專案中開始使用 IronOCR

    arrow pointer

通常,您只需從圖片的局部區域擷取文字,例如發票上的總金額或表單中的特定欄位。 掃描整份文件效率不高,且可能因擷取無關文字而導致錯誤。

IronOCR 允許您透過指定確切的矩形掃描區域,來提升精準度、效能與準確性。 本指南提供逐步操作說明,指導您如何定義特定的 OCR 區域、從中擷取文字,並透過視覺化方式驗證座標是否正確,以確保 OCR 任務順利執行。

開始使用 IronOCR


如何對特定區域執行 OCR?

若要定義特定的 OCR 區域,請從 IronSoftware.Drawing 命名空間建立 Rectangle 物件。 此物件需要四個數值:width 以及 height,單位皆為像素。 坐標 (x, y) 代表您指定區域的左上角。

當您使用 LoadImage 載入圖片時,請將此 Rectangle 作為第二個參數傳入。 IronOCR 隨後會將其 OCR 處理範圍限制在該邊界框內的像素區域內。

區域性 OCR 在處理結構化文件(如發票掃描表單身分證明文件)時特別有用,因為這類文件中的特定資訊總是出現在可預測的位置。 透過將 OCR 僅限於相關區域,您可以顯著提升處理速度,並減少來自無關文字的誤判。

若要找出您的 Rectangle 的座標,您可以使用像 MS Paint 這樣的簡易圖片編輯軟體。 開啟您的輸入圖片,將滑鼠懸停在指定區域的左上角與右下角,並記下 (x, y) 像素座標。 接著即可計算矩形的屬性:(x1, y1, width, height),其中 width = x1,且 height = y1

測試時應使用哪張圖片?

我們將使用包含三個段落的範例圖片。我們的目標是僅提取第二段,並忽略其餘文字。 這展示了常見的場景,即您需要從較大的文件中擷取特定欄位或區段。

顯示 OCR 結果的終端機視窗,包含標題

如何在程式碼中實作區域性 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
$vbLabelText   $csharpLabel

針對更複雜的場景,您可以在同一張圖像中定義多個區域。 這在處理文件中包含多個欄位或表格的表單時特別有用:

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
$vbLabelText   $csharpLabel

我可以期待什麼樣的成果?

如您從主控台輸出所見,僅有第二段落經過 OCR 處理。 這種針對性的方法可確保圖片中其他部分的不相關文字不會干擾您的翻譯結果。

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
$vbLabelText   $csharpLabel

視覺化呈現效果為何?

OCR 標示的輸出內容

淺藍色矩形確認我們已正確標示出第二段落以供處理。

何時該使用區域性 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 便會將 OCR 處理範圍限制在該定義區域內。

定義 OCR 區域而非掃描整張圖片有何好處?

透過 IronOCR 定義特定的 OCR 區域,您可以提升處理速度、提高準確度,並減少因擷取無關文字而產生的錯誤。這對於資訊出現在可預測位置的結構化文件特別有用。

建立區域 OCR 用的矩形時,需要哪些參數?

要為 IronOCR 的區域 OCR 建立一個矩形,您需要四個以像素為單位的數值:x 座標、y 座標、寬度和高度。座標 (x, y) 代表您所需掃描區域的左上角。

哪個命名空間包含用於 OCR 區域的 Rectangle 物件?

用於在 IronOCR 中定義 OCR 區域的 Rectangle 物件,位於 IronSoftware.Drawing 命名空間中。

哪些類型的文件最能從區域性 OCR 處理中受益?

IronOCR 的區域性 OCR 技術對於結構化文件(如發票、掃描表單及身分證明文件)特別有效,因這些文件中的特定資訊通常會固定出現在相同位置。

IronOCR 是否支援多種語言?

IronOCR 支援多種語言,使其成為適用於需要識別不同語言文字的全球應用程式的多功能工具。

IronOCR 能否整合至現有應用程式中?

IronOCR 設計上可輕鬆透過 C# 整合至現有應用程式中,讓開發人員能以最少的努力,為其軟體增添 OCR 功能。

使用 IronOCR 進行文件管理有哪些好處?

使用 IronOCR 進行文件管理,可將掃描文件轉換為可搜尋且可編輯的文字,從而簡化工作流程,減少人工資料輸入的需求,並提升文件的可存取性。

IronOCR 如何提升資料準確性?

IronOCR 透過其先進的辨識演算法與影像校正功能來提升資料準確性,確保文字擷取過程既可靠又精確。

IronOCR 是否有提供免費試用版?

是的,Iron Software 提供 IronOCR 的免費試用版,讓使用者能在決定購買前測試其功能與效能。

Curtis Chau
技術撰稿人

Curtis Chau 擁有卡爾頓大學(Carleton University)的電腦科學學士學位,專精於前端開發,並精通 Node.js、TypeScript、JavaScript 及 React。他熱衷於打造直觀且美觀的用戶介面,喜歡運用現代框架,並創建結構完善、視覺上吸引人的手冊。

除了開發工作之外,Curtis 對物聯網(IoT)抱有濃厚興趣,致力於探索整合硬體與軟體的創新方法。閒暇時,他喜歡玩遊戲和開發 Discord 機器人,將對科技的熱愛與創意相結合。

準備開始了嗎?
Nuget 下載 5,896,332 | 版本: 2026.5 just released
Still Scrolling Icon

還在往下捲動嗎?

想要快速確認成果嗎? PM > Install-Package IronOcr
執行範例 觀看您的圖片轉為可搜尋文字。