一、前期准备:
HDevelop 20.11 Steady - MVTec HALCON
Visual Studio 2022
版本不做要求
将其中的九个文件备份一份
二、C#引用dll文件
创建Windows窗体应用,将备份的九个文件拷贝到bin---->Debug中
1.右键添加引用——点击“浏览”,将halcondotnet.dll文件添加进去
2.打开Form1.cs文件---->打开工具箱将引用的文件拖拽进去,完成前期准备
3.控件放置,界面按自己喜欢的方式进行布局
********************************注意,所有命名全部可以按照自己喜欢的方式**************************
(1)第一个窗体用于显示原图,第二个窗体用于显示处理后的图片,命名为:hWindowControl_image和hWindowControl_Threshold
(2)添加两个button,其中一个设置为“读取图片”另一个设置为“图像处理”
(3) 添加四个label和TextBox内容如下
4.代码编写
(1)读取图片
双击读取图片按钮,添加点击事件
变量设置
从文件夹中读取图片,并显示在窗体上
(2)图像处理
三、错误处理
出现下图的错误,只需打开属性,点击生成,去掉首选32位
四、代码部分
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 Form1 : Form
{
public Form1()
{
InitializeComponent();
// comboBoxs--->一个个加了三个选项
comboBox_feature.Items.Add("area");
comboBox_feature.Items.Add("circularity");
comboBox_feature.Items.Add("rectangularity");
//选择首个选项
comboBox_feature.SelectedIndex = 0;
}
private HObject ho_image;
HObject region;
HObject ConnectionRegion;
HObject SelectRegion;
HTuple width, height;
//读取图片
private void button1_Click(object sender, EventArgs e)
{
//通过打开文件夹读取图片
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);
//设置显示范围
HOperatorSet.SetPart(hWindowControl_image.HalconWindow,0,0,height-1,width-1);
HOperatorSet.DispObj(ho_image, hWindowControl_image.HalconWindow);
}
//阈值分割
HTuple MinGray;
HTuple MaxGray;
//特征筛选
HTuple Max;
HTuple Min;
private void button2_Click(object sender, EventArgs e)
{
//清除窗体
hWindowControl_image.HalconWindow.ClearWindow();
hWindowControl_Threshold.HalconWindow.ClearWindow();
//如果用户不按要求操作
if (ho_image == null)
{
button1_Click(null, null);
}
//设置显示的颜色
HOperatorSet.SetColor(hWindowControl_image.HalconWindow, "green");
HOperatorSet.SetColor(hWindowControl_Threshold.HalconWindow, "green");
//int.Parse(string) 字符串类型转换成int类型
MaxGray = int.Parse(textBox_MaxGray.Text);
MinGray = int.Parse(textBox_MinGray.Text);
//阈值分割
HOperatorSet.Threshold(ho_image, out region, MinGray, MaxGray);
//打散
HOperatorSet.Connection(region, out ConnectionRegion);
//特征筛选
Max = Double.Parse(textBox_Max.Text);
Min = Double.Parse(textBox_Min.Text);
//HOperatorSet.SelectShape(ConnectionRegion, out SelectRegion, "area", "and", 3227.85, 7897.33);
HOperatorSet.SelectShape(ConnectionRegion, out SelectRegion, comboBox_feature.SelectedItem.ToString(), "and", Min, Max);
HOperatorSet.GetImageSize(ho_image, out width, out height);
//HWindowControl.HalconWindow -->窗体句柄 设置显示范围
HOperatorSet.SetPart(hWindowControl_Threshold.HalconWindow, 0, 0, height-1 , width-1 );
//显示
HOperatorSet.DispObj(region, hWindowControl_image.HalconWindow);
HOperatorSet.DispObj(SelectRegion, hWindowControl_Threshold.HalconWindow);
}
}
}