Bootstrap

Halcon 表面划痕检测

Halcon表面划伤检测实例


    原图:1.png



     * 关闭活动图形窗口

      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)






    ;