How to Define a Specific OCR Region of an Image

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

Often, you only need to extract text from a small part of an image, such as a total amount on an invoice or a specific field from a form. Scanning the full document is inefficient and can introduce errors by capturing irrelevant text.

IronOCR allows you to improve precision, performance, and accuracy by specifying the exact rectangular region to scan. This guide provides a step-by-step walkthrough on how to define a specific OCR region, extract text from it, and visually verify that your coordinates are correct for your OCR tasks.

Get Started with IronOCR

立即開始在您的項目中使用 IronOCR 並免費試用。

第一步:
green arrow pointer


Perform OCR on Specific Region

To define a specific OCR region, you create a Rectangle object from the IronSoftware.Drawing namespace. This object requires four values: the x-coordinate, the y-coordinate, the width, and the height, all in pixels. The (x, y) coordinates represent the top-left corner of your desired area.

When you load your image using LoadImage, you pass this Rectangle as the second parameter. IronOCR will then restrict its OCR process to only the pixels within that bounding box.

提示To find the coordinates for your Rectangle, you can use a simple image editor like MS Paint. Open your input image, hover your mouse over the top-left and bottom-right corners of the specified region, and note the (x, y) pixel coordinates. You can then calculate the rectangle's properties: (x1, y1, width, height), where width = x2-x1 and height = y2-y1.

OCR Input

We'll use a sample image with three paragraphs. Our goal is to extract only the second paragraph and ignore the rest of the text.

OCR Input

Code

: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);
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Output

As you can see from the console output, only the second paragraph is processed by the OCR.

OCR Output

Verifying the OCR region of an Image

To ensure you've selected the correct coordinates for the input image, you can visualize the ContentArea you defined. A simple way to do this is to draw the rectangle on the input image and save it as a new file with StampCropRectangleAndSaveAs. This helps you debug and fine-tune the coordinates for optimal performance.

Here is the output image after drawing the specified bounding box on our example input image from above.

Code

: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");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Output

OCR Highlighted Output

The light blue rectangle confirms that we have correctly isolated the second paragraph for processing.

常見問題解答

為什麼要定義影像中的特定OCR區域?

定義影像的特定 OCR 區域有助於提高精度和準確性,方法是專注於感興趣的區域(例如發票上的總金額),而不是掃描整個文檔,這可以避免出錯。

如何使用 IronOCR 指定 OCR 區域?

在 IronOCR 中,您可以透過建立一個具有所需座標和尺寸的 `Rectangle` 物件來指定 OCR 區域。然後,您可以將此矩形傳遞給 `LoadImage` 方法,從而將 OCR 處理限制在該特定區域內。

定義OCR區域需要哪些參數?

要定義OCR區域,您需要為`Rectangle`物件提供四個參數:x座標、y座標、寬度和高度。這些參數決定了區域的左上角和大小。

如何確保為OCR選擇正確的座標?

為了確保座標正確,請使用 `StampCropRectangleAndSaveAs` 在輸入影像上繪製矩形來視覺化已定義的內容區域。這有助於調試和微調座標。

我可以使用哪些工具來尋找 OCR 區域的座標?

您可以使用像 MS Paint 這樣的簡單影像編輯器來尋找 OCR 區域的座標。將滑鼠懸停在所需區域的左上角和右下角,即可記下 (x, y) 像素座標。

使用 IronOCR 進行特定區域的 OCR 任務有什麼好處?

IronOCR 能夠有效率地從影像的特定區域提取文本,透過防止捕獲無關文字來減少錯誤並提高效能。

我可以在處理之前目視確認 OCR 區域嗎?

是的,您可以透過在輸入影像上繪製矩形來直觀地驗證 OCR 區域。這可以確認已正確提取出需要處理的區域,從而確保準確性。

IronOCR可以處理哪些類型的影像?

IronOCR 可以處理各種圖像格式,讓您可以從文件、表格和發票中使用的圖像的特定區域提取文字。

IronOCR 如何處理指定區域內的 OCR 流程?

IronOCR 將其 OCR 過程限制在指定邊界框內的像素,確保只提取定義區域內的文字。

IronOCR中OCR製程的輸出格式是什麼?

IronOCR 中 OCR 過程的輸出格式可透過 `OcrResult` 屬性訪問,從而允許您根據需要查看和操作提取的資料。

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'name'

Filename: sections/author_component.php

Line Number: 18

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 18
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'title'

Filename: sections/author_component.php

Line Number: 38

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 38
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: Illegal string offset 'comment'

Filename: sections/author_component.php

Line Number: 48

Backtrace:

File: /var/www/ironpdf.com/application/views/main/sections/author_component.php
Line: 48
Function: _error_handler

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 63
Function: view

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 64
Function: main_view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/how-to/index.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 552
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Howto.php
Line: 31
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

準備好開始了嗎?
Nuget 下載 5,044,537 | 版本: 2025.11 剛剛發布