缺陷检测解决策略之二:blob分析+差分+特征分析 缺陷检测解决策略之二:blob分析+差分+特征分析缺陷检测解决策略之二:blob分析+差分+特征分析
适用类型:毛刺等
* 窗口设置
dev_update_window ('off')
* 读取图像
read_image (Fins, 'fin' + [1:3])
* 获取图像大小
get_image_size (Fins, Width, Height)
* 设置窗口显示
dev_close_window ()
dev_open_window (0, 0, Width[0], Height[0], 'black', WindowID)
set_display_font (WindowID, 14, 'mono', 'true', 'false')
* 遍历
for I := 1 to 3 by 1
* 选取图像
select_obj (Fins, Fin, I)
* 显示图像
dev_display (Fin)
* 二值化分割
binary_threshold (Fin, Background, 'max_separability', 'light', UsedThreshold)
* 设置窗口显示
dev_set_color ('blue')
dev_set_draw ('margin')
dev_set_line_width (4)
dev_display (Background)
disp_continue_message (WindowID, 'black', 'true')
stop ()
* 闭运算
closing_circle (Background, ClosedBackground, 250)
* 设置窗口显示
dev_set_color ('green')
dev_display (ClosedBackground)
disp_continue_message (WindowID, 'black', 'true')
stop ()
* 区域相减
difference (ClosedBackground, Background, RegionDifference)
* 开运算
opening_rectangle1 (RegionDifference, FinRegion, 5, 5)
* 显示结果
dev_display (Fin)
dev_set_color ('red')
dev_display (FinRegion)
* 获取区域面积和中心
area_center (FinRegion, FinArea, Row, Column)
if (I < 3)
disp_continue_message (WindowID, 'black', 'true')
stop ()
endif
endfor
示例2:
背景均匀干净
* 读取图像
read_image (Image, 'pcb')
* 窗口设置
dev_close_window ()
* 获取图像大小
get_image_size (Image, Width, Height)
* 窗口显示设置
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_display (Image)
* detect defects ...
* 灰度开运算:暗的像素点变多
gray_opening_shape (Image, ImageOpening, 7, 7, 'octagon')
* 灰度闭运算:亮的像素点变多
gray_closing_shape (Image, ImageClosing, 7, 7, 'octagon')
* 差分:动态阈值分割
dyn_threshold (ImageOpening, ImageClosing, RegionDynThresh, 75, 'not_equal')
* 结果显示
dev_display (Image)
dev_set_color ('red')
dev_set_draw ('margin')
dev_display (RegionDynThresh)
示例3:
* 窗口设置
get_system ('clip_region', Information)
set_system ('clip_region', 'true')
dev_update_window ('off')
dev_close_window ()
dev_open_window (0, 0, 512, 512, 'black', WindowID)
* 读取图像
read_image (Images, ['ic0','ic1','ic2','ic3'])
* 单通道图像转多通道图像
channels_to_image (Images, Ic)
* 选取第一张图像
select_obj (Images, Input1, 1)
* 显示
dev_display (Input1)
set_display_font (WindowID, 14, 'mono', 'false', 'false')
disp_continue_message (WindowID, 'black', 'true')
stop ()
* 选取第二张图像
select_obj (Images, Input2, 2)
* 显示
dev_display (Input2)
disp_continue_message (WindowID, 'black', 'true')
stop ()
* 选取第三张图像
select_obj (Images, Input3, 3)
* 显示
dev_display (Input3)
disp_continue_message (WindowID, 'black', 'true')
stop ()
* 选取第四张图像
select_obj (Images, Input4, 4)
* 显示
dev_display (Input4)
disp_continue_message (WindowID, 'black', 'true')
stop ()
* 均值滤波
mean_n (Ic, ImageMean)
dev_display (ImageMean)
* 图像分割
threshold (Images, Darks, 0, 40)
* 合并
union1 (Darks, Dark)
* 填充
fill_up (Dark, DarkFilled)
* 显示
dev_set_color ('green')
dev_display (DarkFilled)
disp_continue_message (WindowID, 'black', 'true')
stop ()
* 阈值分割
threshold (Images, Lights, 100, 255)
* 合并
union1 (Lights, Light)
* 交集
intersection (DarkFilled, Light, Intersection)
dev_display (ImageMean)
dev_display (Intersection)
disp_continue_message (WindowID, 'black', 'true')
stop ()
* 补集
complement (DarkFilled, Back)
* 显示
dev_set_color ('red')
dev_display (Back)
disp_continue_message (WindowID, 'black', 'true')
stop ()
* 填充间隙
expand_region (Intersection, Back, RegionExpanded, 10, 'image')
* 填充
fill_up (RegionExpanded, Good)
* 显示
dev_display (ImageMean)
dev_set_color ('green')
dev_display (Good)
disp_continue_message (WindowID, 'black', 'true')
stop ()
* 区域相减
difference (DarkFilled, Good, Rest)
dev_set_color ('red')
dev_display (Rest)
disp_continue_message (WindowID, 'black', 'true')
stop ()
* 填充间隙
expand_gray (Rest, Ic, Good, Bad, 6, 'image', 5)
dev_display (Bad)
disp_continue_message (WindowID, 'black', 'true')
stop ()
* 连通
connection (Bad, ConnectedBad)
* 通过面积选取区域
select_shape (ConnectedBad, BigBad, 'area', 'and', 150, 99999)
* 获取区域的中心和面积
area_center (BigBad, AreaBad, Row, Column)
* 计数
count_obj (BigBad, NumMissingSolder)
* 显示结果
AreaMissingSolder := sum(AreaBad)
dev_display (Images)
dev_set_color ('green')
dev_set_draw ('margin')
dev_set_line_width (3)
dev_display (Good)
dev_set_color ('red')
dev_display (BigBad)
dev_set_draw ('fill')
dev_set_line_width (1)
dev_update_window ('on')
set_system ('clip_region', Information)
实例:注塑吸嘴缺口检测
******************************************第一步 初始化**************************************************
*读取一张图像
read_image (Hull, 'hull')
*获取图像大小
get_image_size (Hull, Width, Height)
*关闭已经打开的窗口
dev_close_window ()
*打开一个新的窗口
dev_open_window (0, 0, Width, Height, 'black', WindowID)
*显示图像
dev_display (Hull)
******************************************第二步 图像处理**************************************************
*阈值操作,分割出吸嘴
threshold (Hull, Dark, 0, 80)
*补集运算,获取背景区域
difference (Hull, Dark, Light)
*对背景区域进行连通处理
connection (Light, ConnectedRegions)
*过滤出背景区域
select_shape (ConnectedRegions, NoHullCand, 'area', 'and', 50000, 9999999)
*对过滤的背景区域进行闭运算,填充背景间隙和平滑背景边界
closing_circle (NoHullCand, NoHull, 13.5)
*补集运算,获取吸嘴区域
difference (Hull, NoHull, Region)
*对吸嘴区域开运算
opening_circle (Region, RegionOpening, 2.5)
*对吸嘴区域进行连通处理
connection (RegionOpening, ConnectedRegions)
*过滤出吸嘴区域
select_shape (ConnectedRegions, RegionHull, 'area', 'and', 5000, 9999999)
*将吸嘴区域转换为凸包区域
shape_trans (RegionHull, ConvexHull, 'convex')
*补集运算,获取吸嘴的缺口
difference (ConvexHull, RegionHull, Region)
*对吸嘴缺口区域进行连通处理
connection (Region, ConnectedRegions)
*过滤出吸嘴缺口
select_shape (ConnectedRegions, LargeHoles, 'area', 'and', 2000, 99999)
select_shape (LargeHoles, Holes, 'convexity', 'and', 0, 0.85)
*显示图像
dev_display (Hull)
*设置输出对象的线宽度
dev_set_line_width (5)
*设置区域的填充方式
dev_set_draw ('margin')
*设置输出对象显示颜色为红色
dev_set_color ('red')
*显示吸嘴缺口
dev_display (Holes)