convert.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #
  2. # Copyright (C) 2023, Inria
  3. # GRAPHDECO research group, https://team.inria.fr/graphdeco
  4. # All rights reserved.
  5. #
  6. # This software is free for non-commercial, research and evaluation use
  7. # under the terms of the LICENSE.md file.
  8. #
  9. # For inquiries contact george.drettakis@inria.fr
  10. #
  11. import os
  12. import logging
  13. from argparse import ArgumentParser
  14. import shutil
  15. # This Python script is based on the shell converter script provided in the MipNerF 360 repository.
  16. parser = ArgumentParser("Colmap converter")
  17. parser.add_argument("--no_gpu", action='store_true')
  18. parser.add_argument("--skip_matching", action='store_true')
  19. parser.add_argument("--source_path", "-s", required=True, type=str)
  20. parser.add_argument("--camera", default="OPENCV", type=str)
  21. # 获取当前工作路径
  22. current_path = os.getcwd()
  23. colmap_path = os.path.join(current_path, 'external', r'COLMAP-3.8-windows-cuda\colmap.bat')
  24. # colmap_path = os.path.join(current_path, 'external', r'COLMAP-3.7-windows-no-cuda\colmap.bat')
  25. magick_path = os.path.join(current_path, 'external', r'ImageMagick-7.1.1-Q16-HDRI\magick.exe')
  26. parser.add_argument("--colmap_executable", default=colmap_path, type=str)
  27. parser.add_argument("--resize", action="store_true")
  28. parser.add_argument("--magick_executable", default=magick_path, type=str)
  29. args = parser.parse_args()
  30. colmap_command = '"{}"'.format(args.colmap_executable) if len(args.colmap_executable) > 0 else "colmap"
  31. magick_command = '"{}"'.format(args.magick_executable) if len(args.magick_executable) > 0 else "magick"
  32. use_gpu = 1 if not args.no_gpu else 0
  33. if not args.skip_matching:
  34. os.makedirs(args.source_path + "/distorted/sparse", exist_ok=True)
  35. ## Feature extraction
  36. feat_extracton_cmd = colmap_command + " feature_extractor " \
  37. "--database_path " + args.source_path + "/distorted/database.db \
  38. --image_path " + args.source_path + "/input \
  39. --ImageReader.single_camera 1 \
  40. --ImageReader.camera_model " + args.camera + " \
  41. --SiftExtraction.use_gpu " + str(use_gpu)
  42. exit_code = os.system(feat_extracton_cmd)
  43. if exit_code != 0:
  44. logging.error(f"Feature extraction failed with code {exit_code}. Exiting.")
  45. exit(exit_code)
  46. ## Feature matching
  47. feat_matching_cmd = colmap_command + " exhaustive_matcher \
  48. --database_path " + args.source_path + "/distorted/database.db \
  49. --SiftMatching.use_gpu " + str(use_gpu)
  50. exit_code = os.system(feat_matching_cmd)
  51. if exit_code != 0:
  52. logging.error(f"Feature matching failed with code {exit_code}. Exiting.")
  53. exit(exit_code)
  54. ### Bundle adjustment
  55. # The default Mapper tolerance is unnecessarily large,
  56. # decreasing it speeds up bundle adjustment steps.
  57. mapper_cmd = (colmap_command + " mapper \
  58. --database_path " + args.source_path + "/distorted/database.db \
  59. --image_path " + args.source_path + "/input \
  60. --output_path " + args.source_path + "/distorted/sparse \
  61. --Mapper.ba_global_function_tolerance=0.000001")
  62. exit_code = os.system(mapper_cmd)
  63. if exit_code != 0:
  64. logging.error(f"Mapper failed with code {exit_code}. Exiting.")
  65. exit(exit_code)
  66. ### Image undistortion
  67. ## We need to undistort our images into ideal pinhole intrinsics.
  68. img_undist_cmd = (colmap_command + " image_undistorter \
  69. --image_path " + args.source_path + "/input \
  70. --input_path " + args.source_path + "/distorted/sparse/0 \
  71. --output_path " + args.source_path + "\
  72. --output_type COLMAP")
  73. exit_code = os.system(img_undist_cmd)
  74. if exit_code != 0:
  75. logging.error(f"Mapper failed with code {exit_code}. Exiting.")
  76. exit(exit_code)
  77. files = os.listdir(args.source_path + "/sparse")
  78. os.makedirs(args.source_path + "/sparse/0", exist_ok=True)
  79. # Copy each file from the source directory to the destination directory
  80. for file in files:
  81. if file == '0':
  82. continue
  83. source_file = os.path.join(args.source_path, "sparse", file)
  84. destination_file = os.path.join(args.source_path, "sparse", "0", file)
  85. shutil.move(source_file, destination_file)
  86. if (args.resize):
  87. print("Copying and resizing...")
  88. # Resize images.
  89. os.makedirs(args.source_path + "/images_2", exist_ok=True)
  90. os.makedirs(args.source_path + "/images_4", exist_ok=True)
  91. os.makedirs(args.source_path + "/images_8", exist_ok=True)
  92. # Get the list of files in the source directory
  93. files = os.listdir(args.source_path + "/images")
  94. # Copy each file from the source directory to the destination directory
  95. for file in files:
  96. source_file = os.path.join(args.source_path, "images", file)
  97. destination_file = os.path.join(args.source_path, "images_2", file)
  98. shutil.copy2(source_file, destination_file)
  99. exit_code = os.system(magick_command + " mogrify -resize 50% " + destination_file)
  100. if exit_code != 0:
  101. logging.error(f"50% resize failed with code {exit_code}. Exiting.")
  102. exit(exit_code)
  103. destination_file = os.path.join(args.source_path, "images_4", file)
  104. shutil.copy2(source_file, destination_file)
  105. exit_code = os.system(magick_command + " mogrify -resize 25% " + destination_file)
  106. if exit_code != 0:
  107. logging.error(f"25% resize failed with code {exit_code}. Exiting.")
  108. exit(exit_code)
  109. destination_file = os.path.join(args.source_path, "images_8", file)
  110. shutil.copy2(source_file, destination_file)
  111. exit_code = os.system(magick_command + " mogrify -resize 12.5% " + destination_file)
  112. if exit_code != 0:
  113. logging.error(f"12.5% resize failed with code {exit_code}. Exiting.")
  114. exit(exit_code)
  115. print("Done.")