halcon与C#混合编程(三)数字识别-阿里云开发者社区

开发者社区> zting科技> 正文

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

作者:韩兆新
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
初识面向对象第三特征-多态 | 带你学《Java面向对象编程》之四十七
本节主要介绍了面向对象的第三大特征-多态性,并简要说明了实现方法多态性的实现模式,介绍了对象多态性这个概念。
770 0
阿里云服务器怎么设置密码?怎么停机?怎么重启服务器?
如果在创建实例时没有设置密码,或者密码丢失,您可以在控制台上重新设置实例的登录密码。本文仅描述如何在 ECS 管理控制台上修改实例登录密码。
10052 0
[并发编程]并发编程第一篇:利用并发编程,实现查找大量数据中的素数
面试时,被提问:怎么判断一个数是素数?如果高效地判断大批量数据是否是素数? 由于工作中对并发编程没有了解,所以不太明白,面试官提示了下,也并没有自己的思路。 回来后,百度了下这个面试题,并浏览了下实现代码,并不是特别明白。
649 0
阿里云服务器如何登录?阿里云服务器的三种登录方法
购买阿里云ECS云服务器后如何登录?场景不同,阿里云优惠总结大概有三种登录方式: 登录到ECS云服务器控制台 在ECS云服务器控制台用户可以更改密码、更换系.
13865 0
初识“数据巨轮”:数组 | 带你学《Java面向对象编程》之十四
本节将通过几个小例子带读者了解数组这一概念,并带领读者简单掌握数组的定义与初始化方法。
1275 0
HTML + CSS 为何得不到编程界的认可?
如果我在用HTML+CSS编程,那么,我能算是名开发人员吗?
1170 0
+关注
3550
文章
0
问答
文章排行榜
最热
最新
相关电子书
更多
《2021云上架构与运维峰会演讲合集》
立即下载
《零基础CSS入门教程》
立即下载
《零基础HTML入门教程》
立即下载