USING IRONOCR

How to create Character Recognition in C#

Published April 29, 2024
Share:

Introduction

The technology of Optical Character Recognition (OCR) allows for the conversion of printed or handwritten text into digital formats readable by machine. When a document is scanned (such as an invoice or receipt), it is saved by your computer as an image file. However, the text within the scanned image cannot be edited, searched, or counted using a regular text editor.

However, OCR can process the image, extract text, and transform it into a text format that can be read by computers. This enables the extraction of text from various sources, including PDF files and other scanned images. Furthermore, OCR capabilities extend beyond simple text extraction to include major image formats and PDF documents, converting them into searchable OCR data.

In C#, developers can leverage the power of OCR through various libraries, and one of which is the powerful library IronOCR from Iron Software. In this tutorial, we'll explore the basics of OCR and demonstrate how to use IronOCR to perform Character Recognition efficiently in C#.

How to create Character Recognition in C#

  1. Create a brand new C# project and name the project in Visual Studio.
  2. Install the IronOCR .NET library and include it in the project folder.
  3. Utilize the IronOCR Tesseract to read text from images.
  4. Utilize the IronOCR Advance features to read the text in images
  5. Performance Tuning of IronOCR Read Operation.

Getting Started with IronOCR

IronOCR a C# library developed by Iron Software is that provides advanced OCR capabilities. It offers accurate text extraction from images, PDFs, and scanned documents. Before we dive into the code, make sure you have IronOCR installed in your project.

Key features of IronOCR from Iron Software

Improved Tesseract OCR Engine

IronOCR elevates the capabilities of the widely used Tesseract OCR engine by enhancing both accuracy and speed. It serves as a robust solution for extracting text from various sources, including images, PDFs, and diverse document formats.

Wide Language Coverage

With support for over 127 languages, IronOCR is adept at handling multilingual requirements, making it an ideal choice for applications demanding linguistic versatility.

Versatile Output Choices

Extracted text can be conveniently outputted as plain text or structured data for seamless integration into further processing pipelines. Additionally, IronOCR facilitates the creation of searchable PDFs directly from image inputs.

Cross-Platform Adaptability

Engineered for compatibility with C#, F#, and VB.NET, IronOCR seamlessly operates across various.NET environments including versions 8, 7, 6, Core, Standard, and Framework.

Leveraging Tesseract 5

IronOCR harnesses the power of Tesseract 5, finely tailored for optimal performance within the.NET ecosystem.

Zone-Based OCR Capability

With IronOCR, users can precisely define specific zones within documents, enabling targeted OCR processing. This feature enhances accuracy and efficiency by focusing processing power where it's needed most.

Image Preprocessing Tools

The library offers a suite of image preprocessing functionalities such as de-skewing and noise reduction. These tools ensure superior results even when dealing with imperfect source images, ultimately enhancing the overall OCR experience.

Now, we will develop a demo application that utilizes IronOCR to read Text from images.

Prerequisites

  1. Visual Studio: Ensure you have installed Visual Studio or any other C# development environment.
  2. NuGet Package Manager: Ensure NuGet is present in order to manage packages in your project.

Step 1: Create a New C# Project in Visual Studio

To start with, let us create a new console application using Visual Studio as shown below.

How to create Character Recognition in C#: Figure 1 - Creating a brand new C# project in Visual Studio

Provide a project name and location below.

How to create Character Recognition in C#: Figure 2 - Provide a project name and the location you wish to save at

Select the required .NET Version for the project.

How to create Character Recognition in C#: Figure 3 - Select the appropriate .NET Version for the project

Click the Create button to create the new project.

Step 2: Install the IronOCR library and integrate it to your project.

IronOCR can be found in the NuGet package manager console as shown below. Use the command provided to install the package.

How to create Character Recognition in C#: Figure 4 - IronOCR NuGet Package Manager download page

Using the Visual Studio NuGet Package Manager, search for IronOCR and install to your project folder.

How to create Character Recognition in C#: Figure 5 - Searching for IronOCR through the browse tab using NuGet Package Manager

Once installed, the application is ready to make use of IronOCR to read text from images.

Step 3: Utilize the IronOCR Tesseract to read text from images.

IronOCR stands out as the exclusive .NET library offering Tesseract 5 OCR capabilities. At present, it holds the distinction of being the most sophisticated Tesseract 5 library across all programming languages. IronOCR seamlessly integrates Tesseract 5 into various .NET environments, including Framework, Standard, Core, Xamarin, and Mono, ensuring comprehensive support across the ecosystem.

Consider the below image file as input. Now, let's see how to read the text in this image file

How to create Character Recognition in C#: Figure 6 - Example input

using IronOcr;
public class Program
{
    public static void Main(String [] args)
    {
        var ocrTesseract = new IronTesseract(); 
    using var ocrInput = new OcrInput();
    ocrInput.LoadImage(@"sample1.png");
    var ocrResult = ocrTesseract.Read(ocrInput);
    Console.WriteLine(ocrResult.Text);
    }
}
using IronOcr;
public class Program
{
    public static void Main(String [] args)
    {
        var ocrTesseract = new IronTesseract(); 
    using var ocrInput = new OcrInput();
    ocrInput.LoadImage(@"sample1.png");
    var ocrResult = ocrTesseract.Read(ocrInput);
    Console.WriteLine(ocrResult.Text);
    }
}
Imports IronOcr
Public Class Program
	Public Shared Sub Main(ByVal args() As String)
		Dim ocrTesseract = New IronTesseract()
	Dim ocrInput As New OcrInput()
	ocrInput.LoadImage("sample1.png")
	Dim ocrResult = ocrTesseract.Read(ocrInput)
	Console.WriteLine(ocrResult.Text)
	End Sub
End Class
VB   C#

Code Explanation

  1. We start with creating IronTesseract with the required configuration
  2. Then we load the sample image shown to the OcrInput object
  3. Finally, we read the text in the image and output it on the console

Output

How to create Character Recognition in C#: Figure 7 - Extracted text using IronOCR

Step 4: Utilize the IronOCR Advance features to read the text in images

The IronTesseract.Configuration object grants advanced users access to the underlying Tesseract API within C#/.NET, enabling detailed setup configuration for fine-tuning and optimization. Below are some of the advanced configurations possible

Language Selection

You can specify the language for OCR using the Language property. For instance, to set the language to English, use:

IronTesseract ocr = new IronTesseract();
ocr.Language = OcrLanguage.English;
IronTesseract ocr = new IronTesseract();
ocr.Language = OcrLanguage.English;
Dim ocr As New IronTesseract()
ocr.Language = OcrLanguage.English
VB   C#

Page Segmentation Mode

The PageSegmentationMode determines how Tesseract segments the input image. Options include AutoOsd, SingleBlock, SingleLine, and more. For example:

ocr.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.AutoOsd;
ocr.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.AutoOsd;
ocr.Configuration.PageSegmentationMode = TesseractPageSegmentationMode.AutoOsd
VB   C#

Custom Tesseract Variables

You can fine-tune Tesseract by setting specific variables. For instance, to disable parallelization:

ocr.Configuration.TesseractVariables ["tessedit_parallelize"] = false;
ocr.Configuration.TesseractVariables ["tessedit_parallelize"] = false;
ocr.Configuration.TesseractVariables ("tessedit_parallelize") = False
VB   C#

Whitelisting and Blacklisting Characters

Use WhiteListCharacters and BlackListCharacters to control which characters Tesseract recognizes. For example:

ocr.Configuration.WhiteListCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
ocr.Configuration.BlackListCharacters = "`ë|^";
ocr.Configuration.WhiteListCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
ocr.Configuration.BlackListCharacters = "`ë|^";
ocr.Configuration.WhiteListCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
ocr.Configuration.BlackListCharacters = "`ë|^"
VB   C#

Additional Configuration Variables

Explore other Tesseract configuration variables to customize behavior according to your needs. For instance:

ocr.Configuration.TesseractVariables ["classify_num_cp_levels"] = 3;
ocr.Configuration.TesseractVariables ["textord_debug_tabfind"] = 0;
// ... (more variables)
ocr.Configuration.TesseractVariables ["classify_num_cp_levels"] = 3;
ocr.Configuration.TesseractVariables ["textord_debug_tabfind"] = 0;
// ... (more variables)
ocr.Configuration.TesseractVariables ("classify_num_cp_levels") = 3
ocr.Configuration.TesseractVariables ("textord_debug_tabfind") = 0
' ... (more variables)
VB   C#

Now let us try to decode the same image using advanced settings

using IronOcr;
public class Program
{
    public static void Main()
    {
        Console.WriteLine("Decoding using advanced features");
        var ocrTesseract = new IronTesseract() // create instance
        {
            Language = OcrLanguage.EnglishBest, // configure best english language
            Configuration = new TesseractConfiguration()
            {
                ReadBarCodes = false, // read bar codes false
                BlackListCharacters = "`ë|^", // black listed characters
                WhiteListCharacters = null, // no white list, allow all
                PageSegmentationMode = TesseractPageSegmentationMode.AutoOsd,
                TesseractVariables = null, // no custom variable used
            },
            MultiThreaded = false,
        };
        using var ocrInput = new OcrInput(); // create a disposible ocr input object
        ocrInput.AddImage(@"sample1.png"); // load the sample image 
        var ocrResult = ocrTesseract.Read(ocrInput); // read the text from the image
        Console.WriteLine(ocrResult.Text);// output the image
    }
}
using IronOcr;
public class Program
{
    public static void Main()
    {
        Console.WriteLine("Decoding using advanced features");
        var ocrTesseract = new IronTesseract() // create instance
        {
            Language = OcrLanguage.EnglishBest, // configure best english language
            Configuration = new TesseractConfiguration()
            {
                ReadBarCodes = false, // read bar codes false
                BlackListCharacters = "`ë|^", // black listed characters
                WhiteListCharacters = null, // no white list, allow all
                PageSegmentationMode = TesseractPageSegmentationMode.AutoOsd,
                TesseractVariables = null, // no custom variable used
            },
            MultiThreaded = false,
        };
        using var ocrInput = new OcrInput(); // create a disposible ocr input object
        ocrInput.AddImage(@"sample1.png"); // load the sample image 
        var ocrResult = ocrTesseract.Read(ocrInput); // read the text from the image
        Console.WriteLine(ocrResult.Text);// output the image
    }
}
Imports IronOcr
Public Class Program
	Public Shared Sub Main()
		Console.WriteLine("Decoding using advanced features")
		Dim ocrTesseract = New IronTesseract() With {
			.Language = OcrLanguage.EnglishBest,
			.Configuration = New TesseractConfiguration() With {
				.ReadBarCodes = False,
				.BlackListCharacters = "`ë|^",
				.WhiteListCharacters = Nothing,
				.PageSegmentationMode = TesseractPageSegmentationMode.AutoOsd,
				.TesseractVariables = Nothing
			},
			.MultiThreaded = False
		}
		Dim ocrInput As New OcrInput() ' create a disposible ocr input object
		ocrInput.AddImage("sample1.png") ' load the sample image
		Dim ocrResult = ocrTesseract.Read(ocrInput) ' read the text from the image
		Console.WriteLine(ocrResult.Text) ' output the image
	End Sub
End Class
VB   C#

Code Explanation

  1. IronOCR Configuration: An instance of IronTesseract (the main IronOCR class) is created and assigned to the variable ocrTesseract.

    Configuration settings are applied to ocrTesseract:

    1. Language: Specifies the language for OCR (in this case, English).
    2. Configuration: A TesseractConfiguration object that allows further customization:
    3. ReadBarCodes: Disables reading barcodes.
    4. BlackListCharacters: Specifies characters to blacklist (characters not to recognize).
    5. WhiteListCharacters: No whitelist specified, allowing all characters.
    6. PageSegmentationMode: Sets the page segmentation mode to “AutoOsd.”
    7. TesseractVariables: No custom variables were used.
      1. MultiThreaded: Disables multithreading.
      2. OCR Input and Image Loading: A using block creates a disposable ocrInput object of type OcrInput. The image file “sample1.png” is added to ocrInput.
      3. Text Extraction: The Read method is called on ocrTesseract, passing in ocrInput.
      4. The result is stored in the ocrResult variable.
      5. Output: The extracted text is printed to the console using Console.WriteLine(ocrResult.Text).

Output

How to create Character Recognition in C#: Figure 8 - Extracted text using IronOCR

Step 5: Performance Tuning of IronOCR Read Operation.

When working with IronOCR, you have access to various image filters that can help preprocess images before performing OCR. These filters optimize the image quality, enhance visibility, and reduce noise or artifacts. They help to improve the performance of the OCR operation.

  1. Rotate:

    The Rotate filter allows you to rotate images by a specified number of degrees clockwise. For anti-clockwise rotation, use negative numbers.

  2. Deskew:

    The Deskew filter corrects image skew, ensuring that the text is upright and orthogonal. This is particularly useful for OCR because Tesseract performs best with properly oriented scans.

  3. Scale:

    The Scale filter proportionally scales OCR input pages.

  4. Binarize:

    The Binarize filter converts every pixel to either black or white, with no middle ground. It can improve OCR performance in cases of very low contrast between text and background.

  5. ToGrayScale:

    The ToGrayScale filter converts every pixel to a shade of grayscale. While unlikely to significantly improve OCR accuracy, it may enhance speed.

  6. Invert:

    The Invert filter reverses colors—white becomes black, and black becomes white.

  7. ReplaceColor:

    The ReplaceColor filter replaces a specific color within an image with another color, considering a certain threshold.

  8. Contrast:

    The Contrast filter automatically increases contrast. It often improves OCR speed and accuracy in low-contrast scans.

  9. Dilate and Erode:

    These advanced morphology filters manipulate object boundaries in an image.

    1. Dilate adds pixels to object boundaries.
      1. Erode removes pixels from object boundaries.
      2. Sharpen:

    The Sharpen filter sharpens blurred OCR documents and flattens alpha channels to white.

    1. DeNoise:

    The DeNoise filter removes digital noise. Use it where noise is expected.

    1. DeepCleanBackgroundNoise:

    This heavy background noise removal filter should be used only when extreme document background noise is known. It may reduce OCR accuracy for clean documents and is CPU-intensive.

    1. EnhanceResolution:

    The EnhanceResolution filter enhances the resolution of low-quality images. It’s not often needed due to automatic resolution handling.

Here’s an example of how to apply filters using IronOCR in C#:

var ocr = new IronTesseract();
var input = new OcrInput();
input.LoadImage("sample.png");
input.Deskew();
var result = ocr.Read(input);
Console.WriteLine(result.Text);
var ocr = new IronTesseract();
var input = new OcrInput();
input.LoadImage("sample.png");
input.Deskew();
var result = ocr.Read(input);
Console.WriteLine(result.Text);
Dim ocr = New IronTesseract()
Dim input = New OcrInput()
input.LoadImage("sample.png")
input.Deskew()
Dim result = ocr.Read(input)
Console.WriteLine(result.Text)
VB   C#

Common OCR Applications

  1. Document Digitization: OCR is widely used to convert scanned paper documents, such as invoices, receipts, forms, and contracts, into digital formats. This digitization process streamlines document storage, retrieval, and management, reducing paper clutter and improving efficiency.
  2. Data Extraction: OCR enables the extraction of text and data from scanned documents, images, and PDFs. This extracted data can be used for automated data entry, content analysis, indexing, and integration into databases or business systems.
  3. Text Recognition in Images: OCR technology allows extracting text from printed or handwritten text images for indexing and search purposes. This capability is utilized in various applications, including augmented reality, image-based search engines, and translation services.
  4. Automatic License Plate Recognition (ALPR): ALPR systems utilize OCR to read license plate numbers from images or video streams captured by cameras installed in traffic surveillance, parking management, toll collection, and law enforcement applications.
  5. Accessibility Solutions: OCR plays a crucial role in creating accessible content for individuals with visual impairments. By converting text from images or documents into speech or braille, OCR helps make information accessible to people with disabilities.
  6. Identity Verification: OCR technology is employed in identity verification processes, such as scanning and processing identity documents like passports, driver's licenses, and IDs. It assists in verifying the authenticity of documents and extracting relevant information for identity verification purposes.
  7. Banking and Finance: OCR is used in banking and finance for tasks such as reading checks, processing invoices, converting an existing PDF document, extracting data from financial statements, and automating document-based workflows to enhance accuracy and efficiency in financial operations.
  8. Medical Records Management: In the healthcare sector, OCR facilitates the conversion of handwritten or printed medical records into electronic formats, aiding in electronic health record (EHR) management, data analysis, and decision-making processes.
  9. Automated Translation: OCR technology is integrated into translation tools and language learning apps to convert printed text from one language to another. Users can capture text with their devices, and OCR assists in translating it into the desired language in real time.
    1. Archival and Historical Document Preservation: OCR is utilized in digitizing archival materials and historical documents, preserving them in digital formats for future access, research, and analysis while ensuring the preservation of valuable cultural heritage.

License Requirements

IronOCR. Provide the below details to get the key delivered to your email ID

How to create Character Recognition in C#: Figure 9 - IronPDF trial license page

Once the key is obtained either by purchase or free trial, follow the below steps to use the key

Setting Your License Key: Set your IronOCR license key using the code. Add the following line to your application startup (before using IronOCR):

IronOcr.License.LicenseKey = "IRONOCR-MYLICENSE-KEY-1EF01";
IronOcr.License.LicenseKey = "IRONOCR-MYLICENSE-KEY-1EF01";
IronOcr.License.LicenseKey = "IRONOCR-MYLICENSE-KEY-1EF01"
VB   C#

Global Application Key (Web.Config or App.Config): To apply a key globally across your application, use the configuration file (Web.Config or App.Config). Add the following key to your appSettings:

<configuration>
    <!-- Other settings -->
    <appSettings>
        <add key="IronOcr.LicenseKey" value="IRONOCR-MYLICENSE-KEY-1EF01"/>
    </appSettings>
</configuration>
<configuration>
    <!-- Other settings -->
    <appSettings>
        <add key="IronOcr.LicenseKey" value="IRONOCR-MYLICENSE-KEY-1EF01"/>
    </appSettings>
</configuration>
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'<configuration> <!-- Other settings -- > <appSettings> <add key="IronOcr.LicenseKey" value="IRONOCR-MYLICENSE-KEY-1EF01"/> </appSettings> </configuration>
VB   C#

Using .NET Core appsettings.json: For .NET Core applications, create an appsettings.json file in your project’s root directory. Replace the "IronOcr.LicenseKey" key with your license value:

{
    "IronOcr.LicenseKey": "IRONOCR-MYLICENSE-KEY-1EF01"
}
{
    "IronOcr.LicenseKey": "IRONOCR-MYLICENSE-KEY-1EF01"
}
If True Then
	"IronOcr.LicenseKey": "IRONOCR-MYLICENSE-KEY-1EF01"
End If
VB   C#

Testing Your License Key: Verify that your key has been installed correctly by testing it:

bool result = IronOcr.License.IsValidLicense("IRONOCR-MYLICENSE-KEY-1EF01");
bool result = IronOcr.License.IsValidLicense("IRONOCR-MYLICENSE-KEY-1EF01");
Dim result As Boolean = IronOcr.License.IsValidLicense("IRONOCR-MYLICENSE-KEY-1EF01")
VB   C#

Conclusion

In conclusion, IronOCR, which starts at $749. Embrace the power of OCR with IronOCR and unlock a world of possibilities in your C# projects.

< PREVIOUS
How to Perform Vehicle Registration OCR in C#
NEXT >
How to Read Identity Documents Using OCR in C#

Ready to get started? Version: 2024.10 just released

Free NuGet Download Total downloads: 2,561,036 View Licenses >