halcon例程解析:在不同照明方向拍摄的图像中检测缺失焊料——board.hdev
为检测缺失焊料,分别采集四个方向打光的图像,利用这四张不同光照的图像来检测焊料的缺失
1. 效果展示
利用如下四张图像,检测是否焊料缺失
原图
结果
2. 思路分析
方案依据:
需要焊料部分较暗
有焊料部分,经过打光后边缘反光、较亮
分割出要焊料区域,分割出有焊料区域,作差即为缺焊区域
思路如下:
- 分割要焊料区域
- 对每一张图进行阈值分割,取较暗区域
- 取并集得到
- 分割有焊料区域
- 对每一张图进行阈值分割,取较亮区域
- 取并集得到粗略边缘区域
- 与要焊料区域取交集,得到边缘区域
- 取要焊料区域的补集作为背景区域
- 依靠背景区域做抑制,填充边缘区域得到有焊料区域
- 要焊料区域与有焊料区域作差
- 后处理
2.1 分割要焊料区域
threshold (Images, Darks, 0, 40)
union1 (Darks, Dark)
fill_up (Dark, DarkFilled)
- 对每一张图进行阈值分割,取较暗区域
- 取并集得到
2.2 分割有焊料区域
threshold (Images, Lights, 100, 255)
union1 (Lights, Light)
intersection (DarkFilled, Light, Intersection)
- 对每一张图进行阈值分割,取较亮区域
- 取并集得到粗略边缘区域
- 与要焊料区域取交集,得到边缘区域
complement (DarkFilled, Back)
expand_region (Intersection, Back, RegionExpanded, 10, 'image')
fill_up (RegionExpanded, Good)
- 取要焊料区域的补集作为背景区域
- 依靠背景区域做抑制,填充边缘区域得到有焊料区域
2.3 要焊料区域与有焊料区域作差
difference (DarkFilled, Good, Rest)
expand_gray (Rest, Ic, Good, Bad, 6, 'image', 5)
3 完整代码
* board.hdev: Detection of missing solder
*
get_system ('clip_region', Information)
set_system ('clip_region', 'true')
dev_update_window ('off')
dev_close_window ()
dev_open_window (0, 0, 512, 512, 'black', WindowID)
read_image (Images, ['ic0','ic1','ic2','ic3'])
channels_to_image (Images, Ic)
select_obj (Images, Input1, 1)
dev_display (Input1)
set_display_font (WindowID, 14, 'mono', 'false', 'false')
disp_continue_message (WindowID, 'black', 'true')
stop ()
select_obj (Images, Input2, 2)
dev_display (Input2)
disp_continue_message (WindowID, 'black', 'true')
stop ()
select_obj (Images, Input3, 3)
dev_display (Input3)
disp_continue_message (WindowID, 'black', 'true')
stop ()
select_obj (Images, Input4, 4)
dev_display (Input4)
disp_continue_message (WindowID, 'black', 'true')
stop ()
mean_n (Ic, ImageMean)
dev_display (ImageMean)
threshold (Images, Darks, 0, 40)
union1 (Darks, Dark)
fill_up (Dark, DarkFilled)
dev_set_color ('green')
dev_display (DarkFilled)
disp_continue_message (WindowID, 'black', 'true')
stop ()
threshold (Images, Lights, 100, 255)
union1 (Lights, Light)
intersection (DarkFilled, Light, Intersection)
dev_display (ImageMean)
dev_display (Intersection)
disp_continue_message (WindowID, 'black', 'true')
stop ()
complement (DarkFilled, Back)
dev_set_color ('red')
dev_display (Back)
disp_continue_message (WindowID, 'black', 'true')
stop ()
expand_region (Intersection, Back, RegionExpanded, 10, 'image')
fill_up (RegionExpanded, Good)
dev_display (ImageMean)
dev_set_color ('green')
dev_display (Good)
disp_continue_message (WindowID, 'black', 'true')
stop ()
difference (DarkFilled, Good, Rest)
dev_set_color ('red')
dev_display (Rest)
disp_continue_message (WindowID, 'black', 'true')
stop ()
expand_gray (Rest, Ic, Good, Bad, 6, 'image', 5)
dev_display (Bad)
disp_continue_message (WindowID, 'black', 'true')
stop ()
connection (Bad, ConnectedBad)
select_shape (ConnectedBad, BigBad, 'area', 'and', 150, 99999)
area_center (BigBad, AreaBad, Row, Column)
count_obj (BigBad, NumMissingSolder)
AreaMissingSolder := sum(AreaBad)
dev_display (Images)
dev_set_color ('green')
dev_set_draw ('margin')
dev_set_line_width (3)
dev_display (Good)
dev_set_color ('red')
dev_display (BigBad)
dev_set_draw ('fill')
dev_set_line_width (1)
dev_update_window ('on')
set_system ('clip_region', Information)