LEADTOOLS OCR

フレームを表示

OCRページの上でゾーンをインタラクティブに描画する方法とこれらのゾーンを認める方法を示すプログラムを作成して、実行するために、以下の手順をとります。チュートリアルは、迅速かつ簡単にOCRプログラムを作成する方法を提供することを目的としています。

  1. Visual Studio 2005または2008を開始します

  2. メニューからファイル->新規作成->プロジェクトを選択します

  3. 新しいプロジェクトダイアログボックスでは、プロジェクト型の並びで「Visual C#プロジェクト」か「Visual Basicプロジェクト」を選択して、テンプレートリストからVisual Studioバージョンに従い「Windowsアプリケーション」または「Windowsフォームアプリケーション」を選択します

  4. プロジェクト名前フィールドの「OcrTutorial2」としてプロジェクト名を入力します、そして、OKを選択します。必要であれば、プロジェクトの新しい場所を入力するか、ブラウズボタンを用いてディレクトリを選択します、そして、OKを選択します。

  5. 「ソリューションエクスプローラー」ウィンドウでは、「参照」フォルダを右クリックします、そして、コンテキストメニューから「参照の追加...」を選択します。「参照の追加」ダイアログボックスでは、「.NET」タブを選択して、LEADTOOLS for .NET「#LEADTOOLS_INSTALLDIR#\Bin\DotNet4\Win32」フォルダに閲覧して、以下のDLLを選択します:

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

    注意:追加されるLeadtools.Codecs.*.dll参照は、BMP、JPG、CMP、TIFとFAX画像ファイル形式のためです。アプリケーションで必要であるならば、任意の追加のファイルフォーマットコーデックDLLを追加します。

  6. :があるならば、Form1 codeビュー(ソリューションエクスプローラーの右クリックForm1ビューCodeをその後選択します)に切り替えて、任意だったusingあとファイルの始めに行に続くことまたはImportsセクションを追加します

    [C#]

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

    [Visual Basic]

    
                 Imports Leadtools
                 Imports Leadtools.Codecs
                 Imports Leadtools.Drawing
                 Imports Leadtools.Controls
                 Imports Leadtools.Forms
                 Imports Leadtools.Forms.Ocr
                 Imports Leadtools.Forms.DocumentWriters
                 
    
  7. Form1クラスに以下のプライベート変数を追加します:

    [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;
                 
    

    [Visual Basic]

    
                 ' 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. Form1OnLoadをオーバーライドして、以下の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);
                 }
                 
    

    Visual Basic

    
                 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. Form1OnFormClosedをオーバーライドして、以下のcodeを追加します:

    [C#]

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

    Visual Basic

    
                 Protected Overrides Sub OnFormClosed(e As FormClosedEventArgs)
                    ' Destroy the page
                    _ocrPage.Dispose()
                
                    ' And the engine
                    _ocrEngine.Dispose()
                
                    MyBase.OnFormClosed(e)
                 End Sub
                 
    
  10. ラバーバンド完了されたイベントを処理するために、以下のcodeを追加します。ゾーンを描画して、それを認めます:

    [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);
                 }
                 
    

    Visual Basic

    
                 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. 最後に、以下が画像ビューワポストを処理するためにコード化する(オプション) addは、イベントを提出します。文字を提出します:

    [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);
                       }
                    }
                 }
                 
    

    Visual Basic

    
                 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. テスト他のコードサンプルの用途に、このプロジェクトを保存します。

参照

参照

OCRチュートリアル - ページの操作
OCRチュートリアル - ページの認識
OCRチュートリアル - ゾーンの追加と描画
OCRチュートリアル - 検索可能PDFのスキャン
概要
ご使用にあたって(サンプルプログラムへのガイド)
LEADTOOLSは、.NETアセンブリのOCR処理を行います
LEADTOOLS .NET OCRによるプログラミング
OCR認識モジュールの概要
OCRエンジンインスタンスの作成
OCRエンジンの開始と終了
LEADTOOLS OCRによるマルチスレッド
OCRスペル言語辞書
OCR言語の操作
OCRページの操作
OCRゾーンの操作
OCRページを認めること
OCR信用度の報告
LEADTOOLS .NET OCRのOMRを使用して
OCR言語とスペルチェック
エンジンに特有の設定のOCR処理を行います