焊点检测的难点主要在于图像上粘连的焊点怎么处理,例如下图中右上边的两个焊点就粘连在一起。

这个问题比较难以处理,下面的例子给出的基本策略是利用腐蚀来进行处理。
原图,待检测焊点

处理后的图像,可以看到9个焊点都检测到了。

源程序:
//焊点检测
var winHandle = win.HalconWindow;
var Image1 = halcon.read_image("C:/Users/Administrator/Desktop/netMarketing视觉测试/img/20181023 134144.tif");
var res1 = halcon.get_image_size(Image1);
halcon.set_part(winHandle, 0, 0, res1.height, res1.width);
halcon.dev_display(Image1, winHandle);
var Rectangle1 = halcon.gen_rectangle1(156, 607, 405, 826);
var ImageReduced = halcon.reduce_domain(Image1, Rectangle1);
var Region1 = halcon.threshold(ImageReduced, 0, 180);
var ConnectedRegions = halcon.connection(Region1);
var SelectedRegions= halcon.select_shape(ConnectedRegions, vFunBaseMethod.selectShapeFeatureEnum.area,
vFunBaseMethod.selectShapeOperationEnum.and, 150, 999999);
var RegionDilation1=halcon.dilation_circle(SelectedRegions, 10);
RegionDilation1=halcon.fill_up(RegionDilation1);
var RegionUnion=halcon.union1(RegionDilation1);
var ConnectedRegions1=halcon.connection(RegionUnion);
//下面的循环是想用越来越小的area,来不断腐蚀焊点以去掉粘连
var Number1=halcon.count_obj(ConnectedRegions1);
var NewObject=halcon.gen_empty_obj();
for (int i = 1; i <= Number1; i++)
{
var ObjectSelected= halcon.select_obj(ConnectedRegions1,i);
(HTuple Area, HTuple Row, HTuple Column) =halcon.area_center(ObjectSelected);
if (6000<Area)
ObjectSelected=halcon.erosion_circle(ObjectSelected, 12);
NewObject=halcon.union2(NewObject, ObjectSelected);
}
NewObject=halcon.connection(NewObject);
Number1 =halcon.count_obj(NewObject);
var NewObject1=halcon.gen_empty_obj();
for (int i = 1; i <= Number1; i++)
{
var ObjectSelected=halcon.select_obj(NewObject, i);
(HTuple Area, HTuple Row, HTuple Column) =halcon.area_center(ObjectSelected);
if (5000 < Area)
ObjectSelected=halcon.erosion_circle(ObjectSelected, 10);
NewObject1=halcon.union2(NewObject1, ObjectSelected);
}
NewObject1=halcon.connection(NewObject1);
Number1 = halcon.count_obj(NewObject1);
var NewObject2=halcon.gen_empty_obj();
for (int i = 1; i <= Number1; i++)
{
var ObjectSelected=halcon.select_obj(NewObject1, i);
(HTuple Area, HTuple Row, HTuple Column) =halcon.area_center(ObjectSelected);
if (4000 < Area)
ObjectSelected=halcon.erosion_circle(ObjectSelected, 8);
NewObject2=halcon.union2(NewObject2, ObjectSelected);
}
NewObject2=halcon.connection(NewObject2);
Number1 = halcon.count_obj(NewObject2);
var NewObject3=halcon.gen_empty_obj();
for (int i = 1; i <= Number1; i++)
{
var ObjectSelected=halcon.select_obj(NewObject2, i);
(HTuple Area, HTuple Row, HTuple Column) =halcon.area_center(ObjectSelected);
if (3000 < Area)
{
ObjectSelected=halcon.erosion_circle(ObjectSelected, 5);
}
NewObject3=halcon.union2(NewObject3, ObjectSelected);
}
NewObject3=halcon.connection(NewObject3);
Number1 = halcon.count_obj(NewObject3);
var NewObject4= halcon.gen_empty_obj();
for (int i = 1; i <= Number1; i++)
{
var ObjectSelected= halcon.select_obj(NewObject3, i);
(HTuple Area, HTuple Row, HTuple Column)= halcon.area_center(ObjectSelected);
if (2000 < Area)
ObjectSelected=halcon.erosion_circle(ObjectSelected, 3);
NewObject4= halcon.union2(NewObject4, ObjectSelected);
}
NewObject4 = halcon.union2(NewObject4, NewObject4);
NewObject4= halcon.connection(NewObject4);
halcon.dispObj2(winHandle, NewObject4,2);
var Count=halcon.count_obj(NewObject3 );
//注意HObject要自己手动释放资源!
NewObject.Dispose(); NewObject1.Dispose(); NewObject2.Dispose(); NewObject3.Dispose(); NewObject4.Dispose();
Image1.Dispose();基于netMarketing的视觉例程的说明:
netMarketing是一个综合类库,其视觉部分的功能基于halcon,作者开发时的halcon版本为10
要运行上面的程序,建议使用C#7.0,即VS2017的C#,其新增加的Tuple语法糖可以简化调用类似halcon这种函数变态多的参数的情况,它会节省你大量编码时间。
如果下载使用netMarketing? 请查阅这个链接: https://www.skcircle.com/?id=202
netMarketing封装了自动化行业常用的视觉功能,以及包装了halcon的常用算子,以符合C#函数的调用习惯。事实上,你可以直接把halcon代码复制到C#中,简单修改一下即可使用(因为封装的函数名和halcon函数名是保持一致的!)。
有些例程使用的素材是一些实际项目中的图片,因为尺寸较大,通常放于网盘供大家下载。
有些例程及素材压缩包是有密码的,需要你打赏作者取密码。作者用所得款项维持建站费用。