small_image_match.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # @Time : 2024/6/17 0017 上午 10:19
  4. # @Author : liudan
  5. # @File : small_image_match.py
  6. # @Software: pycharm
  7. import cv2 as cv
  8. import cv2
  9. import numpy as np
  10. from loguru import logger
  11. import os
  12. from superpoint_superglue_deployment import Matcher
  13. from datetime import datetime
  14. import random
  15. # timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
  16. def registration(ref_image_path, query_image_path):
  17. ref_image = cv2.imread(ref_image_path)
  18. query_image = cv2.imread(query_image_path)
  19. ref_gray = cv2.cvtColor(ref_image, cv2.COLOR_BGR2GRAY)
  20. query_gray = cv2.cvtColor(query_image, cv2.COLOR_BGR2GRAY)
  21. query_gray_rotate = cv2.rotate(query_gray, cv2.ROTATE_180)
  22. superglue_matcher = Matcher(
  23. {
  24. "superpoint": {
  25. "input_shape": (-1, -1),
  26. "keypoint_threshold": 0.003,
  27. },
  28. "superglue": {
  29. "match_threshold": 0.5,
  30. },
  31. "use_gpu": True,
  32. }
  33. )
  34. _, _, _, _, matches1 = superglue_matcher.match(query_gray, ref_gray)
  35. _, _, _, _, matches2 = superglue_matcher.match(query_gray_rotate, ref_gray)
  36. if len(matches1) > len(matches2):
  37. flag = True
  38. else:
  39. flag = False
  40. print(flag)
  41. # num_matches = len(matches2)
  42. # score = 0
  43. # if num_matches < 4:
  44. # print('The welding has been done in reverse')
  45. # score = 0
  46. # else:
  47. # score = 1
  48. # print("score:", score)
  49. if __name__ == "__main__":
  50. ref_image_path = './data/small_image_match/json_image1_E_0.jpg'
  51. query_image_path = './data/small_image_match/json_image2_E_0.jpg'
  52. registration(ref_image_path, query_image_path)