代码片段
#巴氏距离
hist1 = create_hist(imgName) # 给标准样品绘制直方图
for i in range(1, 2):
print(i) # 打印图片序号
img = cv2.imread(imgName1)
hist2 = create_hist(imgName1) # 给测试样品绘制直方图
match1 = cv2.compareHist(hist1, hist2, cv2.HISTCMP_BHATTACHARYYA) # 返回巴氏距离
match2 = cv2.compareHist(hist1, hist2, cv2.HISTCMP_CORREL) # 返回相关性
print("巴氏距离:%s, 相关性:%s" % (match1, match2))
print("\n")
if match2 >= 0.99999999999999:
jpg = QtGui.QPixmap("hege.png").scaled(self.label_3.width(), self.label_3.height())
self.label_3.setPixmap(jpg)
else:
jpg = QtGui.QPixmap("buhege.png").scaled(self.label_3.width(), self.label_3.height())
self.label_3.setPixmap(jpg)
print("\n")
#采用surf特征点集寻找图片缺陷
std = cv2.imread(imgName1)
test = cv2.imread(imgName)
# 统一尺寸
std = cv2.resize(std, (int(test.shape[1]), int(test.shape[0])))
# 标准图透视变换成待测图角度
std_wrap = rectify(test, std, is_debug=True)
show = cv2.resize(std_wrap, (740, 580))
cv2.imwrite("toushi.jpeg", show)
#os.remove("toushi.jpeg")
# 调整后的标准图与待测图作差
diff = cv2.absdiff(test, std_wrap)
diff = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)
cv2.imwrite("diff.jpeg", diff)
# 阈值化
diff_thr = cv2.threshold(diff, 50, 255, cv2.THRESH_TOZERO)[1]
cv2.imwrite("d_thr.jpeg", diff_thr)
# 均值滤波
diff_thr_blur = cv2.blur(diff_thr, (3, 3))
cv2.imwrite("dt_blur.jpeg", diff_thr_blur)
# 闭运算后阈值化
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
diff_thr_mor = cv2.morphologyEx(diff_thr, cv2.MORPH_CLOSE, kernel)
cv2.imwrite("dtb_mor.jpeg", diff_thr_mor)
# 标记缺陷
dst = diff_thr_mor
_, contours, hierarchy1 = cv2.findContours(dst, mode=cv2.RETR_TREE, method=cv2.CHAIN_APPROX_NONE)
ex = test.copy()
for c in contours:
if len(c) < 10:
continue
x, y, w, h = cv2.boundingRect(c)
cv2.rectangle(ex, (x, y), (x + w, y + h), (255, 255, 0), 2)
ex = cv2.resize(ex, (640, 480))
print(ex)
cv2.imwrite("final.jpeg", ex)
cv2.waitKey(0)
compare(match2, img, i) # 比较并绘制
巴士距离和surf特征点匹配。
缺陷检测(图片作差)
缺陷标定(两张测试图)(蓝色)
1、已生成可执行exe文件。无需任何环境搭建和软件下载,打开即可使用
链接:https://download.csdn.net/download/m0_51381592/20086159
2、源码,环境需要
python3.6(必须)
pyqt
链接:https://download.csdn.net/download/m0_51381592/20086249
源码