Halcon表面划伤检测实例
分类: Halcon 机器视觉 2013-05-04 10:36 216人阅读 评论(0) 收藏 举报
* 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消息处理
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