Halcon学习日志——示例程序表面检测surface_scratch.hdev(通过局部阈值和形态学检测表面划痕)

* This programm shows the extraction of surface scratches via
* local thresholding and morphological post-processing
* 
dev_update_off ()*将dev_update_pc,dev_update_var,dev_update窗口切换到“关闭”。
dev_close_window ()*关闭激活的图形显示窗口
* 
* Step 1: Acquire image获取图像
* 
read_image (Image, 'surface_scratch')
*读取有不同文件格式的图像read_image( : Image : FileName : )
get_image_size (Image, Width, Height)
*返回图像大小
dev_open_window_fit_image (Image, 0, 0, Width, Width, WindowID)
*dev_open_window_fit_image(Image : : Row, Column, WidthLimit, HeightLimit : WindowHandle)
*在给定的最小和最大范围内打开一个新的图形窗口,该窗口保留给定图像的长宽比。新图形窗口的位置由参数*ROW,Column给出,定义图形窗口左上角的位置。
set_display_font (WindowID, 16, 'mono', 'true', 'false')
*set_display_font( : : WindowHandle, Size, Font, Bold, Slant : )
*设置当前窗口的字体属性,Size设置字体大小。
*Font设置字体名称字体名称。可以使用“mono”、“sans”、“serif”或特定的字体名称。“mono”将在Windows上映射为“Courier New”,在Linux上映射为“Courier”,在OS X上映射为“Menlo”。SAN将在Windows和OS X上映射为“Arial”,在Linux上映射为“helvetica”。serif将在Windows和OS X上映射为“Times New Roman”,在Linux上映射为“Times”。
Bold设置字体粗体,如果设置为“真”,则使用粗体字体。
Slant设置字体斜体,如果设置为“true”,则使用倾斜字体。
dev_set_draw ('margin')
*dev_set_draw( : : DrawMode : )
*定义区域填充,区域输出的填充模式。默认值:“填充”,值列表:“填充”、“边距”。
dev_set_line_width (4)
*dev_set_line_width( : : LineWidth : )
*轮廓模式下区域输出的线宽。默认值:1。
dev_display (Image)
*dev_display(Object : : : )
*要显示的图像对象。
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_message( : : WindowHandle, String, CoordSystem, Row, Column, Color, Box : )
*String包含要显示的文本消息的字符串元组。元组的每个值将显示在一行中。
*Coordsystem如果设置为“窗口”,则文字位置相对于窗口坐标系给出。如果设置为“图像”,则使用图像坐标(这在缩放图像中可能有用)。默认值:“窗口”。值的列表: “窗口”,“图像”.
*Color将文本的颜色定义为字符串。如果设置为[]或“”,则使用当前设置的颜色。如果传递一个字符串元组,则颜色将循环用于每个新位置或文本行。默认值:“黑色”。
*Box如果设置为“true”,文本将写入橙色框中。如果设置为“false”,则不显示任何框。如果设置为颜色字符串(例如“白色”、“FF00CC”等),文本将写入该颜色的框中。(可选)第二个值控制长方体阴影的外观。
默认值:“true”
disp_continue_message (WindowID, 'black', 'true')
*disp_continue_message( : : WindowHandle, Color, Box : )
*此过程在屏幕右下角以给定颜色显示“单击“运行”以继续”。
如果参数框设置为“true”,则文本将写入白色框中,这在不规则背景下非常有用。
stop ()
*停止连续程序执行
* 
* Step 2: Segment image*分段图像
* 
* Using a local threshold*使用局部阈值
mean_image (Image, ImageMean, 7, 7)
*mean_image(Image : ImageMean : MaskWidth, MaskHeight : )通过均值平滑一个图像
*ImageMean输出参数
*MaskWidth输入平滑蒙板宽度
*MaskHeight输入平滑蒙板高度
dyn_threshold (Image, ImageMean, DarkPixels, 5, 'dark')
*dyn_threshold(OrigImage, ThresholdImage : RegionDynThresh : Offset, LightDark : )利用局域阈值分割图像
*OrigImage输入原始图像
*ThresholdImage输入阈值图像
*RegionDynThresh输出分割后区域
*Offset灰度值偏移量,默认值5
*LightDark提取区域类型,提取光、暗或类似区域,默认值:“亮”。值列表:“暗”、“相等”、“亮”、“不相等”
* Extract connected components 提取链接的组件
connection (DarkPixels, ConnectedRegions)
*connection(Region : ConnectedRegions : : )
*Region输入区域
*ConnectedRegions连接组件
dev_set_colored (12)
*dev_set_colored( : : NumColors : )输出颜色的数量。默认值:6。值列表3,6,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选择大的区域
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 10, 1000)
*select_shape(Regions : SelectedRegions : Features, Operation, Min, Max : )
*Regions输入需要检查的区域
*SelectedRegions输出满足条件的区域
*Features输入需要检查的形状特征。默认值:“area”
*Operation输入单个特征的链接类型。默认值:”和“。值列表:'and', 'or'
*Min输入特征最小值
*Max输入特征最大值
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)
*union1(Region : RegionUnion : : )
*Region输入需要计算并集的区域
*RegionUnion输出所有输入区域的并集。元素个数:RegionUnion <= Region。
dilation_circle (RegionUnion, RegionDilation, 3.5)
*使用圆形结构元素扩展一个区域
*dilation_circle(Region : RegionDilation : Radius : )
*Region输入需要扩展的区域
*RegionDilation输出扩张的区域
*Radius输入圆形结构半径    
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)
*计算区域的中轴    
*skeleton(Region : Skeleton : : )
*Region输入要变薄的区域
*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')

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值