image_hash.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import cv2
  2. import khandy
  3. import numpy as np
  4. def _convert_bool_matrix_to_int(bool_mat):
  5. hash_val = int(0)
  6. for item in bool_mat.flatten():
  7. hash_val <<= 1
  8. hash_val |= int(item)
  9. return hash_val
  10. def calc_image_ahash(image):
  11. """Average Hashing
  12. References:
  13. http://www.hackerfactor.com/blog/index.php?/archives/432-Looks-Like-It.html
  14. """
  15. assert khandy.is_numpy_image(image)
  16. if image.ndim == 3:
  17. image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  18. resized = cv2.resize(image, (8, 8))
  19. mean_val = np.mean(resized)
  20. hash_mat = resized >= mean_val
  21. hash_val = _convert_bool_matrix_to_int(hash_mat)
  22. return f'{hash_val:016x}'
  23. def calc_image_dhash(image):
  24. """Difference Hashing
  25. References:
  26. http://www.hackerfactor.com/blog/index.php?/archives/432-Looks-Like-It.html
  27. """
  28. assert khandy.is_numpy_image(image)
  29. if image.ndim == 3:
  30. image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  31. resized = cv2.resize(image, (9, 8))
  32. hash_mat = resized[:,:-1] >= resized[:,1:]
  33. hash_val = _convert_bool_matrix_to_int(hash_mat)
  34. return f'{hash_val:016x}'
  35. def calc_image_phash(image):
  36. """Perceptual Hashing
  37. References:
  38. http://www.hackerfactor.com/blog/index.php?/archives/432-Looks-Like-It.html
  39. """
  40. assert khandy.is_numpy_image(image)
  41. if image.ndim == 3:
  42. image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  43. resized = cv2.resize(image, (32, 32))
  44. dct_coeff = cv2.dct(resized.astype(np.float32))
  45. reduced_dct_coeff = dct_coeff[:8, :8]
  46. # # mean of coefficients excluding the DC term (0th term)
  47. # mean_val = np.mean(reduced_dct_coeff.flatten()[1:])
  48. # median of coefficients
  49. median_val = np.median(reduced_dct_coeff)
  50. hash_mat = reduced_dct_coeff >= median_val
  51. hash_val = _convert_bool_matrix_to_int(hash_mat)
  52. return f'{hash_val:016x}'