链接:https://pan.baidu.com/s/15zKJ3Ea_EZJRbYE72V5pgw
提取码:pzg0
* 1.读取并显示图片 *************************************
dev_close_window ()
read_image (Image, './img.png')
get_image_size (Image, Width, Height)
dev_open_window_fit_size (0, 0, Width, Height, -1, -1, WindowHandle)
dev_display (Image)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_set_draw ('margin')
dev_set_line_width (3)
rgb1_to_gray (Image, GrayImage)
* 图像增强
emphasize (GrayImage, ImageEmphasize, Width, Height, 5)
mult_image (ImageEmphasize, ImageEmphasize, ImageResult, 1, 0)
* 2.获取处理区域 *************************************
* 二值化分析
threshold (ImageResult, Region, 200, 255)
* 形态学 膨胀
dilation_rectangle1 (Region, RegionDilation, 3, 3)
* 连通域分析与筛选
connection (RegionDilation, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 150, 999999999)
* 形态学 膨胀
dilation_rectangle1 (SelectedRegions, RegionDilation1, 2, 2)
* 孔洞填充
fill_up (RegionDilation1, RegionFillUp)
* 获取全图区域
get_domain (GrayImage, Domain)
* 获取三条黑色 边缘区域
difference (Domain, RegionFillUp, RegionDifference)
* 连通域分析
connection (RegionDifference, ConnectedRegions1)
* 形状变换 带角度矩形
shape_trans (ConnectedRegions1, RegionTrans, 'rectangle2')
* 去除边缘区域
difference (Domain, RegionTrans, RegionDifference1)
* 连通域分析
connection (RegionDifference1, ConnectedRegions2)
* 连通域排序(水平排序)
sort_region (ConnectedRegions2, SortedRegions, 'first_point', 'true', 'column')
* 获取第2个区域与第三个区域(处理区域)
select_obj (SortedRegions, ObjectSelected2, 2)
select_obj (SortedRegions, ObjectSelected3, 3)
* 合并排序为 2、3的区域
union2 (ObjectSelected2, ObjectSelected3, RegionUnion)
stop()
* 3.检测缺陷 *************************************
* 聚焦处理区域
reduce_domain (ImageResult, RegionUnion, ImageReduced)
* 二值化处理
threshold (ImageReduced, Region1, 0, 20)
* 开运算
opening_rectangle1 (Region1, RegionOpening, 3, 3)
* 连通域分析
connection (RegionOpening, ConnectedRegions3)
* 生成空的区域
gen_empty_region (EmptyRegion)
* 灰度扩张 (图像上填充周边相近区域)
expand_gray (ConnectedRegions3, ImageResult, EmptyRegion, RegionExpand, \
'maximal', 'image', 16)
* 处理区域 与 结果区域 交集
intersection (RegionUnion, RegionExpand, RegionIntersection)
* 闭运算
closing_circle (RegionIntersection, RegionClosing, 3.5)
* 连通域分析与筛选
connection (RegionClosing, ConnectedRegions4)
select_shape (ConnectedRegions4, SelectedRegions1, 'area', 'and', 250, 99999)
* 生成连通域区域XLD
gen_contour_region_xld (SelectedRegions1, Contours, 'border')
* 平滑显示
smooth_contours_xld (Contours, SmoothedContours, 5)
* 结果显示
dev_display (Image)
dev_set_color ('yellow')
dev_display (SmoothedContours)