image-stitching.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # @Time : 2024/6/4 0004 下午 1:52
  4. # @Author : liudan
  5. # @File : image-stitching.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. def registration_demo():
  14. query_image = cv2.imread('./data/stitcher_image/frame_000240.jpg') #hw
  15. ref_image = cv2.imread('./data/stitcher_image/frame_000330.jpg')
  16. new_size = (1420, 800) # W,H
  17. query_image_resize = cv2.resize(query_image,new_size)
  18. ref_image_resize = cv2.resize(ref_image,new_size)
  19. query_gray = cv2.cvtColor(query_image_resize, cv2.COLOR_BGR2GRAY)
  20. ref_gray = cv2.cvtColor(ref_image_resize, cv2.COLOR_BGR2GRAY)
  21. superglue_matcher = Matcher(
  22. {
  23. "superpoint": {
  24. "input_shape": (-1, -1),
  25. "keypoint_threshold": 0.003,
  26. },
  27. "superglue": {
  28. "match_threshold": 0.5,
  29. },
  30. "use_gpu": True,
  31. }
  32. )
  33. query_kpts, ref_kpts, _, _, matches = superglue_matcher.match(query_gray, ref_gray)
  34. M, mask = cv2.findHomography(
  35. np.float64([query_kpts[m.queryIdx].pt for m in matches]).reshape(-1, 1, 2),
  36. np.float64([ref_kpts[m.trainIdx].pt for m in matches]).reshape(-1, 1, 2),
  37. method=cv2.USAC_MAGSAC,
  38. ransacReprojThreshold=5.0,
  39. maxIters=10000,
  40. confidence=0.95,
  41. )
  42. logger.info(f"number of inliers: {mask.sum()}")
  43. matches = np.array(matches)[np.all(mask > 0, axis=1)]
  44. matches = sorted(matches, key=lambda match: match.distance)
  45. matched_image = cv2.drawMatches(
  46. query_image_resize,
  47. query_kpts,
  48. ref_image_resize,
  49. ref_kpts,
  50. matches[:50],
  51. None,
  52. flags=2,
  53. )
  54. wrap_image = cv.warpPerspective(query_image_resize, M, (ref_image_resize.shape[1] + query_image_resize.shape[1], ref_image_resize.shape[0]))
  55. wrap_image = cv2.resize(wrap_image,(ref_image.shape[1]+query_image.shape[1], ref_image.shape[0]))
  56. # wrap_filename = f"wrap_image_{idx1}_{idx2}_{iteration}.jpg"
  57. # cv2.imwrite(os.path.join(demo_image_path, wrap_filename), wrap_image)
  58. # cv2.imwrite(os.path.join(demo_image_path + f"wrap_image.jpg"), wrap_image)
  59. wrap_image[0:ref_image.shape[0], 0: ref_image.shape[1]] =ref_image
  60. cv2.imwrite("image2.jpg", wrap_image)
  61. return wrap_image
  62. if __name__ == "__main__":
  63. registration_demo()