打开halcon,按下ctrl+e打开halcon自带例程。工业领域->橡胶,人造材料,金属薄片->fin.hdev
* fin.hdev: Detection of a fin
*
dev_update_window ('off')
read_image (Fins, 'fin' + [1:3])
get_image_size (Fins, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width[0], Height[0], 'black', WindowID)
set_display_font (WindowID, 14, 'mono', 'true', 'false')
for I := 1 to 3 by 1
select_obj (Fins, Fin, I)
dev_display (Fin)
*二值化处理,选中“亮”的那部分区域
binary_threshold (Fin, Background, 'max_separability', 'light', UsedThreshold)
dev_set_color ('blue')
dev_set_draw ('margin')
dev_set_line_width (4)
dev_display (Background)
disp_continue_message (WindowID, 'black', 'true')
stop ()
*闭运算,为什么闭运算能把那个“翼”形去掉,闭运算是先膨胀,后腐蚀。所谓膨胀就是把黑色边缘部分用白色替换,
*当这个用作运算的卷积核足够大,白色就把那个黑色的区域整个覆盖掉了
*腐蚀是为了把原来的边缘还原
closing_circle (Background, ClosedBackground, 250)
dev_set_color ('green')
dev_display (ClosedBackground)
disp_continue_message (WindowID, 'black', 'true')
stop ()
*比较两幅图像,选中两张图像中不相同的区域
difference (ClosedBackground, Background, RegionDifference)
*开运算
opening_rectangle1 (RegionDifference, FinRegion, 5, 5)
dev_display (Fin)
dev_set_color ('red')
dev_display (FinRegion)
*获取区域中心位置坐标
area_center (FinRegion, FinArea, Row, Column)
if (I < 3)
disp_continue_message (WindowID, 'black', 'true')
stop ()
endif
endfor
毛刺图片
剔除部分