How to use Computer Vision to Find Text in C#

In this tutorial, we explore how to use computer vision to detect text in images using the Iron OCR library in C#. We begin by setting up a C# console application in Visual Studio, ensuring the Iron OCR and Iron Doomu Vision packages are installed via the NuGet Package Manager. First, we import the Iron OCR library to access OCR functionalities and create an instance of the Iron Tesa class for OCR operations. We open an input image file and define an OCR input object, using computer vision to detect text regions. The Read method of the Iron Tesa object is then used to perform OCR, storing results in a variable for display.

Below is a corrected and documented C# code snippet demonstrating this process:

// Import necessary namespaces
using IronOcr; // Namespace for Iron OCR library

class OCRExample
{
    static void Main(string[] args)
    {
        // Create an instance of the Iron Tesseract class for handling OCR operations
        var Ocr = new IronTesseract();

        // Specify the path to the image file to be processed
        string inputImagePath = @"C:\path\to\your\image.jpg";

        // Perform OCR on the image
        using (var Input = new OcrInput(inputImagePath))
        {
            // Detect and read the text from the image using the Read method
            var Result = Ocr.Read(Input);

            // Output the detected text to the console
            Console.WriteLine(Result.Text);
        }
    }
}
// Import necessary namespaces
using IronOcr; // Namespace for Iron OCR library

class OCRExample
{
    static void Main(string[] args)
    {
        // Create an instance of the Iron Tesseract class for handling OCR operations
        var Ocr = new IronTesseract();

        // Specify the path to the image file to be processed
        string inputImagePath = @"C:\path\to\your\image.jpg";

        // Perform OCR on the image
        using (var Input = new OcrInput(inputImagePath))
        {
            // Detect and read the text from the image using the Read method
            var Result = Ocr.Read(Input);

            // Output the detected text to the console
            Console.WriteLine(Result.Text);
        }
    }
}
' Import necessary namespaces
Imports IronOcr ' Namespace for Iron OCR library

Friend Class OCRExample
	Shared Sub Main(ByVal args() As String)
		' Create an instance of the Iron Tesseract class for handling OCR operations
		Dim Ocr = New IronTesseract()

		' Specify the path to the image file to be processed
		Dim inputImagePath As String = "C:\path\to\your\image.jpg"

		' Perform OCR on the image
		Using Input = New OcrInput(inputImagePath)
			' Detect and read the text from the image using the Read method
			Dim Result = Ocr.Read(Input)

			' Output the detected text to the console
			Console.WriteLine(Result.Text)
		End Using
	End Sub
End Class
$vbLabelText   $csharpLabel

The tutorial also covers using the Crop Rectangle class to focus on identified text regions, applying a red stamp for visual inspection, and further processing with the Read method. Additionally, we demonstrate detecting multiple text regions, dividing input into separate images, and using the GetTextRegions method for comprehensive text extraction. With the right settings and input files, Iron OCR combined with computer vision can become a potent tool for text detection. The tutorial concludes by encouraging viewers to download a trial version of Iron OCR for further exploration.

Further Reading: How to use Computer Vision to Find Text

Kannaopat Udonpant
Software Engineer
Before becoming a Software Engineer, Kannapat completed a Environmental Resources PhD from Hokkaido University in Japan. While pursuing his degree, Kannapat also became a member of the Vehicle Robotics Laboratory, which is part of the Department of Bioproduction Engineering. In 2022, he leveraged his C# skills to join Iron Software's engineering team, where he focuses on IronPDF. Kannapat values his job because he learns directly from the developer who writes most of the code used in IronPDF. In addition to peer learning, Kannapat enjoys the social aspect of working at Iron Software. When he's not writing code or documentation, Kannapat can usually be found gaming on his PS5 or rewatching The Last of Us.
< PREVIOUS
How to Read PDFs with IronOCR
NEXT >
How to Read Barcodes and QR Codes Using OCR in C#