image_compare.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import cv2
  2. import numpy as np
  3. import imagehash
  4. from PIL import Image
  5. from skimage.metrics import structural_similarity as ssim
  6. # 色度直方图相似度
  7. def calculate_color_histogram_similarity(image_path1, image_path2):
  8. # 读取图像
  9. img1 = cv2.imread(image_path1)
  10. img2 = cv2.imread(image_path2)
  11. # 将图像转换为 HSV 色彩空间
  12. hsv1 = cv2.cvtColor(img1, cv2.COLOR_BGR2HSV)
  13. hsv2 = cv2.cvtColor(img2, cv2.COLOR_BGR2HSV)
  14. # 计算色度直方图
  15. hist1 = cv2.calcHist([hsv1], [0, 1], None, [50, 60], [0, 180, 0, 256])
  16. hist2 = cv2.calcHist([hsv2], [0, 1], None, [50, 60], [0, 180, 0, 256])
  17. # 归一化直方图
  18. hist1 = cv2.normalize(hist1, hist1).flatten()
  19. hist2 = cv2.normalize(hist2, hist2).flatten()
  20. # 计算相似性
  21. correlation = cv2.compareHist(hist1, hist2, cv2.HISTCMP_CORREL)
  22. chi_square = cv2.compareHist(hist1, hist2, cv2.HISTCMP_CHISQR)
  23. intersection = cv2.compareHist(hist1, hist2, cv2.HISTCMP_INTERSECT)
  24. bhattacharyya = cv2.compareHist(hist1, hist2, cv2.HISTCMP_BHATTACHARYYA)
  25. return correlation, chi_square, intersection, bhattacharyya
  26. # SIFT特征相似度
  27. def feature_matching(img1_path, img2_path):
  28. img1 = cv2.imread(img1_path, cv2.IMREAD_GRAYSCALE)
  29. img2 = cv2.imread(img2_path, cv2.IMREAD_GRAYSCALE)
  30. # 初始化SIFT检测器
  31. sift = cv2.SIFT_create()
  32. kp1, des1 = sift.detectAndCompute(img1, None)
  33. kp2, des2 = sift.detectAndCompute(img2, None)
  34. # FLANN匹配器
  35. FLANN_INDEX_KDTREE = 1
  36. index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
  37. search_params = dict(checks=50)
  38. flann = cv2.FlannBasedMatcher(index_params, search_params)
  39. matches = flann.knnMatch(des1, des2, k=2)
  40. # 筛选优质匹配
  41. good = []
  42. for m, n in matches:
  43. if m.distance < 0.7 * n.distance:
  44. good.append(m)
  45. return len(good) / min(len(kp1), len(kp2)) # 归一化匹配率
  46. # 感知相似度
  47. def phash_similarity(img1_path, img2_path):
  48. hash1 = imagehash.phash(Image.open(img1_path))
  49. hash2 = imagehash.phash(Image.open(img2_path))
  50. return 1 - (hash1 - hash2) / len(hash1.hash) ** 2 # 归一化为0-1
  51. # SSIM相似度
  52. def compare_ssim(img1_path, img2_path):
  53. img1 = cv2.resize(cv2.imread(img1_path), (256, 256))
  54. img2 = cv2.resize(cv2.imread(img2_path), (256, 256))
  55. # 转换为灰度图
  56. gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
  57. gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
  58. # 计算 SSIM
  59. score, _ = ssim(gray1, gray2, full=True)
  60. return score
  61. if __name__ == '__main__':
  62. # 示例用法
  63. image_path1 = '/data/data/luosy/project/oral/data/key_frame/frame_001.jpg'
  64. image_path2 = '/data/data/luosy/project/oral/data/key_frame/frame_002.jpg'
  65. similarity_scores = calculate_color_histogram_similarity(image_path1, image_path2)
  66. print(f'相似性(⬆): {similarity_scores[0]}')
  67. print(f'卡方(⬇): {similarity_scores[1]}')
  68. print(f'交集(⬆): {similarity_scores[2]}')
  69. print(f'巴氏距离(⬇): {similarity_scores[3]}')
  70. match_ratio = feature_matching(image_path1, image_path2)
  71. print(f"特征点匹配率: {match_ratio:.4f}")
  72. print("pHash Similarity:", phash_similarity(image_path1, image_path2))
  73. print("SSIM:", compare_ssim(image_path1, image_path2))