%load_ext autoreload
%autoreload 2
from pathlib import Path
from hloc import extract_features, match_features, reconstruction, visualization
In this notebook, we will run SfM reconstruction from scratch on a set of images. We choose the South-Building dataset - we will download it later. First, we define some paths.
dataset = Path('datasets/sfm_South-Building/')
images = dataset / 'South-Building/images/'
outputs = Path('outputs/sfm/')
sfm_pairs = outputs / 'pairs-exhaustive.txt' # exhaustive matching
sfm_dir = outputs / 'sfm_superpoint+superglue'
feature_conf = extract_features.confs['superpoint_aachen']
matcher_conf = match_features.confs['superglue']
features = feature_conf['output']
feature_file = f"{features}.h5"
match_file = f"{features}_{matcher_conf['output']}_{sfm_pairs.stem}.h5"
The dataset is simply a set of images. The intrinsic parameters will be extracted from the EXIF data, and refined with SfM.
%%bash -s "$dataset"
wget http://cvg.ethz.ch/research/local-feature-evaluation/South-Building.zip -P $1
unzip $1/South-Building.zip -d $1
extract_features.main(feature_conf, images, outputs)
Since the dataset is small, we can match all $\frac{n(n-1)}{2}$ images pairs. To do so, we pass the argument exhaustive=True
and make sure that the pair file does not exist yet. If your dataset is larger, exhaustive matching might take a long time - consider selecting fewer pairs using image retrieval and hloc/pairs_from_retrieval.py
.
match_features.main(matcher_conf, sfm_pairs, features, outputs, exhaustive=True)
Run COLMAP on the features and matches.
reconstruction.main(
sfm_dir,
images,
sfm_pairs,
outputs / feature_file,
outputs / match_file)
We visualize some of the registered images, and color their keypoint by visibility, track length, or triangulated depth.
visualization.visualize_sfm_2d(sfm_dir / 'models/0', images, color_by='visibility', n=5)
visualization.visualize_sfm_2d(sfm_dir / 'models/0', images, color_by='track_length', n=5)
visualization.visualize_sfm_2d(sfm_dir / 'models/0', images, color_by='depth', n=5)