1. 程式人生 > >Halcon和C#混合程式設計(一):數字識別

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

這裡寫圖片描述