You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 line
3.3KB

  1. ## -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. # 余弦相似度计算
  4. import time
  5. import cv2
  6. from PIL import Image
  7. from numpy import average, dot, linalg
  8. # 对图片进行统一化处理
  9. def get_thum(image, size=(64, 64), greyscale=False):
  10. # 利用image对图像大小重新设置, Image.ANTIALIAS为高质量的
  11. image = image.resize(size, Image.ANTIALIAS)
  12. if greyscale:
  13. # 将图片转换为L模式,其为灰度图,其每个像素用8个bit表示
  14. image = image.convert('L')
  15. return image
  16. # 计算图片的余弦距离
  17. def image_similarity_vectors_via_numpy(image1, image2):
  18. image1 = get_thum(image1)
  19. image2 = get_thum(image2)
  20. images = [image1, image2]
  21. vectors = []
  22. norms = []
  23. for image in images:
  24. vector = []
  25. for pixel_tuple in image.getdata():
  26. vector.append(average(pixel_tuple))
  27. vectors.append(vector)
  28. # linalg=linear(线性)+algebra(代数),norm则表示范数
  29. # 求图片的范数
  30. norms.append(linalg.norm(vector, 2))
  31. a, b = vectors
  32. a_norm, b_norm = norms
  33. # dot返回的是点积,对二维数组(矩阵)进行计算
  34. res = dot(a / a_norm, b / b_norm)
  35. return res
  36. #差值感知算法
  37. def dHash(image):
  38. #缩放9*8
  39. image=cv2.resize(image,(9,8),interpolation=cv2.INTER_CUBIC)
  40. #转换灰度图
  41. image=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
  42. # print(image.shape)
  43. hash=[]
  44. #每行前一个像素大于后一个像素为1,相反为0,生成哈希
  45. for i in range(8):
  46. for j in range(8):
  47. if image[i,j]>image[i,j+1]:
  48. hash.append(1)
  49. else:
  50. hash.append(0)
  51. return hash
  52. #计算汉明距离
  53. def Hamming_distance(hash1,hash2):
  54. num = 0
  55. for index in range(len(hash1)):
  56. if hash1[index] != hash2[index]:
  57. num += 1
  58. return num
  59. if __name__ == '__main__':
  60. # 把图片表示成一个向量,通过计算向量之间的余弦距离来表征两张图片的相似度 0.9760
  61. image1 = cv2.imread('../image/1.jpg')
  62. image2 = cv2.imread('../image/2.jpg')
  63. # image3 = cv2.imread('image/AI2.jpg')
  64. # image4 = cv2.imread('image/AI3.jpg')
  65. # image5 = cv2.imread('image/AI4.jpg')
  66. # image6 = cv2.imread('a.jpg')
  67. # image7 = cv2.imread('AI.jpg')
  68. hash1 = dHash(image1)
  69. hash2 = dHash(image2)
  70. # hash3 = dHash(image3)
  71. # hash4 = dHash(image4)
  72. # hash5 = dHash(image5)
  73. # hash6 = dHash(image6)
  74. # hash7 = dHash(image7)
  75. start = time.time()
  76. dist = Hamming_distance(hash1, hash2)
  77. #将距离转化为相似度
  78. similarity = 1 - dist * 1.0 / 64
  79. # print(dist)
  80. print(similarity, time.time() - start)
  81. # cosin = image_similarity_vectors_via_numpy(image1, image2)
  82. # print('图片余弦相似度', cosin)
  83. # cosin1 = image_similarity_vectors_via_numpy(image1, image3)
  84. # print('图片余弦相似度', cosin1)
  85. # cosin2 = image_similarity_vectors_via_numpy(image1, image4)
  86. # print('图片余弦相似度', cosin2)
  87. # cosin3 = image_similarity_vectors_via_numpy(image1, image5)
  88. # print('图片余弦相似度', cosin3)
  89. # cosin4 = image_similarity_vectors_via_numpy(image5, image6)
  90. # print('图片余弦相似度', cosin4)
  91. # cosin5 = image_similarity_vectors_via_numpy(image1, image6)
  92. # print('图片余弦相似度', cosin5)