Preprocess an Image for OCR - C# .NET 6

This tutorial shows how to preprocess an image for OCR in a C# .NET 6 application using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how to preprocess an image for OCR using the AutoPreprocess method in a C# .NET 6 Console application.
Completion Time 10 minutes
Visual Studio Project Download tutorial project (1 KB)
Platform C# .NET 6 Console Application
IDE Visual Studio 2022
Runtime Target .NET 6 or Higher
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on the Preprocess an Image for OCR - C# .NET 6 tutorial.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in the Add References and Set a License tutorial. If you do not have that project, follow the steps in that tutorial to create it.

The references needed depend upon the purpose of the project. References can be added via NuGet packages.

This tutorial requires the following NuGet package:

For a complete list of which DLL files are required for your application, refer to Files to be Included With Your Application.

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:

Add the OCR Image Preprocessing Code

With the project created, the references added, and the license set, coding can begin.

In the Solution Explorer, open Program.cs. Add the following statements to the using block at the top of Program.cs.

C#
using System; 
using Leadtools; 
using Leadtools.Document.Writer; 
using Leadtools.Ocr; 

Add a new method to the Program class named OCRPreProcessing(). Call the OCRPreProcessing() method inside the Main() method below the set license code, as shown below.

C#
static void Main(string[] args) 
{ 
    InitLEAD(); 
 
    OCRPreProcessing(); 
} 

Add the code below to the OCRPreProcessing() method to initialize the IOcrEngine, preprocess the loaded image, and run OCR exporting to a searchable PDF.

C#
static void OCRPreProcessing() 
{ 
   string tifFileName = @"C:\LEADTOOLS22\Resources\Images\Clean.tif"; 
   string pdfFileName = @"C:\LEADTOOLS22\Resources\Images\Clean.pdf"; 
 
   using (IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD)) 
   { 
      // Start the engine using default parameters 
      ocrEngine.Startup(null, null, null, @"C:\LEADTOOLS22\Bin\Common\OcrLEADRuntime"); 
 
      // Create an OCR document 
      using (IOcrDocument ocrDocument = ocrEngine.DocumentManager.CreateDocument()) 
      { 
         // Add this image to the document 
         IOcrPage ocrPage = ocrDocument.Pages.AddPage(tifFileName, null); 
 
         // Auto-preprocess it 
         ocrPage.AutoPreprocess(OcrAutoPreprocessPageCommand.Deskew, null); 
         ocrPage.AutoPreprocess(OcrAutoPreprocessPageCommand.Invert, null); 
         ocrPage.AutoPreprocess(OcrAutoPreprocessPageCommand.Rotate, null); 
 
         // Recognize it and save it as PDF 
         ocrPage.Recognize(null); 
         ocrDocument.Save(pdfFileName, DocumentFormat.Pdf, null); 
      } 
   } 
} 

Run the Project

Run the project by pressing F5, or by selecting Debug -> Start Debugging.

If the steps were followed correctly, the console appears and the application will "clean-up" the loaded image and export it as a searchable PDF.

Wrap-up

This tutorial showed how to preprocess an image for OCR using the AutoPreprocess method inside the IOcrPage interface, and save it as a searchable PDF. Also, we covered how to use the IOcrDocument and IOcrEngine interfaces.

See Also

Help Version 22.0.2024.3.20
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.