Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

44 rindas
1.3KB

  1. # 自定义计算两个图片相似度函数
  2. import cv2
  3. def img_similarity(img1_path, img2_path):
  4. """
  5. :param img1_path: 图片1路径
  6. :param img2_path: 图片2路径
  7. :return: 图片相似度
  8. """
  9. try:
  10. # 读取图片
  11. img1 = cv2.imread(img1_path, cv2.IMREAD_GRAYSCALE)
  12. img2 = cv2.imread(img2_path, cv2.IMREAD_GRAYSCALE)
  13. # 初始化ORB检测器
  14. orb = cv2.ORB_create()
  15. kp1, des1 = orb.detectAndCompute(img1, None)
  16. kp2, des2 = orb.detectAndCompute(img2, None)
  17. # 提取并计算特征点
  18. bf = cv2.BFMatcher(cv2.NORM_HAMMING)
  19. # knn筛选结果
  20. matches = bf.knnMatch(des1, trainDescriptors=des2, k=2)
  21. # 查看最大匹配点数目
  22. good = [m for (m, n) in matches if m.distance < 0.75 * n.distance]
  23. # print(len(good))
  24. # print(len(matches))
  25. similary = float(len(good))/len(matches)
  26. print("(ORB算法)两张图片相似度为:%s" % similary)
  27. return similary
  28. except:
  29. print('无法计算两张图片相似度')
  30. return '0'
  31. if __name__ == '__main__':
  32. name1='image/AI.jpg'
  33. name2='image/AI3.jpg'
  34. # similary 值为0-1之间,1表示相同
  35. similary = img_similarity(name1, name2)
  36. print(similary)