随着智能化时代的来临,视觉就相当于人的眼睛,接下来给喜欢视觉的朋友出现划痕时我们怎么找到他!
话不多说直接上代码
dev_close_window()
dev_update_window('off')
read_image(img,'C:/Users/user/Desktop/huaheng.png')
get_image_size (img, Width, Height)
dev_open_window_fit_image (img, 0, 0, Width, Height, WindowHandle)
set_display_font (WindowHandle, 12, 'mono', 'true', 'false')
dev_set_draw ('margin')
*定义输出轮廓线宽为4
dev_set_line_width (4)
dev_display (img)
*windowhandle窗口使用黑色字体在一个方框内显示按“F5”继续运行字体,并注册F5消息处理
disp_continue_message (WindowHandle, 'red', 'true')
stop ()
* ****
* step: segment image 步骤:图像分割
* ****
* -> using a local threshold 使用局部阈值
* 对Image进行7*7均值滤波 ,平滑一张图片
mean_image (img, ImageMean, 7, 7)
*用均值滤波图像作为二值化阈值图像,返回小于灰度值小于该点阈值-5的图像。
dyn_threshold (img, ImageMean, RegionDynThresh, 5, 'dark')
* -> extract connected components 提取连通分量
*由分割出来的DarkPixels获得连通区域到ConnectedRegions
*******得到的连通区域为:
connection (RegionDynThresh, ConnectedRegions)
*设置混合输出颜色为12种
dev_set_colored (12)
dev_display (ConnectedRegions)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* ****
* step: process regions 处理区域
* ****
* -> select large regions 选取大区域
*从ConnectedRegions中得到面积大于10小于1000的区域到SelectedRegions
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 15, 1000)
dev_display(img)
dev_display (SelectedRegions)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* -> visualize fractioned scratch 可视化划分划痕 X
dev_set_color ('blue')
dev_display(img)
dev_display (SelectedRegions)
disp_continue_message (WindowHandle, 'black', 'true')
*stop ()
*合并SelectedRegions的并集到RegionUnion
union1 (SelectedRegions, RegionUnion)
dev_display (RegionUnion)
*以3.5作为圆形区域扩张的半径,对RegionUnion扩张得到RegionDilation
dilation_circle (RegionUnion, RegionDilation, 3.5)
dev_display(img)
dev_display (RegionDilation)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
*由RegionDilation获取骨架给Skeleton
skeleton (RegionDilation, Skeleton)
dev_display (Skeleton)
*通过8邻接或四邻接方法将骨架链接后传给ConnectedRegions1
connection (Skeleton, ConnectedRegions1)
dev_set_colored (12)
dev_display (img)
dev_display (ConnectedRegions1)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* -> distinguish small and large scratches 区分大小划痕
*选择面积大于50小于1000的区域到Scratches
select_shape (ConnectedRegions1, SelectedRegions1, 'area', 'and', 50, 10000)
*选择面积大于1小于50的区域到Dots
select_shape (ConnectedRegions1, SelectedRegions2, 'area', 'and', 1, 50)
dev_display(img)
dev_set_color ('red')
dev_display (SelectedRegions1)
dev_set_color('blue')
dev_display (SelectedRegions2)
count_obj (SelectedRegions1, Numberballs)
disp_message (WindowHandle, '大于50小于10000的划痕数量:'+Numberballs, 'window', 0, 0, 'red', 'true')
count_obj (SelectedRegions2, Number)
disp_message (WindowHandle, '大于1小于50的划痕数量:'+Number, 'window', 50, 12, 'black', 'true')
运行结果:
谢谢您的观看!!!!