IRONSOFTWAREHOME

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

Curtis Chau
Curtis Chau
Updated: 2026年6月4日

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

快速入門:從特定圖像區域提取文字
  1. 1Install IronOCR with NuGet Package Manager

    PM > Install-Package IronOcr

  2. 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);
    C#
  3. 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等各種圖像格式。

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);

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

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}");
C#

我可以預期什麼結果?

正如您從控制台輸出中看到的,只有第二段落由OCR處理。 這種針對性的方法確保來自圖像其他部分的不相關文字不會干擾您的結果。

OCR 結果

區域性OCR的準確性取決於多個因素:

  • 圖像質量:更高解析度的圖像通常會產生更好的結果。 考慮使用DPI設置來優化您的圖像。
  • 文字方向:確保文字方向正確。 如有需要,使用頁面旋轉檢測
  • 對比度和清晰度:應用圖像校正濾鏡以提高文字可讀性。

我如何驗證我的坐標正確?

為了確保您選擇的輸入圖像坐標正確,您可以將所定義的ContentArea可視化。 實現這一點的簡單方法是將矩形畫在輸入圖像上,然後將其另存為StampCropRectangleAndSaveAs的新文件。 這有助於您除錯和微調坐標以獲得最佳性能。

這種視覺化技術在處理複雜布局或需要突出顯示特定文字區域以達到品質保證目的時特別有用。

在我們上面的範例輸入圖像上繪製指定框後的輸出圖像。

我如何可視化所選區域?

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");

視覺化結果是什麼樣的?

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對於如發票、掃描表單和身份文件等結構化文件特別有效,因為具體資訊在相同位置一致出現。

Is it possible to visualize the OCR region on the input image?

Yes, you can visualize the OCR region by using the `StampCropRectangleAndSaveAs` method to draw the specified rectangle on the input image, aiding in coordinate verification.

For what applications is regional OCR particularly useful?

Regional OCR is useful for form processing, invoice scanning, license plate recognition, identity document analysis, and extracting text from UI elements in screenshots.

What are some best practices for using regional OCR in document processing?

Best practices include adding padding around text, testing with sample images, handling slight document variations, optimizing performance with multithreading, and monitoring result confidence scores.

How can I improve the accuracy of IronOCR's regional scanning?

Improve accuracy by using high-resolution images, ensuring proper text orientation, applying contrast and clarity filters, and verifying the targeted region visually.

Can IronOCR be used with different image formats?

Yes, IronOCR supports various image formats including JPG, PNG, GIF, TIFF, and BMP, making it versatile for different scanning and processing needs.

Curtis Chau
技術作家

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

...
閱讀更多

準備開始了嗎?

Nuget Downloads 6,236,385版本:2026.9剛剛發布

立即獲取您的30天試用金鑰
無需信用卡或帳戶建立
C# PDF的NuGet程式庫
使用NuGet安裝

版本: 2026.9

PM > Install-Package IronOcr
nuget.org/packages/IronOcr/
  1. 在解決方案資源管理器中,右鍵點擊參考,管理NuGet包
  2. 選擇瀏覽並搜尋"IronOCR"
  3. 選擇包並安裝
C# PDF DLL
下載 DLL

版本: 2026.9

這裡下載Windows安裝程式。

  1. 下載並解壓IronOCR至您的方案目錄下的~/Libs等位置
  2. 在Visual Studio解決方案資源管理器中,右鍵點擊參考。選擇瀏覽,"IronOCR.dll"

授權從$999

有問題嗎?聯絡我們的開發團隊。

Key in blue circle

立即免費取得 30 天試用金鑰

Your trial license will be sent to your email address

無任何限制。100% 解鎖。無需信用卡。

bullet_checked無需信用卡或建立帳號無任何限制。100% 解鎖。無需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
獲取您的無義務諮詢
填寫以下表格或發送電子郵件至sales@ironsoftware.com
您的詳細資訊將始終保密。
被全球數百萬工程師信任
Iron Software的客戶標誌
立即獲取您的30天試用金鑰
無需信用卡或帳戶建立