Halcon和C#混合编程(一):数字识别

这里写图片描述 这里写图片描述 这里写图片描述 这里写图片描述
这里写图片描述](https://img-blog.csdn.net/20180820233012117?   watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3RoaXNpc3pkeQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)                               ![这里写图片描述 这里写图片描述 这里写图片描述 这里写图片描述 这里写图片描述

Halcon导出C#代码

using System;
using HalconDotNet;

public partial class HDevelopExport
{
  public HTuple hv_ExpDefaultWinHandle;

  // Main procedure 
  private void action()
  {
    // Local iconic variables 

    HObject ho_Image, ho_GrayImage, ho_Region;
    HObject ho_ConnectedRegions, ho_RegionUnion, ho_RegionClosing;

    // Local control variables 

    HTuple hv_OCRHandle = null, hv_Class = null;
    HTuple hv_Confidence = null;
    // Initialize local and output iconic variables 
    HOperatorSet.GenEmptyObj(out ho_Image);
    HOperatorSet.GenEmptyObj(out ho_GrayImage);
    HOperatorSet.GenEmptyObj(out ho_Region);
    HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
    HOperatorSet.GenEmptyObj(out ho_RegionUnion);
    HOperatorSet.GenEmptyObj(out ho_RegionClosing);
    ho_Image.Dispose();
    HOperatorSet.ReadImage(out ho_Image, "C:/Users/thisi/Desktop/数字图像/1.jpg");
    HOperatorSet.DispObj(ho_Image, hv_ExpDefaultWinHandle);
    ho_GrayImage.Dispose();
    HOperatorSet.Rgb1ToGray(ho_Image, out ho_GrayImage);
    ho_Region.Dispose();
    HOperatorSet.Threshold(ho_GrayImage, out ho_Region, 0, 100);
    ho_ConnectedRegions.Dispose();
    HOperatorSet.Connection(ho_Region, out ho_ConnectedRegions);
    ho_RegionUnion.Dispose();
    HOperatorSet.Union1(ho_ConnectedRegions, out ho_RegionUnion);
    ho_RegionClosing.Dispose();
    HOperatorSet.ClosingCircle(ho_RegionUnion, out ho_RegionClosing, 3.5);
    HOperatorSet.ReadOcrClassMlp("Document_0-9_NoRej.omc", out hv_OCRHandle);
    HOperatorSet.DoOcrMultiClassMlp(ho_RegionClosing, ho_GrayImage, hv_OCRHandle, 
        out hv_Class, out hv_Confidence);
    HOperatorSet.ClearOcrClassMlp(hv_OCRHandle);
    ho_Image.Dispose();
    ho_GrayImage.Dispose();
    ho_Region.Dispose();
    ho_ConnectedRegions.Dispose();
    ho_RegionUnion.Dispose();
    ho_RegionClosing.Dispose();
  }

  public void InitHalcon()
  {
    // Default settings used in HDevelop 
    HOperatorSet.SetSystem("width", 512);
    HOperatorSet.SetSystem("height", 512);
  }

  public void RunHalcon(HTuple Window)
  {
    hv_ExpDefaultWinHandle = Window;
    action();
  }
}

C#UI界面代码

using HalconDotNet;
namespace _20180811_1
{
    public partial class Form1 : Form
    {
        string[] ImagePath;
        public HTuple hv_ExpDefaultWinHandle;
        HObject ho_Image, ho_GrayImage, ho_Region;
        HObject ho_ConnectedRegions, ho_RegionUnion, ho_RegionClosing;
        int n = 0;
        bool a = true;
        // Local control variables 

        HTuple hv_OCRHandle = null, hv_Class = null;
        HTuple hv_Confidence = null;

        private void btnPro_Click(object sender, EventArgs e)
        {
            HOperatorSet.GenEmptyObj(out ho_GrayImage);
            HOperatorSet.GenEmptyObj(out ho_Region);
            HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
            HOperatorSet.GenEmptyObj(out ho_RegionUnion);
            HOperatorSet.GenEmptyObj(out ho_RegionClosing);
            ho_GrayImage.Dispose();
            HOperatorSet.Rgb1ToGray(ho_Image, out ho_GrayImage);
            ho_Region.Dispose();
            HOperatorSet.Threshold(ho_GrayImage, out ho_Region, 0, 100);
            ho_ConnectedRegions.Dispose();
            HOperatorSet.Connection(ho_Region, out ho_ConnectedRegions);
            ho_RegionUnion.Dispose();
            HOperatorSet.Union1(ho_ConnectedRegions, out ho_RegionUnion);
            ho_RegionClosing.Dispose();
            HOperatorSet.ClosingCircle(ho_RegionUnion, out ho_RegionClosing, 3.5);
            HOperatorSet.ReadOcrClassMlp("Document_0-9_NoRej.omc", out hv_OCRHandle);
            HOperatorSet.DoOcrMultiClassMlp(ho_RegionClosing, ho_GrayImage, hv_OCRHandle,
                out hv_Class, out hv_Confidence);
            HOperatorSet.ClearOcrClassMlp(hv_OCRHandle);
            ho_Image.Dispose();
            ho_GrayImage.Dispose();
            ho_Region.Dispose();
            ho_ConnectedRegions.Dispose();
            ho_RegionUnion.Dispose();
            ho_RegionClosing.Dispose();
            label2.Text = hv_Class;
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void btnRead_Click(object sender, EventArgs e)
        {
            if (n == 9)
            {
                n = 0;
            }
            if (a)
            {
                OpenFileDialog openFileDialog1 = new OpenFileDialog();
                openFileDialog1.Multiselect = true;
                openFileDialog1.Filter = "JPEG文件|*.jpg*|BMP文件|*.bmp*|TIFF文件|*.tiff*";
                openFileDialog1.RestoreDirectory = true;
                openFileDialog1.FilterIndex = 1;
                    if (openFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        ImagePath = openFileDialog1.FileNames;
                    }
                a = false;
            }           
            btnRead.Text = "下张图";
            HOperatorSet.GenEmptyObj(out ho_Image);
            ho_Image.Dispose();
            HOperatorSet.ReadImage(out ho_Image, ImagePath[n]);
            HOperatorSet.DispObj(ho_Image, hWindowControl1.HalconWindow);            
            n+= 1;
        }
    }
}

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值