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` 属性访问,从而允许您根据需要查看和操作提取的数据。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

准备开始了吗?
Nuget 下载 5,044,537 | 版本: 2025.11 刚刚发布