C#调用halcon dll的过程(通俗易懂,需要的宝贝们按照步骤一步一步就能完成)

1.首先我们打开这个软件

下面按照做为基础模板创建好,最初的界面是这样的

在自己的安装路径当中找到还有halcon下面的这个halcon.dll

文件,比如我的绝对路径是在这个下面

添加到自己的

和工具箱下面

工具箱是直接将其拖至BindingNavigator下面,至此我们的准备工作就算做好了

接着我们创建一个窗体来实现调用功能,在工具箱中寻找相应的按钮,完成准备工作

代码实现如下

using HalconDotNet;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 第一个联合项目
{

    public partial class BlobForm : Form
    {
        public BlobForm()
        {
            InitializeComponent();


            // comboBoxs--->一个个加了三个选项
            comboBox_feature.Items.Add("area");
            comboBox_feature.Items.Add("circularity");
            comboBox_feature.Items.Add("rectangularity");
            comboBox_feature.Items.Add("radius");
            //选择首个选项
            comboBox_feature.SelectedIndex = 0;
        }

        HObject ho_image;
        private void button_readImage_Click(object sender, EventArgs e)
        {


            HOperatorSet.SetColor(hWindowControl_yuanshi.HalconWindow, "green");

            OpenFileDialog openFileDialog = new OpenFileDialog();
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                HOperatorSet.ReadImage(out ho_image, openFileDialog.FileName);

            }
            HTuple width, height;
            //获得图片的宽高
            HOperatorSet.GetImageSize(ho_image, out width, out height);

            //设置显示范围
            //HWindowControl.HalconWindow -->控件的句柄  设置显示范围
            HOperatorSet.SetPart(hWindowControl_yuanshi.HalconWindow, 0, 0, (height - 1), (width - 1));

            //阈值分割
            HObject Region;
            HOperatorSet.Threshold(ho_image,out Region,0,50);
            //打散
            HObject ConnectionRegion;
            HOperatorSet.Connection(Region, out ConnectionRegion);
            //特征筛选
            HObject SelectRegion;
            //HOperatorSet.SelectShape(ConnectionRegion,out SelectRegion, "area","and", 3227.85, 7897.33);
            HOperatorSet.SelectShape(ConnectionRegion, out SelectRegion, comboBox_feature.SelectedItem.ToString(), "and",Double.Parse(textBox_Min.Text), Double.Parse(textBox_Max.Text));
           
           //显示
           HOperatorSet.DispObj(ho_image, hWindowControl_yuanshi.HalconWindow);
            HOperatorSet.DispObj(SelectRegion, hWindowControl_yuanshi.HalconWindow);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            //获得comboBox下拉选项
            MessageBox.Show(comboBox_feature.SelectedItem.ToString());
        }

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
           
            if(openFileDialog.ShowDialog()==DialogResult.OK) 
            {
                MessageBox.Show(openFileDialog.FileName);
            }
        }
    }
}

这是BlobForm.cs的代码,还有BlobForm.Designer.cs的代码,如下:
 

namespace 第一个联合项目
{
    partial class BlobForm
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.button_readImage = new System.Windows.Forms.Button();
            this.hWindowControl_yuanshi = new HalconDotNet.HWindowControl();
            this.comboBox_feature = new System.Windows.Forms.ComboBox();
            this.button1 = new System.Windows.Forms.Button();
            this.textBox_Min = new System.Windows.Forms.TextBox();
            this.textBox_Max = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.button2 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // button_readImage
            // 
            this.button_readImage.Location = new System.Drawing.Point(39, 549);
            this.button_readImage.Name = "button_readImage";
            this.button_readImage.Size = new System.Drawing.Size(228, 84);
            this.button_readImage.TabIndex = 3;
            this.button_readImage.Text = "读取图片";
            this.button_readImage.UseVisualStyleBackColor = true;
            this.button_readImage.Click += new System.EventHandler(this.button_readImage_Click);
            // 
            // hWindowControl_yuanshi
            // 
            this.hWindowControl_yuanshi.BackColor = System.Drawing.Color.Black;
            this.hWindowControl_yuanshi.BorderColor = System.Drawing.Color.Black;
            this.hWindowControl_yuanshi.ImagePart = new System.Drawing.Rectangle(0, 0, 640, 480);
            this.hWindowControl_yuanshi.Location = new System.Drawing.Point(39, 24);
            this.hWindowControl_yuanshi.Name = "hWindowControl_yuanshi";
            this.hWindowControl_yuanshi.Size = new System.Drawing.Size(536, 509);
            this.hWindowControl_yuanshi.TabIndex = 4;
            this.hWindowControl_yuanshi.WindowSize = new System.Drawing.Size(536, 509);
            // 
            // comboBox_feature
            // 
            this.comboBox_feature.FormattingEnabled = true;
            this.comboBox_feature.Location = new System.Drawing.Point(364, 588);
            this.comboBox_feature.Name = "comboBox_feature";
            this.comboBox_feature.Size = new System.Drawing.Size(211, 26);
            this.comboBox_feature.TabIndex = 5;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(425, 669);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(309, 72);
            this.button1.TabIndex = 6;
            this.button1.Text = "点击按钮获得Combox的值";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // textBox_Min
            // 
            this.textBox_Min.Location = new System.Drawing.Point(716, 490);
            this.textBox_Min.Name = "textBox_Min";
            this.textBox_Min.Size = new System.Drawing.Size(134, 28);
            this.textBox_Min.TabIndex = 7;
            // 
            // textBox_Max
            // 
            this.textBox_Max.Location = new System.Drawing.Point(716, 549);
            this.textBox_Max.Name = "textBox_Max";
            this.textBox_Max.Size = new System.Drawing.Size(134, 28);
            this.textBox_Max.TabIndex = 7;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(647, 493);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(44, 18);
            this.label1.TabIndex = 8;
            this.label1.Text = "最小";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(647, 552);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(44, 18);
            this.label2.TabIndex = 8;
            this.label2.Text = "最大";
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(760, 98);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(184, 90);
            this.button2.TabIndex = 9;
            this.button2.Text = "选择文件路径";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // BlobForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 18F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(1074, 789);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.textBox_Max);
            this.Controls.Add(this.textBox_Min);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.comboBox_feature);
            this.Controls.Add(this.hWindowControl_yuanshi);
            this.Controls.Add(this.button_readImage);
            this.Name = "BlobForm";
            this.Text = "BlobForm";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Button button_readImage;
        private HalconDotNet.HWindowControl hWindowControl_yuanshi;
        private System.Windows.Forms.ComboBox comboBox_feature;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.TextBox textBox_Min;
        private System.Windows.Forms.TextBox textBox_Max;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Button button2;
    }
}

实现效果如下:
在halcon软件当中的实现效果是这样的:

  • 20
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#调用Halcon引擎可以通过添加引用halcondotnet.dll和hdevenginedonet.dll来实现。以下是两种调用Halcon引擎的方法: 1. 在C#中使用Halcon引擎调用Halcon程序 首先,需要Halcon中导出C#文件,然后在C#调用。具体步骤如下: - 在Halcon中打开需要调用的程序。 - 选择“文件”->“导出”->“导出C#文件”。 - 在C#中添加对halcondotnet.dll和hdevenginedonet.dll的引用。 - 在C#中创建Halcon引擎实例并调用Halcon程序。 以下是一个示例代码: ```csharp using HalconDotNet; class Program { static void Main(string[] args) { // 创建Halcon引擎实例 HDevEngine engine = new HDevEngine(); engine.SetScriptPath("path/to/halcon/program"); // 调用Halcon程序 engine.ExecuteScript("halcon_program.cs"); } } ``` 2. 在C#中使用Halcon引擎调用.hdev下本地函数或者.hdvp外部函数 在C#中使用Halcon引擎调用.hdev下本地函数或者.hdvp外部函数也是通过添加引用halcondotnet.dll和hdevenginedonet.dll来实现。具体步骤如下: - 在C#中添加对halcondotnet.dll和hdevenginedonet.dll的引用。 - 在C#中创建Halcon引擎实例并调用.hdev下本地函数或者.hdvp外部函数。 以下是一个示例代码: ```csharp using HalconDotNet; class Program { static void Main(string[] args) { // 创建Halcon引擎实例 HDevEngine engine = new HDevEngine(); // 调用.hdev下本地函数 engine.CallProcedure("local_procedure"); // 调用.hdvp外部函数 engine.CallExternalProcedure("external_procedure", "path/to/external/procedure.hdvp"); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值