Restoring a 3D point from two observations through triangulation
In this recipe, you will learn how to reconstruct 3D point coordinates given observations in two views. This is a building block for many higher level 3D reconstruction algorithms and SLAM systems.
Getting ready
Before you proceed with this recipe, you need to install the OpenCV version 3.3 (or greater) Python API package.
How to do it
You need to complete the following steps:
- Import the necessary modules.
import cv2 import numpy as np
- Generate the test camera's projection matrices:
P1 = np.eye(3, 4, dtype=np.float32) P2 = np.eye(3, 4, dtype=np.float32) P2[0, 3] = -1
- Generate the test points:
N = 5 points3d = np.empty((4, N), np.float32) points3d[:3, :] = np.random.randn(3, N) points3d[3, :] = 1
- Project the 3D points into two views and add noise:
points1 = P1 @ points3d points1 = points1[:2, :] / points1[2, :] points1[:2, :] += np.random.randn(2, N) * 1e-2 points2 = P2 @ points3d points2 = points2[:2, :] / points2[2, :] points2[:2, :...