demo.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # @Time : 2024/5/23 0023 下午 2:25
  4. # @Author : liudan
  5. # @File : demo.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. #import image_similarity_count
  17. def registration_demo():
  18. image_dir = './data/yongsheng_image/3_image_query'
  19. #images = [f for f in os.listdir(image_dir) if os.path.isfile(os.path.join(image_dir, f))]
  20. demo_image_path = './data/yongsheng_image/3_regis_result/'
  21. os.makedirs(demo_image_path, exist_ok=True)
  22. wraped_image_list =[]
  23. for filename in os.listdir(image_dir):
  24. # 检查文件是否是图片(这里假设是.jpg格式)
  25. if filename.endswith('.JPG') or filename.endswith('.jpg'):
  26. # 构建图片的完整路径
  27. image1_path = os.path.join(image_dir, filename)
  28. query_image = cv2.imread(image1_path)
  29. #query_image = cv2.imread('./data/images/frame_002100_small.jpg') #原图2160*3840
  30. ref_image = cv2.imread('./data/yongsheng_image/ref_image/DSC_0451.JPG')
  31. # new_size = (1420, 800) # W,H
  32. # query_image_resize = cv2.resize(query_image,new_size)
  33. # ref_image_resize = cv2.resize(ref_image,new_size)
  34. height, width = ref_image.shape[:2]
  35. scale_factor = 0.37
  36. query_image_resize = cv2.resize(query_image, dsize=None, fx=scale_factor, fy=scale_factor)
  37. ref_image_resize = cv2.resize(ref_image, dsize=None, fx=scale_factor, fy=scale_factor)
  38. query_gray = cv2.cvtColor(query_image_resize, cv2.COLOR_BGR2GRAY)
  39. ref_gray = cv2.cvtColor(ref_image_resize, cv2.COLOR_BGR2GRAY)
  40. superglue_matcher = Matcher(
  41. {
  42. "superpoint": {
  43. "input_shape": (-1, -1),
  44. "keypoint_threshold": 0.003,
  45. },
  46. "superglue": {
  47. "match_threshold": 0.5,
  48. },
  49. "use_gpu": True,
  50. }
  51. )
  52. query_kpts, ref_kpts, _, _, matches = superglue_matcher.match(query_gray, ref_gray)
  53. M, mask = cv2.findHomography(
  54. np.float64([query_kpts[m.queryIdx].pt for m in matches]).reshape(-1, 1, 2),
  55. np.float64([ref_kpts[m.trainIdx].pt for m in matches]).reshape(-1, 1, 2),
  56. method=cv2.USAC_MAGSAC,
  57. ransacReprojThreshold=5.0,
  58. maxIters=10000,
  59. confidence=0.95,
  60. )
  61. logger.info(f"number of inliers: {mask.sum()}")
  62. matches = np.array(matches)[np.all(mask > 0, axis=1)]
  63. matches = sorted(matches, key=lambda match: match.distance)
  64. matched_image = cv2.drawMatches(
  65. query_image_resize,
  66. query_kpts,
  67. ref_image_resize,
  68. ref_kpts,
  69. matches[:50],
  70. None,
  71. flags=2,
  72. )
  73. #demo_image_path = './data/yongsheng_image/1_regis_result/'
  74. match_file_name = f"match_image_{filename}.jpg"
  75. cv2.imwrite(os.path.join(demo_image_path , match_file_name), matched_image)
  76. # cv2.imwrite(os.path.join(demo_image_path + f"matched_image.jpg"), matched_image)
  77. wrap_image = cv.warpPerspective(query_image_resize, M, (ref_image_resize.shape[1], ref_image_resize.shape[0]))
  78. wrap_image = cv2.resize(wrap_image,(ref_image.shape[1], ref_image.shape[0]))
  79. wrap_filename = f"wrap_image_{filename}.jpg"
  80. cv2.imwrite(os.path.join(demo_image_path, wrap_filename), wrap_image)
  81. #cv2.imwrite(os.path.join(demo_image_path + f"wrap_image.jpg"), wrap_image)
  82. wraped_image_list.append((wrap_image,wrap_filename))
  83. result_image = cv2.subtract(ref_image, wrap_image)
  84. result_file_name = f"result_image_{filename}.jpg"
  85. #cv2.imwrite(os.path.join(demo_image_path + f"result_image.jpg"), result_image)
  86. cv2.imwrite(os.path.join(demo_image_path, result_file_name), result_image)
  87. return wraped_image_list
  88. if __name__ == "__main__":
  89. registration_demo()
  90. #image_similarity_count.compare_boxes_similarity(image1_path, image2_path, json_file_path, similarity_threshold=0.5)