如何在C#中定義圖片的特定OCR區域
若要在 C# 中擷取影像特定區域的文字,請用 IronOCR 的 Rectangle 物件指定 x/y 座標、width 和 height,再將此物件傳給 LoadImage 方法,只對該區域執行 OCR。
-
1使用 IronOCR 安裝 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);Imports IronOcr Imports IronSoftware.Drawing ' 1. Install IronOCR via NuGet: Install-Package IronOcr Dim ocr = New IronTesseract() Using input = New OcrInput() ' 2. Create a Rectangle with coordinates Dim region = New Rectangle(x:=215, y:=1250, width:=1335, height:=280) ' 3. Load image with region input.LoadImage("image.png", region) ' 4. Extract text Dim result = ocr.Read(input) Console.WriteLine(result.Text) End UsingImports IronOcr Imports IronSoftware.Drawing ' 1. Install IronOCR via NuGet: Install-Package IronOcr Dim ocr = New IronTesseract() Using input = New OcrInput() ' 2. Create a Rectangle with coordinates Dim region = New Rectangle(x:=215, y:=1250, width:=1335, height:=280) ' 3. Load image with region input.LoadImage("image.png", region) ' 4. Extract text Dim result = ocr.Read(input) Console.WriteLine(result.Text) End Using -
3部署到您的生產環境進行測試
立即在您的項目中開始使用 IronOCR,並使用 免費試用
通常,您只需要擷取影像中一小部分的文字,例如發票總金額或表單中的特定欄位。掃描整份文件效率較低,也可能因辨識到無關文字而出錯。
您可以在 IronOCR 中指定要掃描的矩形區域,以提升辨識精確度、效能和準確度。本指南將逐步示範如何定義 OCR 區域、從中擷取文字,並以圖像確認座標是否正確。
IronOCR 入門指南
如何定義影像中的特定OCR區域
- 下載用於定義OCR區域的C#程式庫
- 建立 OCR 引擎執行個體
- 以矩形框標示OCR區域。
- 使用
LoadImage載入影像,並傳入定義好的矩形區域 - 存取OcrResult屬性以查看和操作擷取的資料
如何在特定區域執行OCR?
要定義特定的 OCR 區域,請建立 IronSoftware.Drawing 命名空間中的 Rectangle 物件。此物件需要四個值:x 座標、y 座標、width 和 height,單位皆為像素。座標(x、y)代表目標區域的左上角。
使用 LoadImage 載入影像時,請將 Rectangle 當作第二個參數傳入。IronOCR 只會辨識該矩形框內的像素。
區域 OCR 特別適合處理發票、掃描表單或身分證明文件等結構化文件,因為特定資訊通常出現在固定位置。只辨識相關區域,可以大幅加快處理速度,減少無關文字造成的誤判。
您可以使用 MS Paint 等簡單的影像編輯器取得 Rectangle 的座標。開啟輸入影像,將滑鼠游標分別移到指定區域的左上角和右下角,記下像素座標(x、y)。接著計算矩形的屬性(x1、y1、width、height),其中 width = x2-x1,height = y2-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);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 UsingImports 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}");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 = New IronTesseract()
' Define multiple regions for different form fields
Dim nameField = New Rectangle(x:=100, y:=200, width:=300, height:=50)
Dim dateField = New Rectangle(x:=100, y:=300, width:=200, height:=50)
Dim amountField = 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 = New OcrInput()
input.LoadImage("form.png", nameField)
nameResult = ocr.Read(input)
End Using
Dim dateResult As OcrResult
Using input = New OcrInput()
input.LoadImage("form.png", dateField)
dateResult = ocr.Read(input)
End Using
Dim amountResult As OcrResult
Using input = 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}")Imports IronOcr
Imports IronSoftware.Drawing
Dim ocr = New IronTesseract()
' Define multiple regions for different form fields
Dim nameField = New Rectangle(x:=100, y:=200, width:=300, height:=50)
Dim dateField = New Rectangle(x:=100, y:=300, width:=200, height:=50)
Dim amountField = 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 = New OcrInput()
input.LoadImage("form.png", nameField)
nameResult = ocr.Read(input)
End Using
Dim dateResult As OcrResult
Using input = New OcrInput()
input.LoadImage("form.png", dateField)
dateResult = ocr.Read(input)
End Using
Dim amountResult As OcrResult
Using input = 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 另存新檔。這樣有助於除錯和微調座標,以達到最佳效能。
處理複雜版面或為了確認品質而需要標示特定文字區域時,這種視覺化方式特別有用。
這是在上面的範例輸入影像上繪製指定邊界框後的輸出影像。
如何標示選取的區域?
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");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 UsingImports 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#定義圖像中的特定OCR區域?
要在C#中定義特定的OCR區域,請使用IronOCR的Rectangle物件,指定x/y座標、寬度和高度。然後將此矩形傳遞給LoadImage方法進行目標OCR處理。
為什麼定義特定的OCR區域有好處?
指定特定的OCR區域有助於提高精準度和性能,專注於圖像的相關部分,防止提取無關文本並減少錯誤。
在C#中對特定圖像區域進行OCR需要哪些程式庫?
您將需要IronOCR和IronSoftware.Drawing程式庫。IronOCR通過NuGet安裝,允許您對預定義的圖像區域進行OCR。
如何在C#應用程式中指定OCR的矩形坐標?
要指定矩形坐標,請使用圖像編輯器來確定左上角的x/y坐標,並設置所需區域的寬度和高度(以像素為單位)。
我可以使用IronOCR從圖像中的多個區域提取文本嗎?
可以,IronOCR允許您在同一圖像中定義和處理多個矩形區域,非常適合處理多個字段的表單。
能否在輸入圖像上可視化OCR區域?
是的,您可以使用StampCropRectangleAndSaveAs方法在輸入圖像上繪製指定的矩形,以幫助驗證座標。
區域性OCR特別適用於哪些應用程式?
區域性OCR適用於表單處理、發票掃描、車牌識別、身份文件分析以及從截圖中提取UI元素的文本。
在文件處理中使用區域性OCR的一些最佳實踐是什麼?
最佳實踐包括在文本周圍添加填充、使用樣本圖像進行測試、處理文件的輕微變化、使用多執行緒優化性能以及監控結果信心分數。
如何提高IronOCR的區域掃描準確性?
通過使用高分辨率圖像、確保正確的文本方向、應用對比度和清晰度過濾器以及視覺上驗證目標區域來提高準確性。
IronOCR可以與不同的圖像格式一起使用嗎?
可以,IronOCR支持包括JPG、PNG、GIF、TIFF和BMP在內的多種圖像格式,使其在不同的掃描和處理需求中具備靈活性。
Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。