联合编程实践分俩部分
-》算法开发;
-》软件开发;
第一部分 算法开发
Step 1 :环境准备
注:其他版本亦可,步骤一样
b. 原图准备--任意一张图片
Step 2:打开halcon软件,输入以下程序
*1.读图
read_image (Test, 'C:/Users/Administrator/Desktop/test.png')
*2.彩色图转灰度图
rgb1_to_gray (Test, GrayImage)
*3.二值化
threshold (GrayImage, Regions, 0, 248)
*4.孔洞填充
fill_up (Regions, RegionFillUp)
*5.区域分割
connection (RegionFillUp, ConnectedRegions)
*6.面积筛选
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 52504.6, 153432)
*7.统计个数
count_obj (SelectedRegions, Number)
c. 运行效果如下
Step3:导出c#程序
a. 点击“文件” -》“导出”;
b.弹出如下窗口
c. 点击右侧文件夹图标,配置导出文件路径和格式,此Demo配置如下;
d. 其他选项一般默认即可,点击“导出”,会在配置路径下出现对应.cs文件;
Step 4 : 二次封装
a. 打开VS软件,新建类库;
b. 右键解决方案的依赖项,选择“添加引用”;
c. 点击浏览,找到halcon安装目录下的halcondonet.dll, 添加-》确定;
d. 右键项目“CountObjTool”,点击“添加” -》“现有项”,选择刚才导出的程序文件;
f. 将默认的Class1删除,并打开刚导入的类文件,找到方法action(),除此方法外,将其他方法都删除,并将程序整理如下;
using HalconDotNet;
namespace CountObjTool
{
public class CountObjTool
{
public void action()
{
// Local iconic variables
HObject ho_Test, ho_GrayImage, ho_Regions;
HObject ho_RegionFillUp, ho_ConnectedRegions, ho_SelectedRegions;
// Local control variables
HTuple hv_Number = new HTuple();
// Initialize local and output iconic variables
HOperatorSet.GenEmptyObj(out ho_Test);
HOperatorSet.GenEmptyObj(out ho_GrayImage);
HOperatorSet.GenEmptyObj(out ho_Regions);
HOperatorSet.GenEmptyObj(out ho_RegionFillUp);
HOperatorSet.GenEmptyObj(out ho_ConnectedRegions);
HOperatorSet.GenEmptyObj(out ho_SelectedRegions);
//1.读图
ho_Test.Dispose();
HOperatorSet.ReadImage(out ho_Test, "C:/Users/Administrator/Desktop/test.png");
//2.彩色图转灰度图
ho_GrayImage.Dispose();
HOperatorSet.Rgb1ToGray(ho_Test, out ho_GrayImage);
//3.二值化
ho_Regions.Dispose();
HOperatorSet.Threshold(ho_GrayImage, out ho_Regions, 0, 248);
//4.孔洞填充
ho_RegionFillUp.Dispose();
HOperatorSet.FillUp(ho_Regions, out ho_RegionFillUp);
//5.区域分割
ho_ConnectedRegions.Dispose();
HOperatorSet.Connection(ho_RegionFillUp, out ho_ConnectedRegions);
//6.面积筛选
ho_SelectedRegions.Dispose();
HOperatorSet.SelectShape(ho_ConnectedRegions, out ho_SelectedRegions, "area",
"and", 52504.6, 153432);
//7.统计个数
hv_Number.Dispose();
HOperatorSet.CountObj(ho_SelectedRegions, out hv_Number);
ho_Test.Dispose();
ho_GrayImage.Dispose();
ho_Regions.Dispose();
ho_RegionFillUp.Dispose();
ho_ConnectedRegions.Dispose();
ho_SelectedRegions.Dispose();
hv_Number.Dispose();
}
}
}
g. 整理输入参数、输出参数、检测方法,继续对此类封装,程序如下:
using HalconDotNet;
namespace CountObjTool
{
public class CountObjTool
{
/// <summary>
/// 输入源图像
/// </summary>
public HObject srcImg = null;
/// <summary>
/// 输出个数
/// </summary>
public int number = 0;
/// <summary>
/// 检测方法
/// </summary>
public void action()
{
//2.彩色图转灰度图
HOperatorSet.Rgb1ToGray(srcImg, out HObject ho_GrayImage);
//3.二值化
HOperatorSet.Threshold(ho_GrayImage, out HObject ho_Regions, 0, 248);
//4.孔洞填充
HOperatorSet.FillUp(ho_Regions, out HObject ho_RegionFillUp);
//5.区域分割
HOperatorSet.Connection(ho_RegionFillUp, out HObject ho_ConnectedRegions);
//6.面积筛选
HOperatorSet.SelectShape(ho_ConnectedRegions, out HObject ho_SelectedRegions, "area",
"and", 52504.6, 153432);
//7.统计个数
HOperatorSet.CountObj(ho_SelectedRegions, out HTuple hv_Number);
number = hv_Number[0].I;
ho_GrayImage.Dispose();
ho_Regions.Dispose();
ho_RegionFillUp.Dispose();
ho_ConnectedRegions.Dispose();
ho_SelectedRegions.Dispose();
hv_Number.Dispose();
}
}
}
第二部分 软件开发
Step 5 : 新建winform工程
a. 邮件解决方案CountObjTool,“添加”-》“新建项目”,选择“WIndows窗体应用”;
Step 6 : 关联算法项目、halcondotnet.dll 以及halconWindow控件
a. 选择项目CountObj,右键"引用" - 》"添加引用";
b. 选择"项目" -》"CountObjTool";
c.选择"浏览" -》"halcondotnet.dll";
d.打开工具箱,在空白处右键-选择项-
e. 点击”浏览“,选择下边文件”halcolcondotnet.dll“,在工具箱中添加halconWindow控件;
e. 勾选中下图加深的两个halcon组件,并点击确定;
f.工具会更新如下halcon显示控件;
Step 7 : 软件界面设计
a.添加如下窗体控件;
Step 8 : 逻辑代码设计
a.实例化对象计数工具类
CountObjTool.CountObjTool tool = new CountObjTool.CountObjTool();
b.双击读图按钮,其事件代码如下;
private void btnReadImg_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
HOperatorSet.ReadImage(out tool.srcImg, openFileDialog.FileName);
HOperatorSet.GetImageSize(tool.srcImg, out HTuple hv_Width, out HTuple hv_Height);
HOperatorSet.SetPart(this.hWindowControl1.HalconWindow, 0, 0, hv_Height - 1, hv_Width - 1);
HOperatorSet.DispObj(tool.srcImg, this.hWindowControl1.HalconWindow);
}
}
catch (Exception)
{
MessageBox.Show("图片文件错误!");
}
}
b.双击运行按钮,其事件代码如下;
private void btnRun_Click(object sender, EventArgs e)
{
try
{
tool.action();
txtCount.Text = tool.number.ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
c.软件界面所有代码如下:
using HalconDotNet;
using System;
using System.Windows.Forms;
namespace CountObj
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
CountObjTool.CountObjTool tool = new CountObjTool.CountObjTool();
private void btnReadImg_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
HOperatorSet.ReadImage(out tool.srcImg, openFileDialog.FileName);
HOperatorSet.GetImageSize(tool.srcImg, out HTuple hv_Width, out HTuple hv_Height);
HOperatorSet.SetPart(this.hWindowControl1.HalconWindow, 0, 0, hv_Height - 1, hv_Width - 1);
HOperatorSet.DispObj(tool.srcImg, this.hWindowControl1.HalconWindow);
}
}
catch (Exception)
{
MessageBox.Show("图片文件错误!");
}
}
private void btnRun_Click(object sender, EventArgs e)
{
try
{
tool.action();
txtCount.Text = tool.number.ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
Step 9 运行
a.运行前先进行以下设置,否则可能报”试图加载不正确格式“的错误;
b.运行结果如下图所示;