Fork me on GitHub

halcon与C#混合编程(三)数字识别

示例:数字识别

要识别的图片:

123

456

789

halcon源码:

   1: *读取图片
   2: read_image(Image,'D:/MyFile/halcon/数字识别/1.jpg')
   3:  
   4: *图像处理
   5: decompose3(Image, ImageR, ImageG, ImageB)
   6: threshold (ImageG, Regions, 0, 183)
   7: connection (Regions, Connection)
   8:  
   9: select_shape (Connection, SelectedRegions, 'area', 'and', 1987.72, 7500)
  10:  
  11: read_ocr_class_mlp('Document_0-9.omc',OCRHandle)
  12: do_ocr_multi_class_mlp(SelectedRegions,ImageG,OCRHandle, Class, Confidence)

halcon导出的C#代码:

   1: //
   2: //  File generated by HDevelop for HALCON/DOTNET (C#) Version 10.0
   3: //
   4: //  This file is intended to be used with the HDevelopTemplate or
   5: //  HDevelopTemplateWPF projects located under %HALCONEXAMPLES%\c#
   6:  
   7: using System;
   8: using HalconDotNet;
   9:  
  10: public partial class HDevelopExport
  11: {
  12:   public HTuple hv_ExpDefaultWinHandle;
  13:  
  14:   // Main procedure 
  15:   private void action()
  16:   {
  17:  
  18:     // Local iconic variables 
  19:  
  20:     HObject ho_Image, ho_ImageR, ho_ImageG, ho_ImageB;
  21:     HObject ho_Regions, ho_Connection, ho_SelectedRegions;
  22:  
  23:  
  24:     // Local control variables 
  25:  
  26:     HTuple hv_OCRHandle, hv_Class, hv_Confidence;
  27:  
  28:     // Initialize local and output iconic variables 
  29:     HOperatorSet.GenEmptyObj(out ho_Image);
  30:     HOperatorSet.GenEmptyObj(out ho_ImageR);
  31:     HOperatorSet.GenEmptyObj(out ho_ImageG);
  32:     HOperatorSet.GenEmptyObj(out ho_ImageB);
  33:     HOperatorSet.GenEmptyObj(out ho_Regions);
  34:     HOperatorSet.GenEmptyObj(out ho_Connection);
  35:     HOperatorSet.GenEmptyObj(out ho_SelectedRegions);
  36:  
  37:     //读取图片
  38:     ho_Image.Dispose();
  39:     HOperatorSet.ReadImage(out ho_Image, "D:/MyFile/halcon/数字识别/1.jpg");
  40:  
  41:     //图像处理
  42:     ho_ImageR.Dispose();
  43:     ho_ImageG.Dispose();
  44:     ho_ImageB.Dispose();
  45:     HOperatorSet.Decompose3(ho_Image, out ho_ImageR, out ho_ImageG, out ho_ImageB
  46:         );
  47:     ho_Regions.Dispose();
  48:     HOperatorSet.Threshold(ho_ImageG, out ho_Regions, 0, 183);
  49:     ho_Connection.Dispose();
  50:     HOperatorSet.Connection(ho_Regions, out ho_Connection);
  51:  
  52:     ho_SelectedRegions.Dispose();
  53:     HOperatorSet.SelectShape(ho_Connection, out ho_SelectedRegions, "area", "and", 
  54:         1987.72, 7500);
  55:  
  56:     HOperatorSet.ReadOcrClassMlp("Document_0-9.omc", out hv_OCRHandle);
  57:     HOperatorSet.DoOcrMultiClassMlp(ho_SelectedRegions, ho_ImageG, hv_OCRHandle, 
  58:         out hv_Class, out hv_Confidence);
  59:  
  60:     ho_Image.Dispose();
  61:     ho_ImageR.Dispose();
  62:     ho_ImageG.Dispose();
  63:     ho_ImageB.Dispose();
  64:     ho_Regions.Dispose();
  65:     ho_Connection.Dispose();
  66:     ho_SelectedRegions.Dispose();
  67:  
  68:   }
  69:  
  70:   public void InitHalcon()
  71:   {
  72:     // Default settings used in HDevelop 
  73:     HOperatorSet.SetSystem("do_low_error", "false");
  74:   }
  75:  
  76:   public void RunHalcon(HTuple Window)
  77:   {
  78:     hv_ExpDefaultWinHandle = Window;
  79:     action();
  80:   }
  81:  
  82: }
  83:  

C#工程:

   1: using System;
   2: using System.Collections.Generic;
   3: using System.ComponentModel;
   4: using System.Data;
   5: using System.Drawing;
   6: using System.Linq;
   7: using System.Text;
   8: using System.Windows.Forms;
   9:  
  10: using HalconDotNet;
  11:  
  12:  
  13: namespace NumericalRecognition
  14: {
  15:     public partial class FormMain : Form
  16:     {
  17:         //增加代码:
  18:         HDevelopExport HD = new HDevelopExport();
  19:         string ImagePath;
  20:  
  21:         public FormMain()
  22:         {
  23:             InitializeComponent();
  24:             btnRecognitionNumber.Enabled = false;
  25:         }
  26:  
  27:         private void btnOpenImage_Click(object sender, EventArgs e)
  28:         {
  29:             openFileDialog1.Filter = "JPEG文件|*.jpg*|BMP文件|*.bmp*|TIFF文件|*.tiff*";
  30:  
  31:             openFileDialog1.RestoreDirectory = true;
  32:  
  33:             openFileDialog1.FilterIndex = 1;
  34:  
  35:             if (openFileDialog1.ShowDialog() == DialogResult.OK)
  36:  
  37:             {
  38:  
  39:                 ImagePath = openFileDialog1.FileName;
  40:   
  41:                 HD.ReadImage(hWindowControl1.HalconWindow, ImagePath);
  42:   
  43:                 btnRecognitionNumber.Enabled = true;
  44:   
  45:             }   
  46:         }
  47:  
  48:         private void btnRecognitionNumber_Click(object sender, EventArgs e)
  49:         {
  50:             HD.NumberRecognition();
  51:             labNumber.Text = HD.hv_Class.ToString();
  52:  
  53:             btnRecognitionNumber.Enabled = false;
  54:         }
  55:     }
  56:  
  57:     //halcon导出的类
  58:     public partial class HDevelopExport
  59:     {
  60:         public HTuple hv_ExpDefaultWinHandle;
  61:  
  62:         HObject ho_Image, ho_ImageR, ho_ImageG, ho_ImageB;
  63:         HObject ho_Regions, ho_Connection, ho_SelectedRegions;
  64:  
  65:  
  66:         // Local control variables 
  67:         HTuple hv_OCRHandle,  hv_Confidence;
  68:         public HTuple hv_Class;
  69:  
  70:         public void InitHalcon()
  71:         {
  72:             // Default settings used in HDevelop 
  73:             HOperatorSet.SetSystem("do_low_error", "false");
  74:         }
  75:  
  76:         public void ReadImage(HTuple Window,string ImagePath)
  77:         {
  78:             hv_ExpDefaultWinHandle = Window;
  79:  
  80:             // Initialize local and output iconic variables 
  81:             HOperatorSet.GenEmptyObj(out ho_Image);
  82:             
  83:             //读取图片
  84:             ho_Image.Dispose();
  85:             HOperatorSet.ReadImage(out ho_Image, ImagePath);
  86:             HOperatorSet.DispObj(ho_Image, hv_ExpDefaultWinHandle);
  87:         }
  88:  
  89:         public void NumberRecognition()
  90:         {
  91:             HOperatorSet.GenEmptyObj(out ho_ImageR);
  92:             HOperatorSet.GenEmptyObj(out ho_ImageG);
  93:             HOperatorSet.GenEmptyObj(out ho_ImageB);
  94:             HOperatorSet.GenEmptyObj(out ho_Regions);
  95:             HOperatorSet.GenEmptyObj(out ho_Connection);
  96:             HOperatorSet.GenEmptyObj(out ho_SelectedRegions);
  97:             //图像处理
  98:             ho_ImageR.Dispose();
  99:             ho_ImageG.Dispose();
 100:             ho_ImageB.Dispose();
 101:             HOperatorSet.Decompose3(ho_Image, out ho_ImageR, out ho_ImageG, out ho_ImageB
 102:                 );
 103:             ho_Regions.Dispose();
 104:             HOperatorSet.Threshold(ho_ImageG, out ho_Regions, 0, 183);
 105:             ho_Connection.Dispose();
 106:             HOperatorSet.Connection(ho_Regions, out ho_Connection);
 107:  
 108:             ho_SelectedRegions.Dispose();
 109:             HOperatorSet.SelectShape(ho_Connection, out ho_SelectedRegions, "area", "and",
 110:                 1987.72, 7500);
 111:  
 112:             HOperatorSet.ReadOcrClassMlp("Document_0-9.omc", out hv_OCRHandle);
 113:             HOperatorSet.DoOcrMultiClassMlp(ho_SelectedRegions, ho_ImageG, hv_OCRHandle,
 114:                 out hv_Class, out hv_Confidence);
 115:             
 116:  
 117:             ho_Image.Dispose();
 118:             ho_ImageR.Dispose();
 119:             ho_ImageG.Dispose();
 120:             ho_ImageB.Dispose();
 121:             ho_Regions.Dispose();
 122:             ho_Connection.Dispose();
 123:             ho_SelectedRegions.Dispose();
 124:  
 125:         }
 126:  
 127:     }
 128: }

运行结果:

image image

posted @ 2013-02-07 18:39  韩兆新  阅读(16788)  评论(7编辑  收藏  举报