convert.py 4.1 KB

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