cvg / Hierarchical-Localization

Visual localization made easy with hloc
Apache License 2.0
3.14k stars 582 forks source link

> 1. Create an empty reconstruction with pycolmap: add images and cameras but no 3D points #293

Open kaiyi98 opened 1 year ago

kaiyi98 commented 1 year ago
          > 1. Create an empty reconstruction with pycolmap: add images and cameras but no 3D points
rec = pycolmap.Reconstruction()
camera_id = 1
cam1 = pycolmap.Camera(model_name, w, h, params, camera_id)
rec.add_camera(cam1)
im1 = pycolmap.Image(image_name, [], tvec, qvec, camera_id, id=1)
rec.add_image(im1)
rec.write("path/")
  1. Save it to disk
  2. Run hloc.triangulation instead of hloc.reconstruction

hello! What do the "model_name", "params" and "camera_id" mean? params is the camera intrinsic matrix? If I have a sequence images from one camera and know pose, should I to reconstruction.

Originally posted by @kaiyi98 in https://github.com/cvg/Hierarchical-Localization/issues/222#issuecomment-1639851499

iperper commented 1 year ago

The Camera parameters refer to different camera models / params for COLMAP. Each one is described here.

For example, to create a Simple Radial camera, you can do: pycolmap.Camera('SIMPLE_RADIAL', w, h, [f, cx, cy, k], camera_id)

camera_id is just an integer id of a camera in the COLMAP model. Each camera can be used for 1 or more images.

If you have a sequence of images from one camera, you should pass that into the reconstruction.main using

ctorti commented 1 month ago

@iperper Thanks for the guidance.

I have provided the optional args camera_mode and image_options to reconstruction.main as above. And while I can vary the values for camera_mode and camera_model and get reconstructions that honour those values, it appears that camera_params is being ignored (i.e. the resulting camera_params in the model are wildly different from the ones set in image_options).

At first, I thought that might be happening because pycolmap doesn't yet have bindings that allow for ImageReaderOptions.camera_params to be accessible during reconstruction: https://github.com/colmap/pycolmap/issues/61

That post is a bit old, so I searched the pycolmap source code to establish whether or not the bindings have been added since then. As far as I can tell, ImportImages is using ImageReaderOptions: https://github.com/colmap/pycolmap/blob/master/pycolmap/pipeline/images.h

Can anyone confirm whether that is the case?

If not, I'm guessing I'll need to modify the database manually, replacing reconstruction.import_images with some customised code?

I'd appreciate any pointers or sample code that I can add into the pipeline with minimal fuss.

iperper commented 1 month ago

@ctorti

Assuming you are doing incremental reconstruction, the camera parameters are still further refined during bundle adjustment. See IncrementalMapperOptions here.

One situation where you'd see the behavior of camera_params being significantly different from image_options input is if the input parameters are incorrect. BA will generally estimate these well (if you have a lot of images), so I would first investigate how good your custom intrinsics are.

Alternatively, the reconstruction could be failing for some reason. If you are confident in you camera instrinics and model, you could try setting ba_refine_focal_length and ba_refine_extra_params to false.

I haven't worked with this directly in a while, so I would look around a other discussions that mention these settings or fixed / custom camera instrinsics.

ctorti commented 1 month ago

@iperper Thanks very much for your support. Setting ba_refine_focal_length and ba_refine_extra_params to False has done the trick!