如何在C#中定義圖像的特定OCR區域
要從C#中圖像的特定區域提取文字,使用IronOCR的LoadImage方法進行目標OCR處理。
-
1Install IronOCR with NuGet Package Manager
-
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部署以在您的實時環境中測試
今天就開始在您的專案中使用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等各種圖像格式。
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();
// 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}");
我可以預期什麼結果?
正如您從控制台輸出中看到的,只有第二段落由OCR處理。 這種針對性的方法確保來自圖像其他部分的不相關文字不會干擾您的結果。

區域性OCR的準確性取決於多個因素:
我如何驗證我的坐標正確?
為了確保您選擇的輸入圖像坐標正確,您可以將所定義的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");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適用於幾個常見情景:
- 表單處理:當從標準化表單中提取特定字段,資料出現在一致的位置時。
- 發票處理:提取特定值如總數、日期或發票號碼,而不需處理整個文件。
- 車牌號:當使用車牌識別時,僅關注車牌區域。
- 身份文件:提取如護照或身份證上的特定字段。
- 截圖:從截圖中的特定UI元素中捕捉文字。
區域性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對於如發票、掃描表單和身份文件等結構化文件特別有效,因為具體資訊在相同位置一致出現。
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擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。