欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-07-04 16:00:20
...

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

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

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

相关标签: Halcon