OCR a Region of an Image

This example demonstrates a 41% speed improvement by choosing a specific area of an image to perform OCR (Optical Character Recognition) in .NET. These are called ContentAreas or CropAreas.

Here is an example implementation in C#:

using IronOcr;
using System.Drawing;

class Program
{
    static void Main(string[] args)
    {
        // Create an instance of IronTesseract
        var ocr = new IronTesseract();

        // Specify the path to your image file
        string imagePath = "path/to/your/image.jpg";

        // Define the specific area of the image to crop and perform OCR
        var cropArea = new CropRectangle()
        {
            X = 100, // X-coordinate of the top-left corner of the crop area
            Y = 50,  // Y-coordinate of the top-left corner of the crop area
            Width = 200, // Width of the crop area
            Height = 100 // Height of the crop area
        };

        // Load the image and specify the crop area
        using (var input = new OcrInput(imagePath))
        {
            input.Crop(cropArea);

            // Perform OCR within the specified crop area
            var result = ocr.Read(input);

            // Output the recognized text
            Console.WriteLine(result.Text);
        }
    }
}
using IronOcr;
using System.Drawing;

class Program
{
    static void Main(string[] args)
    {
        // Create an instance of IronTesseract
        var ocr = new IronTesseract();

        // Specify the path to your image file
        string imagePath = "path/to/your/image.jpg";

        // Define the specific area of the image to crop and perform OCR
        var cropArea = new CropRectangle()
        {
            X = 100, // X-coordinate of the top-left corner of the crop area
            Y = 50,  // Y-coordinate of the top-left corner of the crop area
            Width = 200, // Width of the crop area
            Height = 100 // Height of the crop area
        };

        // Load the image and specify the crop area
        using (var input = new OcrInput(imagePath))
        {
            input.Crop(cropArea);

            // Perform OCR within the specified crop area
            var result = ocr.Read(input);

            // Output the recognized text
            Console.WriteLine(result.Text);
        }
    }
}
Imports IronOcr
Imports System.Drawing

Friend Class Program
	Shared Sub Main(ByVal args() As String)
		' Create an instance of IronTesseract
		Dim ocr = New IronTesseract()

		' Specify the path to your image file
		Dim imagePath As String = "path/to/your/image.jpg"

		' Define the specific area of the image to crop and perform OCR
		Dim cropArea = New CropRectangle() With {
			.X = 100,
			.Y = 50,
			.Width = 200,
			.Height = 100
		}

		' Load the image and specify the crop area
		Using input = New OcrInput(imagePath)
			input.Crop(cropArea)

			' Perform OCR within the specified crop area
			Dim result = ocr.Read(input)

			' Output the recognized text
			Console.WriteLine(result.Text)
		End Using
	End Sub
End Class
$vbLabelText   $csharpLabel

Explanation

  • IronTesseract: This is the main class provided by the IronOCR library, which is used to perform OCR on images.
  • CropRectangle: This object defines the rectangular area of the image you want to focus OCR processing on, making the process more efficient.
  • AddImage: The image is added along with the crop details specifying which part of the image to perform OCR on.
  • Read: This method executes the OCR process, returning the recognized text from the specified crop area.

Ensure you replace "path/to/your/image.jpg" with the actual file path of the image you wish to process.