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

Nutzen Sie IronOCR heute kostenlos in Ihrem Projekt.

Erster Schritt:
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.

TippsTo 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.

Häufig gestellte Fragen

Warum sollte ich einen bestimmten OCR-Bereich eines Bildes definieren?

Die Definition eines bestimmten OCR-Bereichs eines Bildes ist hilfreich, um die Präzision und Genauigkeit zu verbessern, indem man sich auf den relevanten Bereich konzentriert, beispielsweise auf den Gesamtbetrag einer Rechnung, anstatt das gesamte Dokument zu scannen, was zu Fehlern führen kann.

Wie kann ich mit IronOCR einen OCR-Bereich festlegen?

In IronOCR können Sie einen OCR-Bereich festlegen, indem Sie ein `Rectangle`-Objekt mit den gewünschten Koordinaten und Abmessungen erstellen. Dieses Rechteck übergeben Sie dann an die `LoadImage`-Methode, um die OCR-Verarbeitung auf diesen spezifischen Bereich zu beschränken.

Welche Parameter sind für die Definition einer OCR-Region erforderlich?

Um einen OCR-Bereich zu definieren, müssen Sie vier Parameter für das `Rectangle`-Objekt angeben: x-Koordinate, y-Koordinate, Breite und Höhe. Diese Parameter bestimmen die obere linke Ecke und die Größe des Bereichs.

Wie kann ich sicherstellen, dass die richtigen Koordinaten für die OCR ausgewählt werden?

Um die korrekten Koordinaten sicherzustellen, visualisieren Sie den definierten Inhaltsbereich, indem Sie mit `StampCropRectangleAndSaveAs` ein Rechteck auf das Eingabebild zeichnen. Dies erleichtert das Debuggen und die Feinabstimmung der Koordinaten.

Welche Werkzeuge kann ich verwenden, um die Koordinaten für meinen OCR-Bereich zu finden?

Sie können ein einfaches Bildbearbeitungsprogramm wie MS Paint verwenden, um die Koordinaten für Ihren OCR-Bereich zu ermitteln. Bewegen Sie den Mauszeiger über die obere linke und die untere rechte Ecke des gewünschten Bereichs, um die (x, y)-Pixelkoordinaten zu notieren.

Welchen Vorteil bietet die Verwendung von IronOCR für regionsspezifische OCR-Aufgaben?

IronOCR ermöglicht es Ihnen, Text effizient aus bestimmten Bereichen eines Bildes zu extrahieren, wodurch Fehler reduziert und die Leistung verbessert werden, indem die Erfassung von irrelevantem Text verhindert wird.

Kann ich den OCR-Bereich vor der Verarbeitung visuell überprüfen?

Ja, Sie können den OCR-Bereich visuell überprüfen, indem Sie ein Rechteck auf das Eingabebild zeichnen. Dies bestätigt, dass der richtige Bereich für die Verarbeitung isoliert ist und gewährleistet somit die Genauigkeit.

Welche Arten von Bildern kann ich mit IronOCR verarbeiten?

IronOCR kann eine Vielzahl von Bildformaten verarbeiten und ermöglicht es Ihnen, Text aus bestimmten Bereichen von Bildern zu extrahieren, die in Dokumenten, Formularen und Rechnungen verwendet werden.

Wie handhabt IronOCR den OCR-Prozess innerhalb einer bestimmten Region?

IronOCR beschränkt seinen OCR-Prozess auf Pixel innerhalb des angegebenen Begrenzungsrahmens und stellt so sicher, dass nur der Text innerhalb des definierten Bereichs extrahiert wird.

Welches Ausgabeformat liefert der OCR-Prozess in IronOCR?

Das Ausgabeformat des OCR-Prozesses in IronOCR ist über die `OcrResult`-Eigenschaft zugänglich, sodass Sie die extrahierten Daten nach Bedarf anzeigen und bearbeiten können.

Curtis Chau
Technischer Autor

Curtis Chau hat einen Bachelor-Abschluss in Informatik von der Carleton University und ist spezialisiert auf Frontend-Entwicklung mit Expertise in Node.js, TypeScript, JavaScript und React. Leidenschaftlich widmet er sich der Erstellung intuitiver und ästhetisch ansprechender Benutzerschnittstellen und arbeitet gerne mit modernen Frameworks sowie der Erstellung gut strukturierter, optisch ansprechender ...

Weiterlesen
Bereit anzufangen?
Nuget Downloads 5,044,537 | Version: 2025.11 gerade veröffentlicht