Halcon表面划伤检测实例

本文介绍了一种通过局部阈值法和形态学后处理来提取表面划痕的方法。包括图像获取、分割、连通区域提取及后续处理等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

* surface_scratch.hdev: extraction of surface scratches via local thresholding and morphological post-processing

原图为:


 *关闭活动图形窗口
 dev_close_window ()
* 在程序执行中指定输出行为为 off。
 dev_update_window ('off')
 * ****
* step: acquire image 步骤:获取图像
 * ****读入文件名为 'surface_scratch' 的图像到Image
 read_image (Image, 'surface_scratch')
 get_image_size (Image, Width, Height)
*打开一个和Image宽高比一致的图像窗口
 dev_open_window_fit_image (Image, 0, 0, Width, Width, WindowID)
*设置 窗口字体大小为 12,字体类型为Courier,粗体不倾斜字体。
 set_display_font (WindowID, 12, 'Courier', 'true', 'false')

*设置填充模式为'margin'
 dev_set_draw ('margin')
*定义输出轮廓线宽为4
dev_set_line_width (4)
*显示Image到窗口
 dev_display (Image)
*WindowID窗口使用黑色字体在一个方框内显示按"F5"继续运行 字体,并注册F5消息处理
 disp_continue_message (WindowID, 'black', 'true')
 stop ()
 * ****
 * step: segment image 步骤:图像分割
 * ****
 * -> using a local threshold 使用局部阈值
* 对Image进行7*7均值滤波
 mean_image (Image, ImageMean, 7, 7)

*********************************************************************

*得到的图像为:

 

*

*

*
*用均值滤波图像作为二值化阈值图像,返回小于灰度值小于该点阈值-5的图像。
 dyn_threshold (Image, ImageMean, DarkPixels, 5, 'dark')

***************************************

****得到的区域为:


 * -> extract connected components 提取连通分量

*由分割出来的DarkPixels获得连通区域到ConnectedRegions

*******得到的连通区域为:


 connection (DarkPixels, ConnectedRegions)
*设置混合输出颜色为12种
 dev_set_colored (12)

*显示当前图形窗口中的图像对象。
dev_display (ConnectedRegions)
*
 disp_continue_message (WindowID, 'black', 'true')
 stop ()
 * ****
 * step: process regions 处理区域
 * ****
 * -> select large regions 选取大区域
*从ConnectedRegions中得到面积大于10小于1000的区域到SelectedRegions
 select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 10, 1000)

*设置当期图像窗口的图像对象为Image
 dev_display (Image)
* 设置当期图像窗口的图像对象为SelectedRegions
 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 通过形态学合并划痕

*合并SelectedRegions的并集到RegionUnion
 union1 (SelectedRegions, RegionUnion)

*以3.5作为圆形区域扩张的半径,对RegionUnion扩张得到RegionDilation
 dilation_circle (RegionUnion, RegionDilation, 3.5)


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

*由RegionDilation获取骨架给Skeleton
 skeleton (RegionDilation, Skeleton)

*通过8邻接或四邻接方法将骨架链接后传给Errors
 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)
*选择面积大于50小于1000的区域到Scratches


 select_shape (Errors, Scratches, 'area', 'and', 50, 10000)
*选择面积大于1小于50的区域到Dots
 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)

 

 

原文出处:http://www.halconbbs.com/read.php?tid-25-fpage-3.html

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值