本文要完成的任务是将下图中焊点提取出来,对应于halcon例程Blob分析中的ball.hdev。
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
read_image (Image, 'die_03.png')
disp_continue_message (WindowHandle, 'black', 'true')
stop()
*形状转换,最小外接矩形并填充
threshold (Image, Regions, 91, 242)
shape_trans (Regions, RegionTrans, 'rectangle2')
*抠图
reduce_domain (Image, RegionTrans, ImageReduced)
threshold (ImageReduced, Regions1, 0, 50)
*区域填充,fill_up是将所有的区域填充,fill_up_shape是将面积在1到100的填充
fill_up_shape (Regions1, RegionFillUp, 'area', 1, 100)
disp_continue_message (WindowHandle, 'black', 'true')
stop()
*用一个圆形的结构元素做开运算
opening_circle (RegionFillUp, RegionOpening, 13.5)
*打断区域做特征选择(这里用圆度特征)
connection (RegionOpening, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'roundness', 'and', 0.88349, 1)
*排序
sort_region (SelectedRegions, SortedRegions, 'first_point', 'true', 'column')
*计算连通域的个数
count_obj (SortedRegions, Number)
disp_continue_message (WindowHandle, 'black', 'true')
stop()
*计算最小外接圆并画出
dev_display (Image)
smallest_circle (SortedRegions, Row, Column, Radius)
disp_circle (WindowHandle, Row, Column, Radius)
Diameter:= 2*Radius
*显示直径
for i:=0 to |Radius|-1 by 1
if(fmod(i,2)==1)
disp_message (WindowHandle, 'D: ' + Diameter[i], 'image', Row[i]-Diameter[i] , max([Column[i] - 60,0]), 'white', 'false')
else
disp_message (WindowHandle, 'D: ' + Diameter[i], 'image', Row[i]+Diameter[i], max([Column[i] - 60,0]), 'white', 'false')
endif
endfor
运行结果如下:
请关注我,让我们一起开创视觉的领域!!!!