halcon 缺陷检测 划伤检测

halcon 缺陷检测 划伤检测_第1张图片

图片资源

链接:https://pan.baidu.com/s/1bZQAmamRi-dEIMhFmGJ38g
提取码:s7pm

处理流程

1、动态阈值处理 划伤连通域分析
2、筛选面积较大的连通域
3、合并划伤 计算划伤骨骼线
4、大小划伤分类
5、显示缺陷

完整代码

* 检测物体表面 划伤缺陷 

**** 1、读取图片并显示 ***********
dev_update_off ()
dev_close_window ()

read_image (Image, './surface_scratch.png')
get_image_size (Image, Width, Height)
dev_open_window_fit_image (Image, 0, 0, Width, Height, WindowHandle)
* 字体
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_set_draw ('margin')
dev_set_line_width (4)
dev_display (Image)

disp_continue_message (WindowHandle, 'black', 'true')
stop()

**** 2、动态阈值处理 计算 ***********
* 均值处理
mean_image (Image, ImageMean, 7, 7)
* 动态阈值
dyn_threshold (Image, ImageMean, DarkPixels, 5, 'dark')
* 连通域分析
connection (DarkPixels, ConnectedRegions)

dev_set_colored (12)
dev_display (Image)
dev_display (ConnectedRegions)

disp_continue_message (WindowHandle, 'black', 'true')
stop()

**** 3、筛选面积大的连通域 ***********
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 10, 1000)
dev_display (Image)
dev_display (SelectedRegions)

disp_continue_message (WindowHandle, 'black', 'true')
stop()

* 显示局部区域
open_zoom_window (0, round(Width / 2), 2, 303, 137, 496, 3, WindowHandleZoom)
dev_set_color ('blue')
dev_display (Image)
dev_display (SelectedRegions)

disp_continue_message (WindowHandleZoom, 'black', 'true')
stop()

**** 4、划伤合并 缺陷骨骼处理***********
* 联合连通域
union1 (SelectedRegions, RegionUnion)
* 圆形膨胀
dilation_circle (RegionUnion, RegionDilation, 3.5)
dev_display (Image)
dev_display (RegionDilation)

disp_continue_message (WindowHandleZoom, 'black', 'true')
stop()

* 细化骨骼算法
skeleton (RegionDilation, Skeleton)
* 连通域分析
connection (Skeleton, Defects)
dev_display (Image)
dev_display (Defects)

disp_continue_message (WindowHandleZoom, 'black', 'true')
stop()

**** 5、大小划伤分类***********
close_zoom_window (WindowHandleZoom, Width, Height)
select_shape (Defects, Scratches, 'area', 'and', 50, 99999)
select_shape (Defects, Dots, 'area', 'and', 0, 50)
dev_display(Image)
dev_set_color ('red')
dev_display (Scratches)
dev_set_color('blue')
dev_display (Dots)

Message := '红色:划伤缺陷'
Message[1] := '蓝色:点缺陷'
disp_message (WindowHandle, Message, 'window', 390, 450, ['red','blue'], 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop()

halcon 缺陷检测 划伤检测_第2张图片

你可能感兴趣的:(Halcon,halcon)