• Halcon学习笔记之缺陷检测(一)


    例程:surface_scratch.hdev

    说明:这个程序利用局部阈值和形态学处理提取表面划痕

    代码中绿色部分为个人理解和注释,其余为例程中原有代码

    复制代码
    *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’)  //读入图片名为’surface_scratch’的图片
    get_image_size(Image,Width,Height)    //获取图像的尺寸:宽、高
    //在坐标为(0,0)处打开一个图像窗口。其宽和高分别是Width和Width,窗口的句柄为WindowID
    dev_open_window_fit_image(Image,0,0,Width,Width,WindowID)
    //设置窗口中显示的字体颜色,大小,加粗,倾斜
    set_display_font(WindowID,12,’Courier’,’true’,’false’)                        
    dev_set_draw(‘margin’)              //设置区域填充类型:’margin’ or ‘fill’
    dev_set_line_width(4)                //设置线宽,这个在填充类型为fill时效果不明显
    
    dev_display(Image)                   //显示图片
    disp_continue_message(WindowID,’black’,’true’)    //显示继续消息,等待用户按F5继续执行
    stop()
    
    *****
    *step: segment image
    *****
    *-> using a local threshold
    mean_image(Image,ImageMean,7,7)                      //用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
    *****
    *->select large regions
    select_shape(ConnectedRegions,SelsectedRegions,’area’,’and’,10,1000)    //区域选择,用面积的形态特征选择面积在10到10000间的区域
    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_set_colored类似
    dev_display(Image)                //显示原图
    dev_display(SelectedRegions)      //显示选中的区域图
    disp_continue_message(WindowID,’black’,’true’)    //显示继续提示信息
    stop()
    
    *->merge fractioned scratches via morphology
    union1(SelectedRegions,RegionUnion)             //合并所有区域
    dilation_circle(RegionUnion,RegionDilation,3.5) //用半径3.5 的掩膜进行膨胀
    dev_display(Image)                              //显示原图
    dev_display(RegionDilation)                     //显示膨胀后的区域图
    disp_continue_message(WindowID,’black’,’true’)//显示继续提示信息
    stop()
    skeleton(RegionDialtion,Skeleton)      //计算区域的中轴,见例程:surface_scratch.hdev
    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)//选择区域面积在50到10000的作为划痕
    select_shape(Errors,Dots,’area’,’and’,1,50)         //选择区域面积在1到50 的作为点
    dev_display(Image)                    //显示原图
    dev_set_color(‘red’)                 //设置区域显示颜色为红色
    dev_display(Scratches)                //显示条状划痕(显示为红色)
    dev_set_color(‘blue’)               //设置区域显示颜色蓝色
    dev_display(Dots)                     //显示点状划痕(显示为蓝色)
    复制代码
     
     
  • 相关阅读:
    Props属性
    逆卷积:convtranspose2d(fractionally-strided convolutions)
    nn.ReflectionPad2d(镜像填充)
    conv1*1的作用
    如何将jupyter中的.ipynb文件转换成python中的.py文件
    低光图像增强学习
    pytorch 中的variable函数
    BCELoss和BCEWithLogitsLoss
    正则化的理解
    网络压缩方法总结
  • 原文地址:https://www.cnblogs.com/shuimuqingyang/p/10837185.html
Copyright © 2020-2023  润新知