* This programm shows the extraction of surface scratches via
  * local thresholding(局部阈值) and morphological post-processing(形态后处理)
  *常规初始化
  dev_update_off ()
  dev_close_window ()
  *
  * Step 1: Acquire image
  *
  * read_image (Image, 'surface_scratch')
  *Good
  * read_image (Image, 'C:/Users/Administrator/Desktop/漏检缺陷图片/7_[0]_ORIGIN_201310 4 112551.jpg')
  *图像噪点多,可通过选择像素大小筛选
  * read_image (Image, 'C:/Users/Administrator/Desktop/漏检缺陷图片/13.10.4第二个算法下-较暗(可变框)/边缘漏涂/187_[110]_INIT_ORIGIN_201310 4 1129 5.jpg')
  * read_image (Image, 'C:/Users/Administrator/Desktop/漏检缺陷图片/13.10.4第二个算法下-较暗(可变框)/不明显/9_[8]_INIT_ORIGIN_201310 4 112554.jpg')
  * Image Acquisition 01: Code generated by Image Acquisition 01
  *批量处理图片
  ImageFiles := []
  ImageFiles[0] := 'C:/Users/Administrator/Desktop/漏检缺陷图片/1 (2).jpg'
  ImageFiles[1] := 'C:/Users/Administrator/Desktop/漏检缺陷图片/1 (3).jpg'
  ImageFiles[2] := 'C:/Users/Administrator/Desktop/漏检缺陷图片/1.jpg'
  ImageFiles[3] := 'C:/Users/Administrator/Desktop/漏检缺陷图片/2 (2).jpg'
  for Index := 0 to |ImageFiles| - 1 by 1
  * stop ()
  read_image (Image, ImageFiles[Index])
  * Image Acquisition 01: Do something
  get_image_size (Image, Width, Height)
  *设置打开窗口大小、字体
  dev_open_window_fit_image (Image, 0, 0, Width, Width, WindowID)
  set_display_font (WindowID, 16, 'mono', 'true', 'false')
  *设置标记的填充模式和线宽
  dev_set_draw ('margin')
  dev_set_line_width (4)
  dev_display (Image)
  *显示悬浮文字提示
  *赋值符号:=
  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')
  *在右下角显示“Press Run to continue"
  disp_continue_message (WindowID, 'black', 'true')
  * stop ()
  *
  * Step 2: Segment image
  *
  * Using a local threshold
  *通过实验对比,对Image进行15*15均值滤波效果较好
  mean_image (Image, ImageMean, 15, 15)
  *比较均值滤波图像ImageMean和原始图像Image,返回滤波图像灰度值阈值差大于4的区域
  dyn_threshold (Image, ImageMean, DifPixels, 4, 'not_equal')
  * dyn_threshold (Image, ImageMean, LightPixels, 5, 'light')
  *合并明暗区域
  * union2 (DarkPixels, LightPixels, RegionUnion1)
  * Extract connected components
  connection (DifPixels, ConnectedRegions)
  *设置混合输出颜色为12种
  dev_set_colored (3)
  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
  *从ConnectedRegions中得到面积大于5小于1000的区域到SelectedRegions.可通过改变值调整筛选精度,滤除噪点
  select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 5, 99999)
  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
  *打开局部显示窗口,RowROI1、RowROI1、RowROI2、ColROI2分别是要显示区域的坐标。可按F7单步跳入函数查看。
  *取整运算:round(width/2)
  * open_zoom_window (0, 0, 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
  *合并SelectedRegions的并集到RegionUnion
  union1 (SelectedRegions, RegionUnion)
  *以3.5作为圆形区域扩张的半径,对RegionUnion扩张得到RegionDilation
  dilation_circle (RegionUnion, RegionDilation, 3.5)
  dev_display (Image)
  dev_display (RegionDilation)
  Message := 'Region of the scratches after dilation'
  disp_message (WindowID, Message, 'window', 12, 12, 'black', 'true')
  disp_continue_message (WindowID, 'black', 'true')
  * stop ()
  *由RegionDilation获取骨架给Skeleton
  skeleton (RegionDilation, Skeleton)
  connection (Skeleton, Errors)
  dev_set_colored (12)
  dev_display (Image)
  dev_display (Errors)
  Message := 'Fractioned scratches merged via morphology'
  disp_message (WindowID, Message, 'window', 12, 12, 'black', 'true')
  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)
  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, 'image', 440, 310, ['red','blue'], 'true')
  *保存窗口显示内容
  dump_window_image (Image, WindowID)
  *自动按序号保存图像
  write_image (Image, 'jpeg 60', 0, ('C:/Users/Administrator/Desktop/缺陷标记/Defect'+Index)+'.jpg')
  *关闭窗口显示,减少内存占用
  dev_close_window ()
  stop()
  endfor