| 1 |
#include "colmap/controllers/image_reader.h" |
| 2 |
#include "colmap/controllers/undistorters.h" |
| 3 |
#include "colmap/exe/feature.h" |
| 4 |
#include "colmap/image/undistortion.h" |
| 5 |
#include "colmap/scene/camera.h" |
| 6 |
#include "colmap/scene/reconstruction.h" |
| 7 |
#include "colmap/util/base_controller.h" |
| 8 |
#include "colmap/util/file.h" |
| 9 |
#include "colmap/util/logging.h" |
| 10 |
|
| 11 |
#include "pycolmap/helpers.h" |
| 12 |
#include "pycolmap/pybind11_extension.h" |
| 13 |
|
| 14 |
#include <filesystem> |
| 15 |
#include <memory> |
| 16 |
|
| 17 |
#include <glog/logging.h> |
| 18 |
#include <pybind11/pybind11.h> |
| 19 |
#include <pybind11/stl.h> |
| 20 |
|
| 21 |
using namespace colmap; |
| 22 |
using namespace pybind11::literals; |
| 23 |
namespace py = pybind11; |
| 24 |
|
| 25 |
void ImportImages(const std::filesystem::path& database_path, |
| 26 |
const std::filesystem::path& image_path, |
| 27 |
const CameraMode camera_mode, |
| 28 |
const std::vector<std::string>& image_names, |
| 29 |
const ImageReaderOptions& options_) { |
| 30 |
THROW_CHECK_FILE_EXISTS(database_path); |
| 31 |
THROW_CHECK_DIR_EXISTS(image_path); |
| 32 |
|
| 33 |
ImageReaderOptions options(options_); |
| 34 |
options.image_path = image_path; |
| 35 |
options.image_names = image_names; |
| 36 |
UpdateImageReaderOptionsFromCameraMode(options, camera_mode); |
| 37 |
|
| 38 |
auto database = Database::Open(database_path); |
| 39 |
ImageReader image_reader(options, database.get()); |
| 40 |
|
| 41 |
PyInterrupt py_interrupt(2.0); |
| 42 |
|
| 43 |
while (image_reader.NextIndex() < image_reader.NumImages()) { |
| 44 |
if (py_interrupt.Raised()) { |
| 45 |
throw py::error_already_set(); |
| 46 |
} |
| 47 |
Rig rig; |
| 48 |
Camera camera; |
| 49 |
Image image; |
| 50 |
PosePrior pose_prior; |
| 51 |
Bitmap bitmap; |
| 52 |
const ImageReader::Status status = |
| 53 |
image_reader.Next(&rig, &camera, &image, &pose_prior, &bitmap, nullptr); |
| 54 |
if (status != ImageReader::Status::SUCCESS) { |
| 55 |
LOG(ERROR) << image.Name() << " " << ImageReader::StatusToString(status); |
| 56 |
continue; |
| 57 |
} |
| 58 |
DatabaseTransaction database_transaction(database.get()); |
| 59 |
if (image.ImageId() == kInvalidImageId) { |
| 60 |
image.SetImageId(database->WriteImage(image)); |
| 61 |
|
| 62 |
Frame frame; |
| 63 |
frame.SetRigId(rig.RigId()); |
| 64 |
frame.AddDataId(image.DataId()); |
| 65 |
|
| 66 |
if (pose_prior.HasPosition() || pose_prior.HasGravity()) { |
| 67 |
pose_prior.corr_data_id = image.DataId(); |
| 68 |
database->WritePosePrior(pose_prior); |
| 69 |
} |
| 70 |
|
| 71 |
database->WriteFrame(frame); |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
Camera InferCameraFromImage(const std::filesystem::path& image_path, |
| 77 |
const ImageReaderOptions& options) { |
| 78 |
Bitmap bitmap; |
| 79 |
THROW_CHECK_FILE_EXISTS(image_path); |
| 80 |
THROW_CHECK(bitmap.Read(image_path, false)) |
| 81 |
<< "Cannot read image file: " << image_path; |
| 82 |
|
| 83 |
const std::optional<double> maybe_focal_length = bitmap.ExifFocalLength(); |
| 84 |
const double focal_length = |
| 85 |
maybe_focal_length.value_or(options.default_focal_length_factor * |
| 86 |
std::max(bitmap.Width(), bitmap.Height())); |
| 87 |
Camera camera = Camera::CreateFromModelName(kInvalidCameraId, |
| 88 |
options.camera_model, |
| 89 |
focal_length, |
| 90 |
bitmap.Width(), |
| 91 |
bitmap.Height()); |
| 92 |
camera.has_prior_focal_length = maybe_focal_length.has_value(); |
| 93 |
THROW_CHECK(camera.VerifyParams()) |
| 94 |
<< "Invalid camera params: " << camera.ParamsToString(); |
| 95 |
|
| 96 |
return camera; |
| 97 |
} |
| 98 |
|
| 99 |
void UndistortImages( |
| 100 |
const std::filesystem::path& output_path, |
| 101 |
const std::filesystem::path& input_path, |
| 102 |
const std::filesystem::path& image_path, |
| 103 |
const std::vector<std::string>& image_names, |
| 104 |
const std::string& output_type, |
| 105 |
const FileCopyType copy_type, |
| 106 |
const int num_patch_match_src_images, |
| 107 |
const UndistortCameraOptions& undistort_camera_options, |
| 108 |
int jpeg_quality, |
| 109 |
int num_threads, |
| 110 |
const std::shared_ptr<CancellationToken>& cancellation_token) { |
| 111 |
THROW_CHECK_DIR_EXISTS(image_path); |
| 112 |
CreateDirIfNotExists(output_path); |
| 113 |
|
| 114 |
if (output_type != "COLMAP") { |
| 115 |
LOG_IF(WARNING, !image_names.empty()) |
| 116 |
<< "Ignoring `image_names` for output type " << output_type; |
| 117 |
LOG_IF(WARNING, num_patch_match_src_images != -1) |
| 118 |
<< "Ignoring `num_patch_match_src_images` for output type " |
| 119 |
<< output_type; |
| 120 |
} |
| 121 |
|
| 122 |
Reconstruction reconstruction; |
| 123 |
reconstruction.Read(input_path); |
| 124 |
LOG(INFO) << StringPrintf(" => Reconstruction with %d images and %d points", |
| 125 |
reconstruction.NumImages(), |
| 126 |
reconstruction.NumPoints3D()); |
| 127 |
|
| 128 |
PyInterruptChecker interrupt_checker(cancellation_token); |
| 129 |
std::unique_ptr<BaseController> undistorter; |
| 130 |
if (output_type == "COLMAP") { |
| 131 |
COLMAPUndistorter::Options options; |
| 132 |
options.copy_type = copy_type; |
| 133 |
if (num_patch_match_src_images > 0) { |
| 134 |
options.num_patch_match_src_images = num_patch_match_src_images; |
| 135 |
} |
| 136 |
options.jpeg_quality = jpeg_quality; |
| 137 |
options.num_threads = num_threads; |
| 138 |
options.image_ids.reserve(image_names.size()); |
| 139 |
for (const auto& image_name : image_names) { |
| 140 |
const Image* image = reconstruction.FindImageWithName(image_name); |
| 141 |
if (image != nullptr) { |
| 142 |
options.image_ids.push_back(image->ImageId()); |
| 143 |
} else { |
| 144 |
LOG(WARNING) << "Cannot find image " << image_name; |
| 145 |
} |
| 146 |
} |
| 147 |
undistorter = std::make_unique<COLMAPUndistorter>(std::move(options), |
| 148 |
undistort_camera_options, |
| 149 |
reconstruction, |
| 150 |
image_path, |
| 151 |
output_path); |
| 152 |
} else if (output_type == "PMVS") { |
| 153 |
PMVSUndistorter::Options pmvs_options; |
| 154 |
pmvs_options.jpeg_quality = jpeg_quality; |
| 155 |
pmvs_options.num_threads = num_threads; |
| 156 |
undistorter = std::make_unique<PMVSUndistorter>(pmvs_options, |
| 157 |
undistort_camera_options, |
| 158 |
reconstruction, |
| 159 |
image_path, |
| 160 |
output_path); |
| 161 |
} else if (output_type == "CMP-MVS") { |
| 162 |
CMPMVSUndistorter::Options cmpmvs_options; |
| 163 |
cmpmvs_options.jpeg_quality = jpeg_quality; |
| 164 |
cmpmvs_options.num_threads = num_threads; |
| 165 |
undistorter = std::make_unique<CMPMVSUndistorter>(cmpmvs_options, |
| 166 |
undistort_camera_options, |
| 167 |
reconstruction, |
| 168 |
image_path, |
| 169 |
output_path); |
| 170 |
} else { |
| 171 |
LOG(FATAL_THROW) |
| 172 |
<< "Invalid `output_type` - supported values are {'COLMAP', " |
| 173 |
"'PMVS', 'CMP-MVS'}."; |
| 174 |
} |
| 175 |
|
| 176 |
py::gil_scoped_release release; |
| 177 |
undistorter->SetCheckIfStoppedFunc(interrupt_checker.Callback()); |
| 178 |
undistorter->Run(); |
| 179 |
interrupt_checker.CheckAndThrow(); |
| 180 |
} |
| 181 |
|
| 182 |
void BindImages(py::module& m) { |
| 183 |
auto PyCameraMode = py::enum_<CameraMode>(m, "CameraMode") |
| 184 |
.value("AUTO", CameraMode::AUTO) |
| 185 |
.value("SINGLE", CameraMode::SINGLE) |
| 186 |
.value("PER_FOLDER", CameraMode::PER_FOLDER) |
| 187 |
.value("PER_IMAGE", CameraMode::PER_IMAGE); |
| 188 |
AddStringToEnumConstructor(PyCameraMode); |
| 189 |
|
| 190 |
using IROpts = ImageReaderOptions; |
| 191 |
auto PyImageReaderOptions = |
| 192 |
py::classh<IROpts>(m, "ImageReaderOptions") |
| 193 |
.def(py::init<>()) |
| 194 |
.def_readwrite("camera_model", |
| 195 |
&IROpts::camera_model, |
| 196 |
"Name of the camera model.") |
| 197 |
.def_readwrite("mask_path", |
| 198 |
&IROpts::mask_path, |
| 199 |
"Optional root path to folder which contains image" |
| 200 |
"masks. For a given image, the corresponding mask" |
| 201 |
"must have the same sub-path below this root as the" |
| 202 |
"image has below image_path. The filename must be" |
| 203 |
"equal, aside from the added extension .png. " |
| 204 |
"For example, for an image image_path/abc/012.jpg," |
| 205 |
"the mask would be mask_path/abc/012.jpg.png. No" |
| 206 |
"features will be extracted in regions where the" |
| 207 |
"mask image is black (pixel intensity value 0 in" |
| 208 |
"grayscale).") |
| 209 |
.def_readwrite("existing_camera_id", |
| 210 |
&IROpts::existing_camera_id, |
| 211 |
"Whether to explicitly use an existing camera for " |
| 212 |
"all images. Note that in this case the specified " |
| 213 |
"camera model and parameters are ignored.") |
| 214 |
.def_readwrite("camera_params", |
| 215 |
&IROpts::camera_params, |
| 216 |
"Manual specification of camera parameters. If " |
| 217 |
"empty, camera parameters will be extracted from " |
| 218 |
"EXIF, i.e. principal point and focal length.") |
| 219 |
.def_readwrite( |
| 220 |
"default_focal_length_factor", |
| 221 |
&IROpts::default_focal_length_factor, |
| 222 |
"If camera parameters are not specified manually and the image " |
| 223 |
"does not have focal length EXIF information, the focal length " |
| 224 |
"is set to the value `default_focal_length_factor * max(width, " |
| 225 |
"height)`.") |
| 226 |
.def_readwrite( |
| 227 |
"camera_mask_path", |
| 228 |
&IROpts::camera_mask_path, |
| 229 |
"Optional path to an image file specifying a mask for all " |
| 230 |
"images. No features will be extracted in regions where the " |
| 231 |
"mask is black (pixel intensity value 0 in grayscale)") |
| 232 |
.def("check", &IROpts::Check); |
| 233 |
MakeDataclass(PyImageReaderOptions); |
| 234 |
|
| 235 |
auto PyFileCopyType = py::enum_<FileCopyType>(m, "FileCopyType") |
| 236 |
.value("copy", FileCopyType::COPY) |
| 237 |
.value("softlink", FileCopyType::SOFT_LINK) |
| 238 |
.value("hardlink", FileCopyType::HARD_LINK); |
| 239 |
AddStringToEnumConstructor(PyFileCopyType); |
| 240 |
|
| 241 |
m.def("import_images", |
| 242 |
&ImportImages, |
| 243 |
"database_path"_a, |
| 244 |
"image_path"_a, |
| 245 |
"camera_mode"_a = CameraMode::AUTO, |
| 246 |
"image_names"_a = std::vector<std::string>(), |
| 247 |
py::arg_v("options", ImageReaderOptions(), "ImageReaderOptions()"), |
| 248 |
"Import images into a database"); |
| 249 |
|
| 250 |
m.def("infer_camera_from_image", |
| 251 |
&InferCameraFromImage, |
| 252 |
"image_path"_a, |
| 253 |
py::arg_v("options", ImageReaderOptions(), "ImageReaderOptions()"), |
| 254 |
"Guess the camera parameters from the EXIF metadata"); |
| 255 |
|
| 256 |
m.def("undistort_images", |
| 257 |
&UndistortImages, |
| 258 |
"output_path"_a, |
| 259 |
"input_path"_a, |
| 260 |
"image_path"_a, |
| 261 |
"image_names"_a = std::vector<std::string>(), |
| 262 |
"output_type"_a = "COLMAP", |
| 263 |
"copy_policy"_a = FileCopyType::COPY, |
| 264 |
"num_patch_match_src_images"_a = -1, |
| 265 |
py::arg_v("undistort_options", |
| 266 |
UndistortCameraOptions(), |
| 267 |
"UndistortCameraOptions()"), |
| 268 |
"jpeg_quality"_a = -1, |
| 269 |
"num_threads"_a = -1, |
| 270 |
"cancellation_token"_a = py::none(), |
| 271 |
"Undistort images"); |
| 272 |
} |