云计算期末大作业 论文图像复用的机器自动检查 魏如蓝 10172100262
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.

35 line
883 B

3 年之前
  1. # 正则化图像
  2. def regularizeImage(img, size = (9, 8)):
  3. return img.resize(size).convert('L')
  4. # 计算hash值
  5. def getHashCode(img, size = (9, 8)):
  6. result = []
  7. for i in range(size[0] - 1):
  8. for j in range(size[1]):
  9. current_val = img.getpixel((i, j))
  10. next_val = img.getpixel((i + 1, j))
  11. if current_val > next_val:
  12. result.append(1)
  13. else:
  14. result.append(0)
  15. return result
  16. # 比较hash值
  17. def compHashCode(hc1, hc2):
  18. cnt = 0
  19. for i, j in zip(hc1, hc2):
  20. if i == j:
  21. cnt += 1
  22. return cnt
  23. # 计算差异哈希算法相似度
  24. def caldHashSimilarity(img1, img2):
  25. img1 = regularizeImage(img1)
  26. img2 = regularizeImage(img2)
  27. hc1 = getHashCode(img1)
  28. hc2 = getHashCode(img2)
  29. return compHashCode(hc1, hc2)
  30. __all__ = ['caldHashSimilarity']