基于Halcon学习的缺陷检测【二】surface_scratch.hdev

该程序显示了通过局部阈值和形态学后处理提取表面划痕


总代码:

dev_update_off ()
dev_close_window ()
* 
* Step 1: Acquire image
* 
*读取图片
read_image (Image, 'surface_scratch')

*得到图片的宽高
get_image_size (Image, Width, Height)

*打开窗口
dev_open_window_fit_image (Image, 0, 0, Width, Width, WindowID)
set_display_font (WindowID, 16, 'mono', 'true', 'false')
dev_set_draw ('margin')
dev_set_line_width (4)
dev_display (Image)


Message := 'This program shows the extraction of'
Message[1] := 'surface scratches via local thresholding'
Message[2] := 'and morphological post-processing'
*显示信息--该程序显示了通过局部阈值和形态学后处理提取表面划痕
disp_message (WindowID, Message, 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowID, 'black', 'true')
stop ()
* 
* Step 2: Segment image
* Using a local threshold

*2.分割图像
*使用局部阈值
*使用中值滤波
mean_image (Image, ImageMean, 7, 7)
*使用局部阈值分割图像。
dyn_threshold (Image, ImageMean, DarkPixels, 5, 'dark')

* 
* Extract connected components
*提取连接的组件
*连通域分割
connection (DarkPixels, ConnectedRegions)
dev_set_colored (12)
dev_display (Image)
dev_display (ConnectedRegions)

Message := 'Connected components after image segmentation'
Message[1] := 'using a local threshold.'
*显示信息--“使用局部阈值”进行图像分割后连接的组件
disp_message (WindowID, Message, 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowID, 'black', 'true')
stop ()
* 
* Step 3: Process regions
* 
* Select large regions
*步骤3:处理区域
*选择大区域

*像素面积大于10的区域
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 10, 1000)
dev_display (Image)
dev_display (SelectedRegions)

*显示信息--大区域
disp_message (WindowID, 'Large Regions', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowID, 'black', 'true')
stop ()
* 
* Visualize fractioned scratch
*想象破裂的划痕
*打开窗口并设置大小
open_zoom_window (0, round(Width / 2), 2, 303, 137, 496, 3, WindowHandleZoom)
dev_set_color ('blue')
dev_display (Image)
dev_display (SelectedRegions)
set_display_font (WindowHandleZoom, 16, 'mono', 'true', 'false')

*显示信息-裂开的划痕
disp_message (WindowHandleZoom, 'Fractioned scratches', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandleZoom, 'black', 'true')
stop ()
* 
* Merge fractioned scratches via morphology
*通过形态学合并断裂划痕
*联合区域
union1 (SelectedRegions, RegionUnion)
*膨胀
dilation_circle (RegionUnion, RegionDilation, 3.5)
dev_display (Image)
dev_display (RegionDilation)
Message := 'Region of the scratches after dilation'
*显示信息--扩张后的划痕区域
disp_message (WindowHandleZoom, Message, 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandleZoom, 'black', 'true')
stop ()
*计算区域的骨架。
skeleton (RegionDilation, Skeleton)
*连通域分割
connection (Skeleton, Errors)
dev_set_colored (12)
dev_display (Image)
dev_display (Errors)
Message := 'Fractioned scratches merged via morphology'
*显示信息--分形划痕通过形态学合并
disp_message (WindowHandleZoom, Message, 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandleZoom, 'black', 'true')
stop ()
* 
* Distinguish small and large scratches
*区分大小划痕
*关闭窗口
close_zoom_window (WindowHandleZoom, Width, Height)
*挑选出大的划痕
select_shape (Errors, Scratches, 'area', 'and', 50, 10000)
*挑选出小的划痕

select_shape (Errors, Dots, 'area', 'and', 1, 50)
dev_display (Image)
dev_set_color ('red')
dev_display (Scratches)
dev_set_color ('blue')
dev_display (Dots)
Message := 'Extracted surface scratches'
Message[1] := 'Not categorized as scratches'
*显示信息--提取的表面划痕不属于“划痕”
disp_message (WindowID, Message, 'window', 440, 310, ['red','blue'], 'true')

逐段分析:


dev_update_off ()


*读取图片
read_image (Image, 'surface_scratch')

*得到图片的宽高
get_image_size (Image, Width, Height)

*打开窗口
dev_open_window_fit_image (Image, 0, 0, Width, Width, WindowID)
set_display_font (WindowID, 16, 'mono', 'true', 'false')
dev_set_draw ('margin')
dev_set_line_width (4)
dev_display (Image)


Message := 'This program shows the extraction of'
Message[1] := 'surface scratches via local thresholding'
Message[2] := 'and morphological post-processing'

*显示信息--该程序显示了通过局部阈值和形态学后处理提取表面划痕
disp_message (WindowID, Message, 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowID, '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)

Message := 'Connected components after image segmentation'
Message[1] := 'using a local threshold.'

*显示信息--“使用局部阈值”进行图像分割后连接的组件
disp_message (WindowID, Message, 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowID, 'black', 'true')
stop ()

*步骤3:处理区域
*选择大区域

*像素面积大于10的区域
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 10, 1000)
dev_display (Image)
dev_display (SelectedRegions)

*显示信息--大区域
disp_message (WindowID, 'Large Regions', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowID, '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)
set_display_font (WindowHandleZoom, 16, 'mono', 'true', 'false')

*显示信息-裂开的划痕
disp_message (WindowHandleZoom, 'Fractioned scratches', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandleZoom, 'black', 'true')
stop ()

*通过形态学合并断裂划痕
*联合区域
union1 (SelectedRegions, RegionUnion)

*膨胀
dilation_circle (RegionUnion, RegionDilation, 3.5)
dev_display (Image)
dev_display (RegionDilation)
Message := 'Region of the scratches after dilation'

*显示信息--扩张后的划痕区域
disp_message (WindowHandleZoom, Message, 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandleZoom, 'black', 'true')
stop ()

*计算区域的骨架。
skeleton (RegionDilation, Skeleton)

*连通域分割
connection (Skeleton, Errors)
dev_set_colored (12)
dev_display (Image)
dev_display (Errors)
Message := 'Fractioned scratches merged via morphology'

*显示信息--分形划痕通过形态学合并
disp_message (WindowHandleZoom, Message, 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandleZoom, 'black', 'true')
stop ()

*区分大小划痕
*关闭窗口
close_zoom_window (WindowHandleZoom, Width, Height)
*挑选出大的划痕
select_shape (Errors, Scratches, 'area', 'and', 50, 10000)
*挑选出小的划痕

select_shape (Errors, Dots, 'area', 'and', 1, 50)
dev_display (Image)
dev_set_color ('red')
dev_display (Scratches)
dev_set_color ('blue')
dev_display (Dots)
Message := 'Extracted surface scratches'
Message[1] := 'Not categorized as scratches'
*显示信息--提取的表面划痕不属于“划痕”
disp_message (WindowID, Message, 'window', 440, 310, ['red','blue'], 'true')

 

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值