Preprocess an Image for OCR - Console C#

This tutorial shows how to create a C# Windows Console application that uses the LEADTOOLS SDK to preprocess images for OCR Recognition.

Overview  
Summary This tutorial covers how to use LEADTOOLS Image Processing SDK technology in a C# Windows Console application
Completion Time 30 minutes
Visual Studio Project Download tutorial project (3 KB)
Platform C# Windows Console Application
IDE Visual Studio 2017, 2019
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 - Console C# tutorial.

Create the Project and Add the 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 by one or the other of the following two methods (but not both). For this project, the following references are needed:

If using NuGet references, this tutorial requires the following NuGet package:

If local DLL references are used, the following DLLs are needed. The DLLs are located at <INSTALL_DIR>\LEADTOOLS22\Bin\Dotnet4\x64:

For a complete list of which DLL files are required for your application, refer to Files to be Included in 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 Image Preprocessing and OCR code

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

In Solution Explorer, open Program.cs and add the following statements to the using block at the top:

C#
// Using block at the top 
using System; 
using System.IO; 
using Leadtools; 
using Leadtools.Document.Writer; 
using Leadtools.Ocr; 

Add a new method called OCRPreProcessing() and call it inside the Main method after the SetLicense() method call. Add the below code to start up the OCR Engine, load a TIFF image as an IOcrPage, preprocess the TIF image, and then run OCR recognition and export to 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); 
      } 
   } 
} 

Handling Streams

To handle the files using MemoryStream, replace the existing code in the OCRPreProcessing() method with the following:

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  
         byte[] tifData = File.ReadAllBytes(tifFileName); 
         IOcrPage ocrPage; 
         using (MemoryStream tifStream = new MemoryStream(tifData)) 
            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 into stream as PDF  
         ocrPage.Recognize(null); 
         MemoryStream outputStream = new MemoryStream(); 
         ocrDocument.Save(outputStream, 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 application should OCR the TIFF and provide a cleaned up PDF document.

Wrap-up

This tutorial showed how to OCR an image, clean up image, and save to PDF. Also, it covered how to use the IOcrPage, 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.