←Select platform

AutoZone Method

Summary

Performs auto decomposition of the page to find the text and graphics zones using predefined parameters

Syntax
C#
VB
Objective-C
C++
Java
public void AutoZone( 
   OcrProgressCallback callback 
) 
Sub AutoZone( _ 
   ByVal callback As OcrProgressCallback _ 
)  
- (BOOL)autoZone:(nullable LTOcrProgressHandler)progressHandlererror:(NSError **)error 
public void autoZone(OcrProgressListener callback) 
void AutoZone(  
   OcrProgressCallback^ callback 
)  

Parameters

callback
Optional callback to show operation progress.

Remarks

You can use the OcrProgressCallback to show the operation progress or to abort it. For more information and an example, refer to OcrProgressCallback.

The zones found by this method are added to the Zones collection of this page. Any previously added zones will be removed from Zones first.

This method finds the zone coordinates (OcrZone.Bounds which will always have units of pixels and type (OcrZone.ZoneType). The type of the zone determines its role in the page layout and can be classified into three different groups:

  1. The text zone types: OcrZoneType.Text. This type means that the zone contains textual information without a table type structure inside (it is flowed text).
  2. The OcrZoneType.Table type. A zone having this type means that the zone is detected as containing a table, i.e. with columns, with or without a grid. Such zones will be handled differently from those of flowed text type zones.
  3. The OcrZoneType.Graphic type means a zone contains graphics, i.e. this zone will not be recognized at all. The only reason to have such a zone is to save or export the image inside the zone area.

Note: If this IOcrPage is an empty page, in other words, when the OCR engine performs automatic page decomposing with the AutoZone method and could not find any zones in it, the Recognize method will fail with an exception. It is recommended you call AutoZone and then check if there is at least one zone found by the engine (using Zones.Count). If the count is zero, do not call Recognize.

If a recognition module is not able to recognize an object (i.e. character, or checkmark etc.), this object will be marked as a rejected one. It will become marked by a rejection symbol during conversion to the final output document. Note that IOcrDocumentManager.RejectionSymbol can be set to specify the rejection symbol used in the final document.

Note on AutoZone/Recognize and the page image: In certain cases, AutoZone and Recognize will perform image processing on the page that might result in the page being manipulated. For example, if you add a zone of type table, the engine might automatically deskew the page if required. This result in the image representation of the image to be different after AutoZone or Recognize is called. If your application has a requirement to view the image of the page, then call GetRasterImage after AutoZone or Recognize to get the latest version of the image representation of the page in case it has changed. The LEADTOOLS Main OCR C# and VB demos do exactly that.

Example

This example will perform auto-zoning on an OCR page then displays information about the zones found.

C#
VB
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Ocr; 
using Leadtools.Forms.Common; 
using Leadtools.Document.Writer; 
using Leadtools.WinForms; 
using Leadtools.Drawing; 
using Leadtools.ImageProcessing; 
using Leadtools.ImageProcessing.Color; 
 
public void OcrAutoZoneExample() 
{ 
   // Create an image with some text in it 
   RasterImage image = new RasterImage(RasterMemoryFlags.Conventional, 320, 200, 24, RasterByteOrder.Bgr, RasterViewPerspective.TopLeft, null, IntPtr.Zero, 0); 
   Rectangle imageRect = new Rectangle(0, 0, image.ImageWidth, image.ImageHeight); 
 
   IntPtr hdc = RasterImagePainter.CreateLeadDC(image); 
   using (Graphics g = Graphics.FromHdc(hdc)) 
   { 
      g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 
      g.FillRectangle(Brushes.White, imageRect); 
 
      using (Font f = new Font("Arial", 20, FontStyle.Regular)) 
         g.DrawString("Normal line", f, Brushes.Black, 0, 0); 
 
      using (Font f = new Font("Courier New", 20, FontStyle.Regular)) 
         g.DrawString("Monospaced line", f, Brushes.Black, 0, 80); 
   } 
 
   RasterImagePainter.DeleteLeadDC(hdc); 
 
   string zonesFileName = Path.Combine(LEAD_VARS.ImagesDir, "MyZones.xml"); 
 
   // Create an instance of the engine 
   using (IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD, false)) 
   { 
      // Start the engine using default parameters 
      ocrEngine.Startup(null, null, null, LEAD_VARS.OcrLEADRuntimeDir); 
 
      // Create an OCR page 
      using (IOcrPage ocrPage = ocrEngine.CreatePage(image, OcrImageSharingMode.AutoDispose)) 
      { 
         // Show the zones, there should be no zones yet 
         ShowZones("Right after the page was created", ocrPage); 
 
         // Perform default AutoZoning on the page 
         ocrPage.AutoZone(null); 
 
         // Show the zones, there should be two zones, one for each line 
         ShowZones("AutoZone with default parameters", ocrPage); 
 
         // Update the first zone manually 
         OcrZone ocrZone = ocrPage.Zones[0]; 
         ocrZone.ZoneType = OcrZoneType.Text; 
         ocrPage.Zones[0] = ocrZone; 
 
         // Show the zones 
         ShowZones("After updating the type of the first zone", ocrPage); 
 
         // Save the zones to a file and then clear them 
         ocrPage.SaveZones(zonesFileName); 
         ocrPage.Zones.Clear(); 
 
         // Show the zones, there should be no zones since we just cleared them 
         ShowZones("After calling save and clear", ocrPage); 
 
         // Re-load the zones 
         ocrPage.LoadZones(zonesFileName); 
         ShowZones("After re-loading the zones", ocrPage); 
      } 
 
      // Shutdown the engine 
      // Note: calling Dispose will also automatically shutdown the engine if it has been started 
      ocrEngine.Shutdown(); 
   } 
} 
 
private void ShowZones(string message, IOcrPage ocrPage) 
{ 
   Console.WriteLine("Zones after {0}:", message); 
   foreach (OcrZone ocrZone in ocrPage.Zones) 
   { 
      int index = ocrPage.Zones.IndexOf(ocrZone); 
      Console.WriteLine("Zone index: {0}", index); 
      Console.WriteLine("  Id                  {0}", ocrZone.Id); 
      Console.WriteLine("  Bounds              {0}", ocrZone.Bounds); 
      Console.WriteLine("  ZoneType            {0}", ocrZone.ZoneType); 
      Console.WriteLine("  CharacterFilters:   {0}", ocrZone.CharacterFilters); 
      Console.WriteLine("----------------------------------"); 
   } 
 
   Console.WriteLine("Hit enter to continue"); 
   //Console.ReadLine(); 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images"; 
   public const string OcrLEADRuntimeDir = @"C:\LEADTOOLS 20\Bin\Common\OcrLEADRuntime"; 
} 
Imports Leadtools 
Imports Leadtools.Codecs 
Imports Leadtools.Ocr 
Imports Leadtools.Forms 
Imports Leadtools.Document.Writer 
Imports Leadtools.WinForms 
Imports Leadtools.Drawing 
Imports Leadtools.ImageProcessing 
Imports Leadtools.ImageProcessing.Color 
 
Public Sub OcrAutoZoneExample() 
   ' Create an image with some text in it 
   Dim image As New RasterImage(RasterMemoryFlags.Conventional, 320, 200, 24, RasterByteOrder.Bgr, RasterViewPerspective.TopLeft, 
      Nothing, IntPtr.Zero, 0) 
   Dim imageRect As New Rectangle(0, 0, image.ImageWidth, image.ImageHeight) 
 
   Dim hdc As IntPtr = RasterImagePainter.CreateLeadDC(image) 
   Using g As Graphics = Graphics.FromHdc(hdc) 
      g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality 
      g.FillRectangle(Brushes.White, imageRect) 
 
      Using f As New Font("Arial", 20, FontStyle.Regular) 
         g.DrawString("Normal line", f, Brushes.Black, 0, 0) 
      End Using 
 
      Using f As New Font("Courier New", 20, FontStyle.Regular) 
         g.DrawString("Monospaced line", f, Brushes.Black, 0, 80) 
      End Using 
   End Using 
 
   RasterImagePainter.DeleteLeadDC(hdc) 
 
   Dim zonesFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "MyZones.xml") 
 
   ' Create an instance of the engine 
   Using ocrEngine As IOcrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD, False) 
      ' Start the engine using default parameters 
      ocrEngine.Startup(Nothing, Nothing, Nothing, LEAD_VARS.OcrLEADRuntimeDir) 
 
      ' Create an OCR page 
      Using ocrPage As IOcrPage = ocrEngine.CreatePage(image, OcrImageSharingMode.AutoDispose) 
         ' Show the zones, there should be no zones yet 
         ShowZones("Right after the page was created", ocrPage) 
 
         ' Perform default AutoZoning on the page 
         ocrPage.AutoZone(Nothing) 
 
         ' Show the zones, there should be two zones, one for each line 
         ShowZones("AutoZone with default parameters", ocrPage) 
 
         ' Update the first zone manually 
         Dim ocrZone As OcrZone = ocrPage.Zones(0) 
         ocrZone.ZoneType = OcrZoneType.Text 
         ocrPage.Zones(0) = ocrZone 
 
         ' Show the zones 
         ShowZones("After updating the type of the first zone", ocrPage) 
 
         ' Save the zones to a file and then clear them 
         ocrPage.SaveZones(zonesFileName) 
         ocrPage.Zones.Clear() 
 
         ' Show the zones, there should be no zones since we just cleared them 
         ShowZones("After calling save and clear", ocrPage) 
 
         ' Re-load the zones 
         ocrPage.LoadZones(zonesFileName) 
         ShowZones("After re-loading the zones", ocrPage) 
      End Using 
 
      ' Shutdown the engine 
      ' Note: calling Dispose will also automatically shutdown the engine if it has been started 
      ocrEngine.Shutdown() 
   End Using 
End Sub 
 
Private Sub ShowZones(message As String, ocrPage As IOcrPage) 
   Console.WriteLine("Zones after {0}:", message) 
   For Each ocrZone As OcrZone In ocrPage.Zones 
      Dim index As Integer = ocrPage.Zones.IndexOf(ocrZone) 
      Console.WriteLine("Zone index: {0}", index) 
      Console.WriteLine("  Id                  {0}", ocrZone.Id) 
      Console.WriteLine("  Bounds              {0}", ocrZone.Bounds) 
      Console.WriteLine("  ZoneType            {0}", ocrZone.ZoneType) 
      Console.WriteLine("  CharacterFilters:   {0}", ocrZone.CharacterFilters) 
      Console.WriteLine("----------------------------------") 
   Next 
 
   Console.WriteLine("Hit enter to continue") 
   Console.ReadLine() 
End Sub 
 
Public NotInheritable Class LEAD_VARS 
   Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images" 
   Public Const OcrLEADRuntimeDir As String = "C:\LEADTOOLS 20\Bin\Common\OcrLEADRuntime" 
End Class 

Requirements

Target Platforms

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

Leadtools.Ocr Assembly