SphereSfM

repository·main·Indexed 43 days ago

https://github.com/json87/spheresfm

A specialized extension of ColMap for sparse and dense 3D reconstruction from spherical (equirectangular) images. Optimized for cameras such as Insta360 and Ricoh theta, it extends the ColMap SfM engine to support spherical image orientation and provides a workflow for 3D modeling in complex urban environments.

Tokens
32.4K
Snippets
96
Records
157
Agent score
55%

What's inside SphereSfM

  1. Overview of SphereSfM

    main
    SphereSfM is a software package designed for the sparse reconstruction of spherical images in EquiRectangular Projection (ERP) format using incremental Structure from Motion (SfM). It is specifically optimized for spherical cameras like Insta360 and Ricoh theta, making it suitable for 3D modeling in complex urban environments, such as streets and near-ground buildings. The project extends the ColMap SfM engine to support spherical image orientation and can be integrated into standard SfM-MVS (Structure from Motion - Multi-View Stereo) workflows.
  2. Manage shared intrinsics in COLMAP

    main
    Images share the same intrinsics if they refer to the same camera, identified by the camera_id property in the database. You can manage shared intrinsics by adding new cameras and setting these properties using the database management tool.
  3. Terminology in COLMAP

    main

    Understanding the core concepts used in COLMAP:

    • Camera: Represents a physical object with a specific zoom factor and lens. It defines the intrinsic projection model. A single camera can be used for multiple images if they share the same resolution, intrinsic parameters, and distortion characteristics.
    • Image: A bitmap file (e.g., JPEG, PNG) on disk.
    • Keypoints: Specific points detected within an image.
    • Descriptors: Numerical descriptions of the appearance of keypoints.
    • Matches: Pure appearance-based correspondences between keypoints/descriptors.
    • Inlier Matches: Matches that have been geometrically verified and are used for the actual reconstruction.
  4. Understand Sparse Reconstruction file formats

    main

    Sparse models are stored in directories containing three files: cameras, images, and points3D.

    COLMAP supports two formats:

    • Binary (.bin): Machine-readable and fast. This is the default. If a directory contains both binary and text files, the binary format is preferred.
    • Text (.txt): Human-readable but slower.

    Conversion Methods:

    • GUI: Use File > Export model (binary) or File > Export model as text (text). To convert, import the model first, then export in the desired format.
    • CLI: Use the model_converter executable.
    • Other Formats: Use File > Export as... in the GUI to export to VisualSfM's NVM, Bundler, PLY, VRML, etc.

    Programmatic Access:

    • Python: Use scripts/python/read_write_model.py (supports both binary and text).
    • Matlab: Use scripts/matlab/read_model.m (supports text).
  5. Understand Consistency Graphs format

    main

    The consistency graph defines which source images a pixel is consistent with. It is stored as a mixed text and binary file.

    • Text part: Follows the same format as depth/normal maps (with&height&channels& header + float32 binary data).
    • Binary part: A continuous list of int32 values in the format: <row><col><N><image_idx1>...<image_idxN>.

    Note that image_idx values are zero-based indices relative to the ordering in the images.txt file.

  6. Configure Cameras and Images in the database

    main

    The relationship between cameras and images is 1-to-N. One camera (containing intrinsic parameters like focal length and distortion) can be shared by multiple images. Each image has its own unique extrinsic parameters (orientation and location).

    Key Requirements:

    • Identifiers: When manually inserting data, ensure image_id > 0 and camera_id > 0.
    • Camera Parameters: Intrinsic parameters are stored as contiguous float64 binary blobs. The order of parameters depends on the camera model defined in src/base/camera_models.h.
    • Image Paths: The name column in the images table must contain the unique relative path within the image folder. This allows you to move the database and image folder simultaneously as long as the relative structure remains intact.
  7. Understand Dense Reconstruction workspace structure

    main

    A dense reconstruction workspace follows a specific directory structure:

    +── images
    │   +── image1.jpg
    │   +── image2.jpg
    │   +── ...
    +── sparse
    │   +── cameras.txt
    │   +── images.txt
    │   +── points3D.txt
    +── stereo
    │   +── consistency_graphs
    │   │   +── image1.jpg.photometric.bin
    │   │   +── ...
    │   +── depth_maps
    │   │   +── image1.jpg.photometric.bin
    │   │   +── ...
    │   +── normal_maps
    │   │   +── image1.jpg.photometric.bin
    │   │   +── ...
    │   +── patch-match.cfg
    │   +── fusion.cfg
    +── fused.ply
    +── meshed-poisson.ply
    +── meshed-delaunay.ply
    +── run-colmap-geometric.sh
    +── run-colmap-photometric.sh
    • images/: Contains undistorted images.
    • sparse/: Contains the sparse reconstruction (cameras, images, points3D).
    • stereo/: Contains stereo reconstruction results (consistency graphs, depth maps, normal maps).
    • fused.ply / mesh.ply: Results of fusion and meshing procedures.
    +── images
    │   +── image1.jpg
    │   +── image2.jpg
    │   +── ...
    +── sparse
    │   +── cameras.txt
    │   +── images.txt
    │   +── points3D.txt
    +── stereo
    │   +── consistency_graphs
    │   │   +── image1.jpg.photometric.bin
    │   │   +── image2.jpg.photometric.bin
    │   │   +── ...
    │   +── depth_maps
    │   │   +── image1.jpg.photometric.bin
    │   │   +── image2.jpg.photometric.bin
    │   │   +── ...
    │   +── normal_maps
    │   │   +── image1.jpg.photometric.bin
    │   │   +── image2.jpg.photometric.bin
    │   │   +── ...
    │   +── patch-match.cfg
    │   +── fusion.cfg
    +── fused.ply
    +── meshed-poisson.ply
    +── meshed-delaunay.ply
    +── run-colmap-geometric.sh
    +── run-colmap-photometric.sh
  8. Understand the COLMAP database schema

    main

    SphereSfM uses a single SQLite database file to store all extracted information. The database consists of the following tables:

    • cameras: Stores intrinsic parameters.
    • images: Stores image metadata and extrinsic parameters.
    • keypoints: Stores detected feature locations and geometry.
    • descriptors: Stores feature appearance descriptors.
    • matches: Stores feature matches between image pairs.
    • two_view_geometries: Stores geometric verification results used for reconstruction.

    You can interact with this database using the COLMAP GUI, the C++ database API (src/base/database.h), or the Python scripting interface (scripts/python/database.py).

  9. How Multi-View Stereo (MVS) works in COLMAP

    main

    Multi-View Stereo (MVS) is the second major phase of reconstruction. It takes the sparse output from the SfM process and computes depth and/or normal information for every pixel in the input images.

    Key steps:

    1. Depth/Normal Computation: Per-pixel information is derived from the SfM camera poses and sparse points.
    2. Fusion: Depth and normal maps from multiple images are fused in 3D to produce a dense point cloud.
    3. Surface Reconstruction: Using the fused point cloud, algorithms like (screened) Poisson surface reconstruction can be used to recover the final 3D surface geometry (mesh).
  10. How Structure-from-Motion (SfM) works in COLMAP

    main

    Structure-from-Motion (SfM) reconstructs 3D structure and camera parameters (intrinsic and extrinsic) from a series of overlapping images. COLMAP implements an incremental SfM pipeline divided into three main stages:

    1. Feature detection and extraction: Identifying keypoints in images.
    2. Feature matching and geometric verification: Finding correspondences between keypoints and verifying them geometrically.
    3. Structure and motion reconstruction: Recovering the 3D points and camera poses.

    SfM output serves as the input for Multi-View Stereo (MVS), which computes depth/normal information to produce a dense representation (point clouds and meshes).

  11. Calculate and use pair_id for matches

    main

    The matches and two_view_geometries tables use a pair_id to identify matches between two unique images. This pair_id is a row-major, linear index in the upper-triangular match matrix. This allows for efficient querying even with millions of entries.

    Pair ID Logic

    To generate a pair_id from two image IDs:

    def image_ids_to_pair_id(image_id1, image_id2):
        if image_id1 > image_id2:
            return 2147483647 * image_id2 + image_id1
        else:
            return 2147483647 * image_id1 + image_id2

    To retrieve image IDs from a pair_id:

    def pair_id_to_image_ids(pair_id):
        image_id2 = pair_id % 2147483647
        image_id1 = (pair_id - image_id2) / 2147483647
        return image_id1, image_id2

    Constraints

    • Max Images: Because pair_id relies on signed 32-bit integer math, the maximum image_id allowed is 2147483647.
    def image_ids_to_pair_id(image_id1, image_id2):
        if image_id1 > image_id2:
            return 2147483647 * image_id2 + image_id1
        else:
            return 2147483647 * image_id1 + image_id2
    
    def pair_id_to_image_ids(pair_id):
        image_id2 = pair_id % 2147483647
        image_id1 = (pair_id - image_id2) / 2147483647
        return image_id1, image_id2
  12. Quickstart with COLMAP Automatic Reconstruction

    main

    For a simple reconstruction, use the COLMAP Graphical User Interface (GUI). The automatic reconstruction tool takes a folder of input images and produces both sparse and dense reconstructions in a specified workspace folder.

    1. Start the COLMAP GUI.
    2. Navigate to Reconstruction > Automatic Reconstruction.
    3. Specify your input images folder and a workspace folder.
    4. Run the tool.

    Output Structure Example: If your workspace is path/to/project and images are in path/to/project/images, the output will look like this:

    +── images
    │   +── image1.jpg
    │   +── image2.jpg
    │   +── ...
    +── sparse
    │   +── 0
    │   │   +── cameras.bin
    │   │   +── images.bin
    │   │   +── points3D.bin
    │   +── ...
    +── dense
    │   +── 0
    │   │   +── images
    │   │   +── sparse
    │   │   +── stereo
    │   │   +── fused.ply
    │   │   +── meshed-poisson.ply
    │   │   +── meshed-delaunay.ply
    │   +── ...
    +── database.db
    • Sparse Models: Located in sparse/ (contains .bin files).
    • Dense Models: Located in dense/ (contains point clouds and meshes).
    • Importing: You can import the dense point cloud (fused.ply) via File > Import model from ... in COLMAP. Dense meshes should be viewed in external tools like Meshlab.
    # No specific command provided, but describes GUI workflow:
    # Reconstruction > Automatic Reconstruction
    ```text
    +── images
    │   +── image1.jpg
    +── sparse
    │   +── 0
    │   │   +── cameras.bin
    │   │   +── images.bin
    │   │   +── points3D.bin
    +── dense
    │   +── 0
    │   │   +── fused.ply
    +── database.db