IronOCR.ComputerVision Installation via NuGet Package

OpenCV methods that perform Computer Vision in IronOCR are visible in the regular IronOCR NuGet package.

Use of these methods requires NuGet installation of IronOcr.ComputerVision to the solution. You are prompted to download it if you do not have it installed.

  • Windows: IronOcr.ComputerVision.Windows
  • Linux: IronOcr.ComputerVision.Linux
  • macOS: IronOcr.ComputerVision.MacOS
  • macOS ARM: IronOcr.ComputerVision.MacOS.ARM

Install using the NuGet Package Manager or paste the following in the Package Manager Console:

Install-Package IronOcr.ComputerVision.Windows

This will provide the necessary assemblies to use IronOCR Computer Vision with our model file.

Functionality and API

Code Examples are included further down this tutorial. Here is a general overview of the methods that are currently available:

Method Explanation
FindTextRegion Detect regions which contain text elements and instruct Tesseract to only search for text within the area in which text was detected.
FindMultipleTextRegions Detect areas which contain text elements and divide the page into separate images based on text regions.
GetTextRegions Scans the image and returns a list of text regions as `List`.

FindTextRegion

Usage of FindTextRegion will use computer vision to detect regions which contain text elements on every page of an OcrInput object.

:path=/static-assets/ocr/content-code-examples/how-to/computer-vision-findtextregion-1.cs
using IronOcr;

// Initialize an instance of IronTesseract for OCR processing
var ocr = new IronTesseract();

// Create an OcrInput instance to hold the image being processed
using var input = new OcrInput();

// Load an image from the specified file path to be processed
// Ensure this file path is correct and accessible
input.AddImage("/path/to/file.png");

// Perform OCR on the input image to extract text region and process it
OcrResult result = ocr.Read(input);

// Retrieve and store the extracted text from the OCR result
string resultText = result.Text;

// Add additional operations as needed to work with the extracted text
// For example:
// Console.WriteLine(resultText);
Imports IronOcr



' Initialize an instance of IronTesseract for OCR processing

Private ocr = New IronTesseract()



' Create an OcrInput instance to hold the image being processed

Private input = New OcrInput()



' Load an image from the specified file path to be processed

' Ensure this file path is correct and accessible

input.AddImage("/path/to/file.png")



' Perform OCR on the input image to extract text region and process it

Dim result As OcrResult = ocr.Read(input)



' Retrieve and store the extracted text from the OCR result

Dim resultText As String = result.Text



' Add additional operations as needed to work with the extracted text

' For example:

' Console.WriteLine(resultText);
$vbLabelText   $csharpLabel

Can optionally be called with custom parameters:

:path=/static-assets/ocr/content-code-examples/how-to/computer-vision-findtextregion-2.cs
using IronOcr;

// Initialize the IronTesseract OCR engine
var ocr = new IronTesseract();

// Create an OCR input object to hold the image(s) for processing
using var input = new OcrInput();

// Load the image from a specified file path
// Note: Ensure that the file path is correct and accessible
input.AddImage("/path/file.png");

// Enhance the OCR process by adjusting the image properties
// Scale: Magnifies the image to better detect characters
// DilationAmount: Increases the thickness of characters to improve recognition
// Binarize: Converts the image to black and white, enhancing contrast
// Invert: Reverses the colors, sometimes necessary for certain images
// Note: These enhancements can significantly affect the OCR results
input.AddAdvancedImagePreprocessing(filter => {
    filter.Invert();      // Reverse the colors
    filter.Binarize();    // Enhance contrast by converting to black and white
    filter.Dilate(20);    // Increase thickness of characters for better recognition
    filter.Resize(Scale: 2.0); // Magnify image to better detect characters
});

// Perform OCR on the input and obtain the result
OcrResult result = ocr.Read(input);

// Extract the recognized text from the OCR result
// Note: 'result.Text' contains the text extracted from the image, if any.
string resultText = result.Text;
Imports IronOcr



' Initialize the IronTesseract OCR engine

Private ocr = New IronTesseract()



' Create an OCR input object to hold the image(s) for processing

Private input = New OcrInput()



' Load the image from a specified file path

' Note: Ensure that the file path is correct and accessible

input.AddImage("/path/file.png")



' Enhance the OCR process by adjusting the image properties

' Scale: Magnifies the image to better detect characters

' DilationAmount: Increases the thickness of characters to improve recognition

' Binarize: Converts the image to black and white, enhancing contrast

' Invert: Reverses the colors, sometimes necessary for certain images

' Note: These enhancements can significantly affect the OCR results

input.AddAdvancedImagePreprocessing(Sub(filter)

	filter.Invert()

	filter.Binarize()

	filter.Dilate(20)

	filter.Resize(Scale:= 2.0)

End Sub)



' Perform OCR on the input and obtain the result

Dim result As OcrResult = ocr.Read(input)



' Extract the recognized text from the OCR result

' Note: 'result.Text' contains the text extracted from the image, if any.

Dim resultText As String = result.Text
$vbLabelText   $csharpLabel

In this example, I will use the following image for a method I am writing which needs to crop to areas containing text but input images may vary in text location. In this case, I can use FindTextRegion to narrow down the scan to an area that Computer Vision has detected text. This is an example image:

Image with Text
:path=/static-assets/ocr/content-code-examples/how-to/computer-vision-findtextregion-3.cs
using IronOcr;
using IronSoftware.Drawing; // Import IronSoftware.Drawing for handling drawing related functionalities
using System;
using System.Linq; // Import LINQ for handling collections

// Create an instance of IronTesseract, which is responsible for OCR operations
var ocr = new IronTesseract();

// Use an 'using' statement to automatically dispose of resources when done
using var input = new OcrInput();

// Load the image file to perform OCR
input.AddImage("wh-words-sign.jpg");

// Find the text region using computer vision techniques
// This step is crucial for narrowing down the area of interest in the image
var pages = ocr.Read(input); // Perform OCR to get the pages first
var textCropArea = pages.Pages.First().FindTextRegion();

// Debugging: Stamp the identified text region onto the image with a red rectangle
// and save it as a new file. This helps visually verify the identified text area.
input.DrawRectangle(textCropArea, System.Drawing.Color.Red); // Correct method to draw the rectangle in IronSoftware
input.SaveAs("image_text_area.png", AnyBitmap.ImageFormat.Png); // Save the drawn image

// Once the text region looks good from the generated image, perform OCR on the specific region
var ocrResult = ocr.Read("wh-words-sign.jpg", textCropArea);

// Output the recognized text to the console
Console.WriteLine(ocrResult.Text);
Imports IronOcr

Imports IronSoftware.Drawing ' Import IronSoftware.Drawing for handling drawing related functionalities

Imports System

Imports System.Linq ' Import LINQ for handling collections



' Create an instance of IronTesseract, which is responsible for OCR operations

Private ocr = New IronTesseract()



' Use an 'using' statement to automatically dispose of resources when done

Private input = New OcrInput()



' Load the image file to perform OCR

input.AddImage("wh-words-sign.jpg")



' Find the text region using computer vision techniques

' This step is crucial for narrowing down the area of interest in the image

Dim pages = ocr.Read(input) ' Perform OCR to get the pages first

Dim textCropArea = pages.Pages.First().FindTextRegion()



' Debugging: Stamp the identified text region onto the image with a red rectangle

' and save it as a new file. This helps visually verify the identified text area.

input.DrawRectangle(textCropArea, System.Drawing.Color.Red) ' Correct method to draw the rectangle in IronSoftware

input.SaveAs("image_text_area.png", AnyBitmap.ImageFormat.Png) ' Save the drawn image



' Once the text region looks good from the generated image, perform OCR on the specific region

Dim ocrResult = ocr.Read("wh-words-sign.jpg", textCropArea)



' Output the recognized text to the console

Console.WriteLine(ocrResult.Text)
$vbLabelText   $csharpLabel

Now this code has two outputs, the first is a .png file saved by StampCropRectangleAndSaveAs which is used for debugging. We can see where IronCV (Computer Vision) thought the text was:

Image with Text Area Highlighted

Looks pretty good. Now the second output is the Text itself which is:

IRONSOFTWARE

50,000+

Developers in our active community

10,777,061 19,313
NuGet downloads Support tickets resolved
50%+ 80%+
Engineering Team growth Support Team growth
$25,000+

Raised with #TEAMSEAS to clean our beaches & waterways

FindMultipleTextRegions

Usage of FindMultipleTextRegions takes all pages of an OcrInput object and uses computer vision to detect areas which contain text elements and divide the input into separate images based on text regions:

:path=/static-assets/ocr/content-code-examples/how-to/computer-vision-findmultipletextregions-1.cs
using IronOcr;

// Create a new instance of the OCR engine
var ocr = new IronTesseract();

// Initialize a new OCR input
using var input = new OcrInput();

// Load an image file into the OCR input
// Make sure the file path is correct and the file exists
input.AddImage("/path/file.png");

// Perform OCR on the loaded image and get the result
OcrResult result = ocr.Read(input);

// Extract the recognized text from the OCR result
string resultText = result.Text;

// The `resultText` variable now contains all text extracted from the image
// You can easily use this result in further processing or display it
Imports IronOcr



' Create a new instance of the OCR engine

Private ocr = New IronTesseract()



' Initialize a new OCR input

Private input = New OcrInput()



' Load an image file into the OCR input

' Make sure the file path is correct and the file exists

input.AddImage("/path/file.png")



' Perform OCR on the loaded image and get the result

Dim result As OcrResult = ocr.Read(input)



' Extract the recognized text from the OCR result

Dim resultText As String = result.Text



' The `resultText` variable now contains all text extracted from the image

' You can easily use this result in further processing or display it
$vbLabelText   $csharpLabel

Can optionally be called with custom parameters:

:path=/static-assets/ocr/content-code-examples/how-to/computer-vision-findmultipletextregions-2.cs
using IronOcr;

// Create an instance of IronTesseract, which is the main class for performing OCR.
var ocr = new IronTesseract();

// Use a 'using' statement to ensure the OcrInput resources are disposed of after use.
using var input = new OcrInput();

// Load an image from the specified path into the OCR input object.
input.AddImage("/path/file.png"); // Use AddImage according to IronOcr's latest convention.

/*
 * FindMultipleTextRegions method attempts to enhance OCR by attempting
 * to find and resolve multiple regions of text within an image.
 * Parameters:
 * scale - (double) Adjusts the scale of the image to detect smaller text. Default is 1.0.
 * dilationAmount - (int) Specifies the extent of dilation for creating a mask. 
 *                  Negative values might be invalid, so a non-negative value (e.g., 1) is often used.
 * binarize - (bool) If true, the image will be converted to two colors: black and white.
 * invert - (bool) If true, inverts the colors of the image.
 */
try
{
    input.FindMultipleTextRegions(scale: 2.0, dilationAmount: 1, binarize: true, invert: false);

    // Use the Read method to perform OCR on the specified input, retrieving the result object.
    OcrResult result = ocr.Read(input);

    // Get the recognized text from the OCR result object.
    string resultText = result.Text;

    // Output the recognized text to the console.
    Console.WriteLine(resultText);
}
catch (Exception ex)
{
    // Catch potential exceptions and output the message to the console.
    Console.WriteLine("An error occurred during OCR processing: " + ex.Message);
}
Imports IronOcr



' Create an instance of IronTesseract, which is the main class for performing OCR.

Private ocr = New IronTesseract()



' Use a 'using' statement to ensure the OcrInput resources are disposed of after use.

Private input = New OcrInput()



' Load an image from the specified path into the OCR input object.

input.AddImage("/path/file.png") ' Use AddImage according to IronOcr's latest convention.



'

' * FindMultipleTextRegions method attempts to enhance OCR by attempting

' * to find and resolve multiple regions of text within an image.

' * Parameters:

' * scale - (double) Adjusts the scale of the image to detect smaller text. Default is 1.0.

' * dilationAmount - (int) Specifies the extent of dilation for creating a mask. 

' *                  Negative values might be invalid, so a non-negative value (e.g., 1) is often used.

' * binarize - (bool) If true, the image will be converted to two colors: black and white.

' * invert - (bool) If true, inverts the colors of the image.

' 

Try

	input.FindMultipleTextRegions(scale:= 2.0, dilationAmount:= 1, binarize:= True, invert:= False)



	' Use the Read method to perform OCR on the specified input, retrieving the result object.

	Dim result As OcrResult = ocr.Read(input)



	' Get the recognized text from the OCR result object.

	Dim resultText As String = result.Text



	' Output the recognized text to the console.

	Console.WriteLine(resultText)

Catch ex As Exception

	' Catch potential exceptions and output the message to the console.

	Console.WriteLine("An error occurred during OCR processing: " & ex.Message)

End Try
$vbLabelText   $csharpLabel

Another overload method of FindMultipleTextRegions takes an OCR Page and returns a list of OCR Pages, one for each Text region on it:

:path=/static-assets/ocr/content-code-examples/how-to/computer-vision-findmultipletextregions-3.cs
using IronOcr;
using System.Collections.Generic;
using System.Linq;

// Initialize the page index to select a specific page
int pageIndex = 0;

// Create a new instance of OcrInput to handle OCR operations
using var input = new OcrInput();

// Load an image from the given path into the OCR input for processing
input.AddImage("/path/file.png");

// Retrieve the selected page based on the pageIndex
var selectedPage = input.Pages[pageIndex];

// Find and list multiple text regions on the selected page
List<OcrResult.TextRegion> textRegionsOnPage = selectedPage.FindTextRegions().ToList(); 

// Note: The type OcrInput.OcrInputPage does not appear to be valid, replaced with an inferred correct type OcrResult.TextRegion.
// The original GetPages() method assumption was addressed correctly by utilizing the Pages collection property.
Imports IronOcr

Imports System.Collections.Generic

Imports System.Linq



' Initialize the page index to select a specific page

Private pageIndex As Integer = 0



' Create a new instance of OcrInput to handle OCR operations

Private input = New OcrInput()



' Load an image from the given path into the OCR input for processing

input.AddImage("/path/file.png")



' Retrieve the selected page based on the pageIndex

Dim selectedPage = input.Pages(pageIndex)



' Find and list multiple text regions on the selected page

Dim textRegionsOnPage As List(Of OcrResult.TextRegion) = selectedPage.FindTextRegions().ToList()



' Note: The type OcrInput.OcrInputPage does not appear to be valid, replaced with an inferred correct type OcrResult.TextRegion.

' The original GetPages() method assumption was addressed correctly by utilizing the Pages collection property.
$vbLabelText   $csharpLabel

GetTextRegions

Usage of GetTextRegions returns a list of crop areas where text was detected in a page:

:path=/static-assets/ocr/content-code-examples/how-to/computer-vision-gettextregions.cs
using IronOcr;
using IronSoftware.Drawing;
using System.Linq;

// Example code to demonstrate how to load an image and work with OCR (Optical Character Recognition) for a specific page.

int pageIndex = 0;

// Create an instance of OcrInput to load image files for OCR processing.
using var input = new OcrInput();

// Load an image from the specified file path into the OCR input.
// Make sure that the file path is correct and that the file exists.
input.AddImage("/path/file.png");

// Retrieve a specific page from the loaded OCR input.
// Ensure that the pageIndex is within the range of available pages.
var selectedPage = input.ToList()[pageIndex];

// The following line is commented out, but it can be used to get text regions from the selected page.
// Uncomment and use if needed to get regions of recognized text from the page.
// List<Rectangle> regions = selectedPage.GetTextRegions();
Imports IronOcr

Imports IronSoftware.Drawing

Imports System.Linq



' Example code to demonstrate how to load an image and work with OCR (Optical Character Recognition) for a specific page.



Private pageIndex As Integer = 0



' Create an instance of OcrInput to load image files for OCR processing.

Private input = New OcrInput()



' Load an image from the specified file path into the OCR input.

' Make sure that the file path is correct and that the file exists.

input.AddImage("/path/file.png")



' Retrieve a specific page from the loaded OCR input.

' Ensure that the pageIndex is within the range of available pages.

Dim selectedPage = input.ToList()(pageIndex)



' The following line is commented out, but it can be used to get text regions from the selected page.

' Uncomment and use if needed to get regions of recognized text from the page.

' List<Rectangle> regions = selectedPage.GetTextRegions();
$vbLabelText   $csharpLabel

Specific Use Case Guides

With the right settings and input files, OCR can be a very powerful tool. It can almost perfectly imitate the reading capability of a human.

Frequently Asked Questions

What is the purpose of using Computer Vision in IronOCR?

IronOCR uses Computer Vision to detect areas where text exists in an image, which helps in processing images with noise, text in different places, or warped text.

How can I install IronOCR.ComputerVision via NuGet?

You can install IronOCR.ComputerVision via NuGet by using the Package Manager Console with the command: Install-Package IronOcr.ComputerVision.Windows. There are also packages available for Linux and macOS.

What does the FindTextRegion method do?

The FindTextRegion method detects regions in an image that contain text elements and instructs Tesseract to only search for text within those areas.

Can I use custom parameters with the FindTextRegion method?

Yes, you can provide custom parameters to the FindTextRegion method to refine detection, such as setting the minimum text height and allowing subregions.

What is the FindMultipleTextRegions method used for?

The FindMultipleTextRegions method is used to detect areas with text and divide the input into separate images based on these text regions.

How can I retrieve areas where text was detected using GetTextRegions?

You can use the GetTextRegions method on an OcrInput object to get a list of CropRectangle areas where text was detected.

What are the installation packages available for IronOCR.ComputerVision?

Installation packages for IronOCR.ComputerVision are available for Windows, Linux, macOS, and macOS ARM.

How does IronOCR handle images with multiple text regions?

IronOCR can handle images with multiple text regions using the FindMultipleTextRegions method, which divides the input into separate images based on detected text regions.

Are there code examples available for using IronOCR with Computer Vision?

Yes, the tutorial provides code examples demonstrating how to use methods like FindTextRegion and FindMultipleTextRegions with IronOCR.

What is required to use IronOCR's Computer Vision capabilities?

To use IronOCR's Computer Vision capabilities, you need to install the IronOcr.ComputerVision package through NuGet and reference the necessary namespaces in your C# project.

Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.