image_match_demo.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # @Time : 2024/5/31 0031 上午 9:55
  4. # @Author : liudan
  5. # @File : image_match_demo.py
  6. # @Software: pycharm
  7. #!/usr/bin/env python
  8. # -*- coding: utf-8 -*-
  9. # @Time : 2024/5/23 0023 下午 2:25
  10. # @Author : liudan
  11. # @File : demo.py
  12. # @Software: pycharm
  13. import cv2 as cv
  14. import cv2
  15. import numpy as np
  16. from loguru import logger
  17. import os
  18. from superpoint_superglue_deployment import Matcher
  19. from datetime import datetime
  20. import random
  21. # timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
  22. #import image_similarity_count
  23. def registration_demo():
  24. query_image = cv2.imread('./data/images/frame_002100_small.jpg') #原图2160*3840
  25. ref_image = cv2.imread('./data/images/frame_002200_small.jpg')
  26. new_size = (800, 1420) # W,H
  27. query_image_resize = cv2.resize(query_image,new_size)
  28. ref_image_resize = cv2.resize(ref_image,new_size)
  29. query_gray = cv2.cvtColor(query_image_resize, cv2.COLOR_BGR2GRAY)
  30. ref_gray = cv2.cvtColor(ref_image_resize, cv2.COLOR_BGR2GRAY)
  31. superglue_matcher = Matcher(
  32. {
  33. "superpoint": {
  34. "input_shape": (-1, -1),
  35. "keypoint_threshold": 0.003,
  36. },
  37. "superglue": {
  38. "match_threshold": 0.5,
  39. },
  40. "use_gpu": True,
  41. }
  42. )
  43. query_kpts, ref_kpts, _, _, matches = superglue_matcher.match(query_gray, ref_gray)
  44. M, mask = cv2.findHomography(
  45. np.float64([query_kpts[m.queryIdx].pt for m in matches]).reshape(-1, 1, 2),
  46. np.float64([ref_kpts[m.trainIdx].pt for m in matches]).reshape(-1, 1, 2),
  47. method=cv2.USAC_MAGSAC,
  48. ransacReprojThreshold=5.0,
  49. maxIters=10000,
  50. confidence=0.95,
  51. )
  52. logger.info(f"number of inliers: {mask.sum()}")
  53. matches = np.array(matches)[np.all(mask > 0, axis=1)]
  54. matches = sorted(matches, key=lambda match: match.distance)
  55. matched_image = cv2.drawMatches(
  56. query_image_resize,
  57. query_kpts,
  58. ref_image_resize,
  59. ref_kpts,
  60. matches[:50],
  61. None,
  62. flags=2,
  63. )
  64. demo_image_path = './data/similarity_image/demo_result/'
  65. # match_file_name = f"match_image_{idx1}_{idx2}_{iteration}.jpg"
  66. # cv2.imwrite(os.path.join(demo_image_path , match_file_name), matched_image)
  67. cv2.imwrite(os.path.join(demo_image_path + f"matched_image.jpg"), matched_image)
  68. wrap_image = cv.warpPerspective(query_image_resize, M, (ref_image_resize.shape[1], ref_image_resize.shape[0]))
  69. wrap_image = cv2.resize(wrap_image,(ref_image.shape[1], ref_image.shape[0]))
  70. # wrap_filename = f"wrap_image_{idx1}_{idx2}_{iteration}.jpg"
  71. # cv2.imwrite(os.path.join(demo_image_path, wrap_filename), wrap_image)
  72. cv2.imwrite(os.path.join(demo_image_path + f"wrap_image.jpg"), wrap_image)
  73. result_image = cv2.subtract(ref_image, wrap_image)
  74. # result_file_name = f"result_image_{idx1}_{idx2}_{iteration}.jpg"
  75. cv2.imwrite(os.path.join(demo_image_path + f"result_image.jpg"), result_image)
  76. # cv2.imwrite(os.path.join(demo_image_path, result_file_name), result_image)
  77. return wrap_image
  78. if __name__ == "__main__":
  79. registration_demo()
  80. #image_similarity_count.compare_boxes_similarity(image1_path, image2_path, json_file_path, similarity_threshold=0.5)