OCR Tutorial - Working with Recognition Results

Take the following steps to create and run a program that shows how to interactively draw zones on an OCR page and how to recognize these zones. Remember, the purpose of the tutorials is to provide you with a quick and easy way to generate an OCR program.

  1. Start Visual Studio 2005 or 2008.

  2. Choose File->New->Project from the menu.

  3. In the New Project dialog box, choose either "Visual C# Projects" or "VB Projects" in the Projects Type List, and choose "Windows Application" or "Windows Forms Application" depending on your Visual Studio version from the Templates List.

  4. Type the project name as "OcrTutorial2" in the Project Name field, and then choose OK. If desired, type a new location for your project or select a directory using the Browse button, and then choose OK.

  5. In the "Solution Explorer" window, right-click on the "References" folder, and select "Add Reference..." from the context menu. In the "Add Reference" dialog box, select the ".NET" tab and browse to LEADTOOLS For .NET "<LEADTOOLS_INSTALLDIR>\Bin\DotNet4\Win32" folder and select the following DLLs:

    • Leadtools.dll
    • Leadtools.Drawing.dll
    • Leadtools.Codecs.dll
    • Leadtools.Controls.WinForms.dll
    • Leadtools.Forms.dll
    • Leadtools.Forms.DocumentWriters.dll
    • Leadtools.Forms.Ocr.dll
    • Leadtools.Forms.Ocr.Advantage.dll
    • Leadtools.Codecs.Bmp.dll
    • Leadtools.Codecs.Cmp.dll
    • Leadtools.Codecs.Tif.dll
    • Leadtools.Codecs.Fax.dll

    Note: The Leadtools.Codecs.*.dll references added are for the BMP, JPG, CMP, TIF and FAX image file formats. Add any additional file format codec DLL if required in your application.

  6. Switch to Form1 code view (Right-click Form1 in the solution explorer then select View Code) and add the following lines at the beginning of the file after any using or Imports section if there are any:

    C#
          using Leadtools; 
          using Leadtools.Codecs; 
          using Leadtools.Drawing; 
          using Leadtools.Controls; 
          using Leadtools.Forms; 
          using Leadtools.Forms.Ocr; 
          using Leadtools.Forms.DocumentWriters; 
         

    VB
          Imports Leadtools 
          Imports Leadtools.Codecs 
          Imports Leadtools.Drawing 
          Imports Leadtools.Controls 
          Imports Leadtools.Forms 
          Imports Leadtools.Forms.Ocr 
          Imports Leadtools.Forms.DocumentWriters 
         

  7. Add the following private variables to the Form1 class:

    C#
          // Image viewer instance 
          private ImageViewer _imageViewer; 
          // The OCR engine instance 
          private IOcrEngine _ocrEngine; 
          // The OCR page 
          private IOcrPage _ocrPage; 
          // Recognition results (characters with bounds information) 
          private IOcrZoneCharacters _zoneCharacters; 
         

    VB
          ' Image viewer instance 
          Private _imageViewer As ImageViewer 
          ' The OCR engine instance 
          Private _ocrEngine As IOcrEngine 
          ' The OCR page 
          Private _ocrPage As IOcrPage 
          ' Recognition results (characters with bounds information) 
          Private _zoneCharacters As IOcrZoneCharacters 
         

  8. Override Form1OnLoad and add the following code:

    C#
          protected override void OnLoad(EventArgs e) 
          { 
             // Add the image viewer to the form 
             _imageViewer = new ImageViewer(); 
             _imageViewer.Dock = DockStyle.Fill; 
             Controls.Add(_imageViewer); 
             _imageViewer.BringToFront(); 
             // Show images using their true size 
             _imageViewer.UseDpi = true; 
                     
             // Add ability to pan/zoom using the left mouse button 
             ImageViewerPanZoomInteractiveMode panZoomMode = new ImageViewerPanZoomInteractiveMode(); 
             panZoomMode.MouseButtons = MouseButtons.Left; 
             _imageViewer.InteractiveModes.Add(panZoomMode); 
             // Add a rubber band mode to the right mouse button 
             ImageViewerRubberBandInteractiveMode rubbBandMode = new ImageViewerRubberBandInteractiveMode(); 
             rubbBandMode.MouseButtons = MouseButtons.Right; 
             // Hook to the rubber-band completed event so we can recognize that area 
             rubbBandMode.RubberBandCompleted += rubbBandMode_RubberBandCompleted; 
             _imageViewer.InteractiveModes.Add(rubbBandMode); 
                     
             // Initialize the OCR engine 
             _ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Advantage, false); 
             // Startup the engine 
             _ocrEngine.Startup(null, null, null, @"C:\LEADTOOLS 19\Bin\Common\OcrAdvantageRuntime"); 
             // Create the OCR page from an image 
             string fileName = @"C:\Users\Public\Documents\LEADTOOLS Images\Ocr1.tif"; 
             RasterImage rasterImage = _ocrEngine.RasterCodecsInstance.Load(fileName, 1); 
             _ocrPage = _ocrEngine.CreatePage(rasterImage, OcrImageSharingMode.AutoDispose); 
             // Show this image in the viewer 
             _imageViewer.Image = _ocrPage.GetRasterImage(); 
             Text = "Left click and drag to pan, CTRL-Left and drag click to zoom, right click and draw to draw the selection rectangle"; 
                     
             // Hook to the post render event of the viewer so we can render the recognition result on the page 
             _imageViewer.PostRender += _imageViewer_PostRender; 
                     
             base.OnLoad(e); 
          } 
         

    VB
          Protected Overrides Sub OnLoad(e As EventArgs) 
             ' Add the image viewer to the form 
             _imageViewer = New ImageViewer() 
             _imageViewer.Dock = DockStyle.Fill 
             Controls.Add(_imageViewer) 
             _imageViewer.BringToFront() 
             ' Show images using their true size 
             _imageViewer.UseDpi = True 
                      
             ' Add ability to pan/zoom using the left mouse button 
             Dim panZoomMode As New ImageViewerPanZoomInteractiveMode() 
             panZoomMode.MouseButtons = MouseButtons.Left 
             _imageViewer.InteractiveModes.Add(panZoomMode) 
             ' Add a rubber band mode to the right mouse button 
             Dim rubbBandMode As New ImageViewerRubberBandInteractiveMode() 
             rubbBandMode.MouseButtons = MouseButtons.Right 
             ' Hook to the rubber-band completed event so we can recognize that area 
             AddHandler rubbBandMode.RubberBandCompleted, AddressOf rubbBandMode_RubberBandCompleted 
             _imageViewer.InteractiveModes.Add(rubbBandMode) 
                      
             ' Initialize the OCR engine 
             _ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Advantage, False) 
             ' Startup the engine 
             _ocrEngine.Startup(Nothing, Nothing, Nothing, "C:\LEADTOOLS 19\Bin\Common\OcrAdvantageRuntime") 
             ' Create the OCR page from an image 
             Dim fileName As String = "C:\Users\Public\Documents\LEADTOOLS Images\Ocr1.tif" 
             Dim rasterImage As RasterImage = _ocrEngine.RasterCodecsInstance.Load(fileName, 1) 
             _ocrPage = _ocrEngine.CreatePage(rasterImage, OcrImageSharingMode.AutoDispose) 
             ' Show this image in the viewer 
             _imageViewer.Image = _ocrPage.GetRasterImage() 
             Text = "Left click and drag to pan, CTRL-Left and drag click to zoom, right click and draw to draw the selection rectangle" 
                      
             ' Hook to the post render event of the viewer so we can render the recognition result on the page 
             AddHandler _imageViewer.PostRender, AddressOf _imageViewer_PostRender 
                      
             MyBase.OnLoad(e) 
          End Sub 
         

  9. Override Form1OnFormClosed and add the following code:

    C#
          protected override void OnFormClosed(FormClosedEventArgs e) 
          { 
             // Destroy the page 
             _ocrPage.Dispose(); 
                        
             // And the engine 
             _ocrEngine.Dispose(); 
                        
             base.OnFormClosed(e); 
          } 
         

    VB
          Protected Overrides Sub OnFormClosed(e As FormClosedEventArgs) 
             ' Destroy the page 
             _ocrPage.Dispose() 
                     
             ' And the engine 
             _ocrEngine.Dispose() 
                     
             MyBase.OnFormClosed(e) 
          End Sub 
         

  10. Add the following code to handle the rubber band completed event. Draw the zone and recognize it:

    C#
          private void rubbBandMode_RubberBandCompleted(object sender, ImageViewerRubberBandEventArgs e) 
          { 
             // Use has drawn a selection rectangle 
                     
             // Remove current zone from the page 
             _ocrPage.Zones.Clear(); 
                     
             // Add the new zone. The points from rubber band is in control coordinates, need to convert them to image 
             LeadRect bounds = LeadRect.FromLTRB(e.Points[0].X, e.Points[0].Y, e.Points[1].X, e.Points[1].Y); 
             bounds = _imageViewer.ConvertRect(null, ImageViewerCoordinateType.Control, ImageViewerCoordinateType.Image, bounds); 
                     
             // Add a new text zone from these coordinates 
             OcrZone ocrZone = new OcrZone(); 
             ocrZone.ZoneType = OcrZoneType.Text; 
             ocrZone.Bounds = new LogicalRectangle(bounds); 
             _ocrPage.Zones.Add(ocrZone); 
                     
             // Recognize the page to get the text of the zone 
             _ocrPage.Recognize(null); 
                     
             // Get the recognized characters 
             IOcrPageCharacters pageCharacters = _ocrPage.GetRecognizedCharacters(); 
             _zoneCharacters = pageCharacters[0]; 
                     
             // Refresh the viewer so we render the character bounds and positions 
             _imageViewer.Refresh(); 
                     
             // Show the result in a message box 
             string text = _ocrPage.GetText(0); 
             if (string.IsNullOrEmpty(text)) 
                text = "[Nothing found]"; 
             MessageBox.Show(this, text); 
          } 
         

    VB
          Private Sub rubbBandMode_RubberBandCompleted(sender As Object, e As ImageViewerRubberBandEventArgs) 
             ' Use has drawn a selection rectangle 
                     
             ' Remove the current zone from the page 
             _ocrPage.Zones.Clear() 
                     
             ' Add the new zone. The points from rubber band is in control coordinates, need to convert them to image 
             Dim bounds As LeadRect = LeadRect.FromLTRB(e.Points(0).X, e.Points(0).Y, e.Points(1).X, e.Points(1).Y) 
             bounds = _imageViewer.ConvertRect(Nothing, ImageViewerCoordinateType.Control, ImageViewerCoordinateType.Image, bounds) 
                     
             ' Add a new text zone from these coordinates 
             Dim ocrZone As New OcrZone() 
             ocrZone.ZoneType = OcrZoneType.Text 
             ocrZone.Bounds = New LogicalRectangle(bounds) 
             _ocrPage.Zones.Add(ocrZone) 
                     
             ' Recognize the page to get the text of the zone 
             _ocrPage.Recognize(Nothing) 
                     
             ' Get the recognized characters 
             Dim pageCharacters As IOcrPageCharacters = _ocrPage.GetRecognizedCharacters() 
             _zoneCharacters = pageCharacters(0) 
                     
             ' Refresh the viewer so we render the character bounds and positions 
             _imageViewer.Refresh() 
                     
             ' Show the result in a message box 
             Dim text As String = _ocrPage.GetText(0) 
             If String.IsNullOrEmpty(text) Then 
                text = "[Nothing found]" 
             End If 
             MessageBox.Show(Me, text) 
          End Sub 
         

  11. Finally (optional) add the following code to handle the image viewer post render event. Render the characters:

    C#
          private void _imageViewer_PostRender(object sender, ImageViewerRenderEventArgs e) 
          { 
             // Draw the recognition results (if we have any) 
             if (_zoneCharacters == null) 
                return; 
                     
             Graphics graphics = e.PaintEventArgs.Graphics; 
                     
             using (Brush characterBrush = new SolidBrush(Color.FromArgb(128, Color.Black))) 
             { 
                foreach (OcrCharacter character in _zoneCharacters) 
                { 
                   // Get the character boundary 
                   LogicalRectangle characterBounds = character.Bounds; 
                   // The character bounds is a logical rectangle, it may be in units other than pixels. Convert to pixels 
                   LeadRect bounds = characterBounds.ToRectangle(_ocrPage.DpiX, _ocrPage.DpiY); 
                   // Convert the bound to what we see in the viewer 
                   // Note that this demo does not have rotation; otherwise, you need to use the four corner points 
                   bounds = _imageViewer.ConvertRect(null, ImageViewerCoordinateType.Image, ImageViewerCoordinateType.Control, bounds); 
                     
                   // Highlight the character area on the page 
                   graphics.FillRectangle(characterBrush, bounds.X, bounds.Y, bounds.Width, bounds.Height); 
                   graphics.DrawRectangle(Pens.Black, bounds.X, bounds.Y, bounds.Width - 1, bounds.Height - 1); 
                     
                   // Finally draw the character code it self 
                   graphics.DrawString(new string(new char[] { character.Code }), this.Font, Brushes.White, bounds.X, bounds.Y); 
                } 
             } 
          } 
         

    VB
          Private Sub _imageViewer_PostRender(sender As Object, e As ImageViewerRenderEventArgs) 
             ' Draw the recognition results (if we have any) 
             If IsNothing(_zoneCharacters) Then Return 
                     
             Dim graphics As Graphics = e.PaintEventArgs.Graphics 
                     
             Using characterBrush As New SolidBrush(Color.FromArgb(128, Color.Black)) 
                For Each character As OcrCharacter In _zoneCharacters 
                   ' Get the character boundary 
                   Dim characterBounds As LogicalRectangle = character.Bounds 
                   ' The character bounds is a logical rectangle, it may be in units other than pixels. Convert to pixels 
                   Dim bounds As LeadRect = characterBounds.ToRectangle(_ocrPage.DpiX, _ocrPage.DpiY) 
                   ' Convert the bound to what we see in the viewer 
                   ' Note that this demo does not have rotation; otherwise, you need to use the four corner points 
                   bounds = _imageViewer.ConvertRect(Nothing, ImageViewerCoordinateType.Image, ImageViewerCoordinateType.Control, bounds) 
                     
                   ' Highlight the character area on the page 
                   graphics.FillRectangle(characterBrush, bounds.X, bounds.Y, bounds.Width, bounds.Height) 
                   graphics.DrawRectangle(Pens.Black, bounds.X, bounds.Y, bounds.Width - 1, bounds.Height - 1) 
                     
                   ' Finally draw the character code it self 
                   graphics.DrawString(New String(New Char() {character.Code}), Me.Font, Brushes.White, bounds.X, bounds.Y) 
                Next 
             End Using 
          End Sub 
         

  12. Save this project to use for testing other code samples.

Help Version 19.0.2017.10.27
Products | Support | Contact Us | Copyright Notices
© 1991-2017 LEAD Technologies, Inc. All Rights Reserved.
LEADTOOLS Imaging, Medical, and Document