halcon中的划痕检查实例surface_scratch

时间:2022-08-23 19:43:23
* surface_scratch.hdev: extraction of surface scratches via local thresholding and morphological post-processing
* 划痕检查实例


dev_close_window ()
dev_update_window ('off')
* ****
* step: 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, 12, 'Courier', 'true', 'false')
dev_set_draw ('margin')
dev_set_line_width (4)
dev_display (Image)
disp_continue_message (WindowID, 'black', 'true')
stop ()
* ****
* step: segment image
* ****

* -> using a local threshold

*平滑图片

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 (ConnectedRegions)
disp_continue_message (WindowID, 'black', 'true')
stop ()
* ****
* step: process regions

* ****

halcon中的划痕检查实例surface_scratch

* -> select large regions

*选择大点的区域

select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 10, 1000)
dev_display (Image)
dev_display (SelectedRegions)
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)
disp_continue_message (WindowID, 'black', 'true')
stop ()

* -> merge fractioned scratches via morphology

**并运算,把所有的区域合并成一个变量,这样膨胀处理更有完整性.

union1 (SelectedRegions, RegionUnion)

halcon中的划痕检查实例surface_scratch

*肿胀区域,肿胀处理,注意试验,如果不union1,处理后的结果线的完整性就不好了.

dilation_circle (RegionUnion, RegionDilation, 3.5)


dev_display (Image)
dev_display (RegionDilation)
disp_continue_message (WindowID, 'black', 'true')

stop ()

*提取骨架

skeleton (RegionDilation, Skeleton)

halcon中的划痕检查实例surface_scratch

connection (Skeleton, Errors)
dev_set_colored (12)
dev_display (Image)
dev_display (Errors)
disp_continue_message (WindowID, '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)

halcon中的划痕检查实例surface_scratch

dev_display (Image)
dev_set_color ('red')
dev_display (Scratches)
dev_set_color ('blue')
dev_display (Dots)