IronOCR 操作指南 圖片的區域 如何在 C# 中定義影像的特定 OCR 區域;。 Curtis Chau 更新:2026年1月10日 下載 IronOCR NuGet 下載 DLL 下載 Windows 安裝程式 開始免費試用 法學碩士副本 法學碩士副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在 Grok 中打開 向 Grok 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 This article was translated from English: Does it need improvement? Translated View the article in English 要在 C# 中從圖像的特定區域擷取文字,請使用 IronOCR 的 Rectangle 物件,透過指定 x/y 座標、width 和 height 來定義精確的區域,然後將其傳送至 LoadImage 方法,以進行有針對性的 OCR 處理。 快速入門:從特定影像區域擷取文字。 立即開始使用 NuGet 建立 PDF 檔案: 使用 NuGet 套件管理器安裝 IronOCR PM > Install-Package IronOcr 複製並運行這段程式碼。 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); 部署到您的生產環境進行測試 立即開始在您的專案中使用 IronOCR,免費試用! 免費試用30天 通常情況下,你只需要從圖像的一小部分中提取文本,例如發票上的總金額或表單中的特定欄位。 掃描整個文件效率低下,並且可能因捕獲無關文字而引入錯誤。 IronOCR 可讓您透過指定要掃描的精確矩形區域來提高精確度、效能和準確性。 本指南提供了逐步演練,指導您如何定義特定的 OCR 區域、從中提取文本,並透過視覺方式驗證您的座標是否適用於您的 OCR 任務。 IronOCR 入門指南 ## 如何定義影像中的特定OCR區域 下載用於定義 OCR 區域的 C# 庫 實例化 OCR 引擎 以矩形框標示OCR區域。 使用LoadImage載入圖像以及定義的矩形區域 存取 OcrResult 屬性,以檢視和操作擷取的資料 如何在特定區域執行 OCR? 若要定義特定的 OCR 區域,您可以從 IronSoftware.Drawing 命名空間建立一個Rectangle物件。 此物件需要四個值:x座標、y座標、width和height,全部以像素為單位。 (x, y) 座標代表您所需區域的左上角。 當您使用LoadImage載入映像時,您需要將此Rectangle作為第二個參數傳遞。 IronOCR 隨後會將 OCR 過程限制在該邊界框內的像素範圍內。 區域 OCR 在處理結構化文件時特別有用,例如 發票、掃描表單,或 身分證明文件,在這些文件中,特定資訊總是出現在可預測的位置。 透過將 OCR 限制在相關區域,您可以大幅提升處理速度,並降低不相關文字所造成的誤判。 若要尋找 Rectangle 的座標,您可以使用 MS Paint 等簡單的影像編輯器。 開啟輸入的影像,將滑鼠懸停在指定區域的左上角和右下角,並記下 (x, y) 像素座標。 然後您可以計算矩形的屬性:(x1, y1, width, height), 其中 width = x2-x1 和 height = y2-y1. 測試時應使用何種圖像? 我們將使用一張包含三個段落的範例圖片。我們的目標是只提取第二個段落,忽略其餘文本。 這展示了一種常見的情況,您需要從較大的文件中抽取特定欄位或部分。 如何在程式碼中實現區域 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 的精確度取決於幾個因素: 影像品質:解析度較高的影像通常會產生較好的結果。 請考慮使用 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 是幾種常見情況的理想選擇: 1.表單處理:從資料出現在一致位置的標準化表格中抽取特定欄位時。 2.發票處理:在不處理整個文件的情況下,擷取特定值,例如總計、日期或發票號碼。 3.授權牌:使用 車牌辨識時,請僅著重於車牌區域。 4.身分證明文件:從護照或身分證中擷取特定欄位。 5.螢幕截圖:在 螢幕快照中擷取特定 UI 元素的文字時。 區域 OCR 的最佳實務 為了達到區域 OCR 的最佳效果: 1.加入襯墊:在您的文字周圍加入一小段緩衝區,以確保不會在邊緣切掉任何字元。 2.使用樣本影像進行測試:在進行大批量處理之前,請務必使用具有代表性的樣品來驗證您的座標。 3.處理變化:考慮到掃描文件中輕微的位置變化,請將您的區域做得比需要的稍微大一些。 4.最佳化效能:對於 多執行緒處理,請平行處理不同的區域。 5.監控信心:檢查結果置信度分數以確保準確性。 透過將 OCR 處理集中在特定區域,您可以大幅提升文字擷取任務的速度與精確度。 這種有針對性的方法對於在您的 .NET 應用程式中建立有效率的文件處理工作流程至關重要。 常見問題解答 如何在 C# 中僅從圖像的特定部分擷取文字? 使用 IronOCR,您可以透過建立一個具有 x/y 座標、寬度和高度值的 Rectangle 物件,從特定區域擷取文字。將此 Rectangle 作為 LoadImage 方法的第二個參數傳送給 IronOCR,IronOCR 就會將其 OCR 處理限制在僅限於該定義的區域。 定義 OCR 區域而非掃描整個影像有什麼好處? 透過 IronOCR 定義特定的 OCR 區域,您可以提高處理速度、增加精確度,並減少擷取無關文字所造成的錯誤。這對於資訊出現在可預測位置的結構化文件尤其有用。 我需要哪些參數才能建立區域 OCR 的矩形? 要為 IronOCR 的區域 OCR 建立一個矩形,您需要四個像素值:x 座標、y 座標、寬度和高度。(x, y) 座標代表您所需掃描區域的左上角。 哪個命名空間包含用於 OCR 區域的 Rectangle 物件? IronOCR 中用於定義 OCR 區域的矩形物件位於 IronSoftware.Drawing 命名空間中。 哪些類型的文件最受益於區域 OCR 處理? IronOCR 的區域 OCR 對於結構化文件尤其有效,例如發票、掃描表格和身份文件,這些文件中的特定資訊會持續出現在相同的位置。 Curtis Chau 立即與工程團隊聊天 技術撰稿人 Curtis Chau 擁有電腦科學學士學位(卡爾頓大學),專長於前端開發,精通 Node.js、TypeScript、JavaScript 和 React。Curtis 對製作直覺且美觀的使用者介面充滿熱情,他喜歡使用現代化的架構,並製作結構良好且視覺上吸引人的手冊。除了開發之外,Curtis 對物聯網 (IoT) 也有濃厚的興趣,他喜歡探索整合硬體與軟體的創新方式。在空閒時間,他喜歡玩遊戲和建立 Discord bots,將他對技術的熱愛與創意結合。 準備好開始了嗎? Nuget 下載 5,384,824 | 版本: 2026.2 剛剛發布 免費 NuGet 下載 總下載量:5,384,824 查看許可證