Skip to content

Instantly share code, notes, and snippets.

@nariyu
Created September 18, 2024 07:06
Show Gist options
  • Save nariyu/2b4120abee728f0ce00dbe391b7df9fe to your computer and use it in GitHub Desktop.
Save nariyu/2b4120abee728f0ce00dbe391b7df9fe to your computer and use it in GitHub Desktop.
colmap
import os
import shutil
import logging
from argparse import ArgumentParser
import shutil
import re
# This Python script is based on the shell converter script provided in the MipNerF 360 repository.
parser = ArgumentParser("Colmap converter")
parser.add_argument("--no_gpu", action='store_true')
parser.add_argument("--skip_matching", action='store_true')
parser.add_argument("--source_path", "-s", type=str, required=True)
parser.add_argument("--colmap_executable", type=str, default="")
parser.add_argument("--resize", action="store_true")
parser.add_argument("--magick_executable", type=str, default="")
parser.add_argument("--camera_posi_path", type=str)
parser.add_argument("--image_size", type=int, default=1000)
parser.add_argument("--max_num_features", type=int, default=3000)
args = parser.parse_args()
colmap_command = '"{}"'.format(args.colmap_executable) if len(args.colmap_executable) > 0 else "colmap"
magick_command = '"{}"'.format(args.magick_executable) if len(args.magick_executable) > 0 else "magick"
use_gpu = 1 if not args.no_gpu else 0
if not args.skip_matching:
shutil.rmtree(args.source_path + "/images", ignore_errors=True)
shutil.rmtree(args.source_path + "/distorted", ignore_errors=True)
shutil.rmtree(args.source_path + "/sparse", ignore_errors=True)
shutil.rmtree(args.source_path + "/stereo", ignore_errors=True)
shutil.rmtree(args.source_path + "/distorted", ignore_errors=True)
shutil.rmtree(args.source_path + "/distorted", ignore_errors=True)
shutil.rmtree(args.source_path + "/distorted", ignore_errors=True)
if os.path.exists(args.source_path + "/run-colmap-geometric.sh"):
os.remove(args.source_path + "/run-colmap-geometric.sh")
if os.path.exists(args.source_path + "/run-colmap-photometric.sh"):
os.remove(args.source_path + "/run-colmap-photometric.sh")
os.makedirs(args.source_path + "/distorted/sparse", exist_ok=True)
## Feature extraction
feat_extracton_cmd = colmap_command + " feature_extractor \
--database_path " + args.source_path + "/distorted/database.db \
--image_path " + args.source_path + "/input \
--ImageReader.single_camera 1 \
--ImageReader.camera_model OPENCV \
--SiftExtraction.max_image_size " + str(args.image_size) + " \
--SiftExtraction.max_num_features " + str(args.max_num_features) + " \
--SiftExtraction.use_gpu " + str(use_gpu)
exit_code = os.system(feat_extracton_cmd)
if exit_code != 0:
logging.error(f"Feature extraction failed with code {exit_code}. Exiting.")
exit(exit_code)
## Feature matching
feat_matching_cmd = colmap_command + " exhaustive_matcher \
--database_path " + args.source_path + "/distorted/database.db \
--SiftMatching.use_gpu " + str(use_gpu)
exit_code = os.system(feat_matching_cmd)
if exit_code != 0:
logging.error(f"Feature matching failed with code {exit_code}. Exiting.")
exit(exit_code)
### Bundle adjustment
# The default Mapper tolerance is unnecessarily large,
# decreasing it speeds up bundle adjustment steps.
mapper_cmd = (colmap_command + " mapper \
--database_path " + args.source_path + "/distorted/database.db \
--image_path " + args.source_path + "/input \
--output_path " + args.source_path + "/distorted/sparse \
--Mapper.ba_global_function_tolerance=0.000001")
exit_code = os.system(mapper_cmd)
if exit_code != 0:
logging.error(f"Mapper failed with code {exit_code}. Exiting.")
exit(exit_code)
### Align camera position
if args.camera_posi_path:
f = open(args.camera_posi_path, 'r')
data = f.read()
f.close()
f = open(args.camera_posi_path, 'w')
f.write(re.sub('#.+\n', "", data).replace(",", " "))
f.close()
print("==============================================================================")
print("Align camera position")
print("==============================================================================")
print(data)
aliner_cmd = (colmap_command + " model_aligner \
--input_path " + args.source_path + "/distorted/sparse/0 \
--output_path " + args.source_path + "/distorted/sparse/0 \
--ref_images_path " + args.camera_posi_path + " \
--ref_is_gps 0 \
--alignment_max_error 1")
exit_code = os.system(aliner_cmd)
if exit_code != 0:
logging.error(f"Alignment failed with code {exit_code}. Exiting.")
exit(exit_code)
### Image undistortion
## We need to undistort our images into ideal pinhole intrinsics.
img_undist_cmd = (colmap_command + " image_undistorter \
--image_path " + args.source_path + "/input \
--input_path " + args.source_path + "/distorted/sparse/0 \
--output_path " + args.source_path + "\
--max_image_size " + str(args.image_size) + " \
--output_type COLMAP")
exit_code = os.system(img_undist_cmd)
if exit_code != 0:
logging.error(f"Mapper failed with code {exit_code}. Exiting.")
exit(exit_code)
files = os.listdir(args.source_path + "/sparse")
os.makedirs(args.source_path + "/sparse/0", exist_ok=True)
# Copy each file from the source directory to the destination directory
for file in files:
if file == '0':
continue
source_file = os.path.join(args.source_path, "sparse", file)
destination_file = os.path.join(args.source_path, "sparse", "0", file)
shutil.move(source_file, destination_file)
if(args.resize):
print("Copying and resizing...")
# Resize images.
os.makedirs(args.source_path + "/images_2", exist_ok=True)
os.makedirs(args.source_path + "/images_4", exist_ok=True)
os.makedirs(args.source_path + "/images_8", exist_ok=True)
# Get the list of files in the source directory
files = os.listdir(args.source_path + "/images")
# Copy each file from the source directory to the destination directory
for file in files:
source_file = os.path.join(args.source_path, "images", file)
destination_file = os.path.join(args.source_path, "images_2", file)
shutil.copy2(source_file, destination_file)
exit_code = os.system(magick_command + " mogrify -resize 50% " + destination_file)
if exit_code != 0:
logging.error(f"50% resize failed with code {exit_code}. Exiting.")
exit(exit_code)
destination_file = os.path.join(args.source_path, "images_4", file)
shutil.copy2(source_file, destination_file)
exit_code = os.system(magick_command + " mogrify -resize 25% " + destination_file)
if exit_code != 0:
logging.error(f"25% resize failed with code {exit_code}. Exiting.")
exit(exit_code)
destination_file = os.path.join(args.source_path, "images_8", file)
shutil.copy2(source_file, destination_file)
exit_code = os.system(magick_command + " mogrify -resize 12.5% " + destination_file)
if exit_code != 0:
logging.error(f"12.5% resize failed with code {exit_code}. Exiting.")
exit(exit_code)
print("Done.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment