convert.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. from argparse import ArgumentParser
  13. import shutil
  14. # This Python script is based on the shell converter script provided in the MipNerF 360 repository.
  15. parser = ArgumentParser("Colmap converter")
  16. parser.add_argument("--no_gpu", action='store_true')
  17. parser.add_argument("--source_path", "-s", required=True, type=str)
  18. parser.add_argument("--camera", default="OPENCV", type=str)
  19. parser.add_argument("--colmap_executable", default="", type=str)
  20. parser.add_argument("--resize", action="store_true")
  21. parser.add_argument("--magick_executable", default="", type=str)
  22. args = parser.parse_args()
  23. colmap_command = '"{}"'.format(args.colmap_executable) if len(args.colmap_executable) > 0 else "colmap"
  24. magick_command = '"{}"'.format(args.magick_executable) if len(args.magick_executable) > 0 else "magick"
  25. use_gpu = 1 if not args.no_gpu else 0
  26. os.makedirs(args.source_path + "/distorted/sparse", exist_ok=True)
  27. ## Feature extraction
  28. os.system(colmap_command + " feature_extractor "\
  29. "--database_path " + args.source_path + "/distorted/database.db \
  30. --image_path " + args.source_path + "/input \
  31. --ImageReader.single_camera 1 \
  32. --ImageReader.camera_model " + args.camera + " \
  33. --SiftExtraction.use_gpu " + str(use_gpu))
  34. ## Feature matching
  35. os.system(colmap_command + " exhaustive_matcher \
  36. --database_path " + args.source_path + "/distorted/database.db \
  37. --SiftMatching.use_gpu " + str(use_gpu))
  38. ### Bundle adjustment
  39. # The default Mapper tolerance is unnecessarily large,
  40. # decreasing it speeds up bundle adjustment steps.
  41. os.system(colmap_command + " mapper \
  42. --database_path " + args.source_path + "/distorted/database.db \
  43. --image_path " + args.source_path + "/input \
  44. --output_path " + args.source_path + "/distorted/sparse \
  45. --Mapper.ba_global_function_tolerance=0.000001")
  46. ### Image undistortion
  47. ## We need to undistort our images into ideal pinhole intrinsics.
  48. os.system(colmap_command + " image_undistorter \
  49. --image_path " + args.source_path + "/input \
  50. --input_path " + args.source_path + "/distorted/sparse/0 \
  51. --output_path " + args.source_path + "\
  52. --output_type COLMAP")
  53. files = os.listdir(args.source_path + "/sparse")
  54. os.makedirs(args.source_path + "/sparse/0", exist_ok=True)
  55. # Copy each file from the source directory to the destination directory
  56. for file in files:
  57. if file == '0':
  58. continue
  59. source_file = os.path.join(args.source_path, "sparse", file)
  60. destination_file = os.path.join(args.source_path, "sparse", "0", file)
  61. shutil.move(source_file, destination_file)
  62. if(args.resize):
  63. print("Copying and resizing...")
  64. # Resize images.
  65. os.makedirs(args.source_path + "/images_2", exist_ok=True)
  66. os.makedirs(args.source_path + "/images_4", exist_ok=True)
  67. os.makedirs(args.source_path + "/images_8", exist_ok=True)
  68. # Get the list of files in the source directory
  69. files = os.listdir(args.source_path + "/images")
  70. # Copy each file from the source directory to the destination directory
  71. for file in files:
  72. source_file = os.path.join(args.source_path, "images", file)
  73. destination_file = os.path.join(args.source_path, "images_2", file)
  74. shutil.copy2(source_file, destination_file)
  75. os.system(magick_command + " mogrify -resize 50% " + destination_file)
  76. destination_file = os.path.join(args.source_path, "images_4", file)
  77. shutil.copy2(source_file, destination_file)
  78. os.system(magick_command + " mogrify -resize 25% " + destination_file)
  79. destination_file = os.path.join(args.source_path, "images_8", file)
  80. shutil.copy2(source_file, destination_file)
  81. os.system(magick_command + " mogrify -resize 12.5% " + destination_file)
  82. print("Done.")