说明:这个程序利用局部阈值和形态学处理提取表面划痕
代码中绿色部分为个人理解和注释,其余为例程中原有代码
*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,窗口的句柄为WindowIDdev_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 thresholdmean_image(Image,ImageMean,7,7) //用7×7的窗口对图像进行均值滤波dyn_threshold(Image,ImageMean,DarkPixels,5,’dark’) //利用本地阈值进行图像分割*->extract connected componentsconnection(DarkPixels,ConnectedRegions) //对区域进行连通运算dev_set_colored(12) //设置区域显示的颜色数目dev_display(ConnectedRegions) //显示图像disp_continue_message(WindowID,’black’,’true’)//显示继续提示,提示继续stop()******step: process regions******->select large regionsselect_shape(ConnectedRegions,SelsectedRegions,’area’,’and’,10,1000) //区域选择,用面积的形态特征选择面积在10到10000间的区域dev_display(Image) //显示原图dev_display(SelectedRegions) //显示选择的区域disp_continue_message(WindowID,’black’,’true’) //显示继续提示,提示继续stop()*->visualize fractioned scratchopen_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 morphologyunion1(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.hdevconnection(Skeleton,Errors) //连通操作,以便后面进行区域选择dev_set_colored(12) //设置颜色dev_display(Image) //显示原图dev_display(Errors) //显示所有抓痕disp_continue_message(WindowID,’black’,’true’) //显示继续提示信息stop()*->distinguish small and large scratchesclose_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) //显示点状划痕(显示为蓝色)
联系客服