如何用diplib检测智能卡接触针凹坑与划痕并过滤干扰
智能卡接触针缺陷检测问题
1. 凹坑检测的多余局部高度过滤
在智能卡制造中,探针卡顿会在接触针上留下凹坑。使用dip.Tophat+dip.HysteresisThreshold能提取凹坑,但会误识别接触针外部/边缘的多余局部高度,需过滤此类干扰。
现有实现代码:
import diplib as dip height = dip.ImageRead('my2.png') local_height = dip.Tophat(height, 9) dip.viewer.ShowModal(local_height) inselbergs = dip.HysteresisThreshold(local_height, 20, 30) dip.viewer.ShowModal(inselbergs) dip.viewer.ShowModal(dip.Overlay(height, inselbergs)) labels = dip.Label(inselbergs, minSize = 3, maxSize = 20) dip.viewer.ShowModal(dip.Overlay(height, labels)) dip.ImageWrite(dip.Overlay(height, labels), 'out.jpg') result = dip.MeasurementTool.Measure(labels, features=['Size', 'Center']) print(result)
(图:两根卡顿探针在接触针上留下的凹坑)
(图:需过滤的多余局部高度)
2. 划痕检测的干扰过滤
需检测接触针上的划痕,使用dip.FrangiVesselness能识别线条,但存在三类干扰:接触针外部/边缘的多余线条、非划痕线条、模块左下角的预打孔,需全部过滤。
现有实现代码:
import diplib as dip height = dip.ImageRead('my2.png') vess = dip.FrangiVesselness(height, sigmas=0.6, polarity='black') dip.viewer.ShowModal(vess) myout = dip.HysteresisThreshold(vess,0.2,0.4) dip.viewer.ShowModal(dip.Overlay(height, myout)) labels = dip.Label(myout, minSize = 3, maxSize = 30) dip.viewer.ShowModal(dip.Overlay(height, labels)) dip.ImageWrite(dip.Overlay(height, labels), 'out2.jpg')
(图:接触针上的划痕)
(图:dip::FrangiVesselness识别出大量线条)
解决方案
一、凹坑检测的多余区域过滤
核心是先提取接触针区域掩码,仅在掩码内部保留检测结果,彻底排除外部干扰。
实现步骤:
- 阈值分割提取接触针大致区域(接触针灰度通常与背景差异明显,需根据实际图像调整阈值)。
- 用形态学开闭操作优化掩码,填补孔洞、去除小噪点。
- 将原始检测结果与掩码做按位与,过滤外部区域。
修改后代码:
import diplib as dip height = dip.ImageRead('my2.png') # 1. 生成接触针区域掩码 mask = height > 150 # 替换为匹配图像的实际阈值 mask = dip.MorphologicalOpening(mask, dip.SE(3)) # 去除小噪点 mask = dip.MorphologicalClosing(mask, dip.SE(5)) # 填补接触针区域内的孔洞 # 2. 原凹坑检测流程 local_height = dip.Tophat(height, 9) inselbergs = dip.HysteresisThreshold(local_height, 20, 30) # 3. 过滤接触针外部的多余区域 inselbergs_filtered = inselbergs * mask # 后续标注与测量 labels = dip.Label(inselbergs_filtered, minSize=3, maxSize=20) dip.viewer.ShowModal(dip.Overlay(height, labels)) dip.ImageWrite(dip.Overlay(height, labels), 'out_filtered.jpg') result = dip.MeasurementTool.Measure(labels, features=['Size', 'Center']) print(result)
二、划痕检测的多类干扰过滤
基于接触针掩码排除外部干扰,同时通过形态特征过滤非划痕区域,并针对预打孔的固定位置做坐标排除。
实现步骤:
- 生成接触针区域掩码(同凹坑检测步骤)。
- 用掩码过滤外部线条。
- 测量标注区域的形态特征:保留**长宽比>3(细长)、圆形度<0.5(非圆形)**的区域,排除接近圆形的非划痕干扰。
- 根据预打孔的固定位置,通过坐标范围排除此类干扰。
修改后代码:
import diplib as dip height = dip.ImageRead('my2.png') # 1. 生成接触针区域掩码 mask = height > 150 # 调整阈值匹配实际图像 mask = dip.MorphologicalOpening(mask, dip.SE(3)) mask = dip.MorphologicalClosing(mask, dip.SE(5)) # 2. 原划痕检测流程 vess = dip.FrangiVesselness(height, sigmas=0.6, polarity='black') myout = dip.HysteresisThreshold(vess, 0.2, 0.4) # 3. 过滤接触针外部的线条 myout_filtered = myout * mask # 4. 标注区域并过滤非划痕、预打孔 labels = dip.Label(myout_filtered, minSize=3) # 测量关键形态特征 measurements = dip.MeasurementTool.Measure(labels, height, features=['Elongation', 'Circularity', 'Center']) # 生成过滤后的标注图 filtered_labels = dip.Image(labels.Sizes(), labels.DataType()) for obj_id in measurements.Objects(): elongation = measurements[obj_id]['Elongation'] circularity = measurements[obj_id]['Circularity'] center_x, center_y = measurements[obj_id]['Center'] # 排除预打孔:假设预打孔在模块左下角,需根据实际坐标调整范围 is_pre_punch = center_x < 50 and center_y > height.Rows() - 50 # 保留细长、非圆形的划痕区域 if elongation > 3 and circularity < 0.5 and not is_pre_punch: filtered_labels[labels == obj_id] = obj_id # 显示与保存结果 dip.viewer.ShowModal(dip.Overlay(height, filtered_labels)) dip.ImageWrite(dip.Overlay(height, filtered_labels), 'out2_filtered.jpg')
内容的提问来源于stack exchange,提问作者HotCat
相关产品推荐
相关产品推荐

