harish-vnkt / structure-from-motion

A simplified version of incremental structure from motion to reconstruct the 3D structure of a scene from a sequence of 2D images
MIT License
80 stars 19 forks source link

3D Reconstruction using Structure from Motion

This repository contains code to reconstruct a scene using structure from motion (SfM). SfM is a technique to recover 3D structure of a scene by making use of a sequence of 2D images. In the process, the technique also recovers the relative pose of a particular view (an image taken by a camera) with respect to the first view in the sequence. This is a personal project.

The process consists of a series of steps:

  1. Extract keypoints and feature descriptors from images
  2. Match features between images
  3. Find a suitable baseline (initial two views) to kickstart the reconstruction
  4. Recover pose of the baseline
  5. Reconstruct 3D points
  6. Recover pose of the next view using Perspective-n-Point
  7. Reconstruct the next set of points
  8. Perform bundle adjustment
  9. Plot points

Requirements

Images

The code has been run on openMVG's benchmark images. The images come with the intrinsic matrix of the camera as a txt file in the images folder. This code has not been tested on images taken on my camera. Users choosing to do so need to calibrate their camera to obtain the intrinsic matrix required for this code. Follow this tutorial to calibrate your camera.

Running the code

The entry point of the module is the main.py file. This file requires three arguments -

For example, if you have downloaded the benchmark images inside the repository root, then the command would be -

python main.py --root_dir ./SfM_quality_evaluation/Benchmarking_Camera_Calibration_2008/entry-P10 --feat_type surf

Or:

python main.py --root_dir ./SfM_quality_evaluation/Benchmarking_Camera_Calibration_2008/fountain-P11 --feat_type sift

Each of the above mentioned folders have an images/ folder that contains the images, along with a txt file called K.txt which contains the intrinsic matrix. If you are experimenting on a new dataset, use the same folder structure. The code stores the computed features and matches in respective pkl files inside the root directory to speed up repeated computations. The results are stored as ply files inside a points/ folder inside the root directory. For visualizing the point clouds inside the ply files, use suitable software such as MeshLab.

Results

From the fountain-P11 image-set:

  1. Original image

  1. Reconstruction after 3 images

3, Reconstruction after 6 images

Limitations

References

  1. Hartley and Zisserman's Multiple View Geometry
  2. Mastering OpenCV with Practical Computer Vision Projects
  3. Colmap
  4. OpenSfM
  5. how-to-sfm