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

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

要從C#中圖像的特定區域提取文字,使用IronOCR的LoadImage方法進行目標OCR處理。

快速入門:從特定圖像區域提取文字

  1. 使用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區域,您需要從Rectangle物件。 此物件需要四個值:height,所有值均以像素為單位。 (x, y)坐標代表您所需區域的左上角。

當您使用Rectangle作為第二個參數傳遞。 IronOCR將只限制OCR過程在該凸邊框內的像素。

在處理如發票掃描表單身份文件等結構化文件時,區域性OCR特別有用,因為特定資訊總是出現在可預測的位置。 通過將OCR限制在相關區域,您可以大幅提高處理速度並減少因不相關文字產生的誤報。

要查找您的Rectangle坐標,您可以使用如MS Paint這樣的簡單圖片編輯器。 打開您的輸入圖像,將滑鼠懸停在指定區域的左上角和右下角,並記下(x, y)像素坐標。 然後您可以計算矩形的屬性:(x1, y1, width, width = height = y1

我應該使用什麼圖像進行測試?

我們將使用帶有三個段落的樣品圖像。我們的目標是僅提取第二段落而忽略其餘文字。 這演示了一個常見情景,您需要從較大的文件中提取特定字段或部分。

終端顯示OCR結果,包含"Hello World"標題和有關書店的提取文字

我如何在程式碼中實施區域性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

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

: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}")
$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座標、寬度和高度值的矩形物件從特定區域提取文字。將這個矩形作為第二個參數傳給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的免費試用版,允許使用者在做出購買決定前測試其功能和能力。

Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

除了開發,Curtis對物聯網(IoT)有濃厚的興趣,探索創新的方法來整合硬體和軟體。在空閒時間,他喜歡玩遊戲和建立Discord機器人,結合他對技術的熱愛與創造力。

準備開始了嗎?
Nuget 下載 6,151,372 | 版本: 2026.7 剛剛發布
Still Scrolling Icon

還在滾動?

想要快速證明? PM > Install-Package IronOcr
執行範例 觀看您的圖像轉變為可搜尋文字。