test_Triangulation.py
Go to the documentation of this file.
1 """
2 GTSAM Copyright 2010-2019, Georgia Tech Research Corporation,
3 Atlanta, Georgia 30332-0415
4 All Rights Reserved
5 
6 See LICENSE for the license information
7 
8 Test Triangulation
9 Author: Frank Dellaert & Fan Jiang (Python)
10 """
11 import unittest
12 
13 import numpy as np
14 
15 import gtsam
16 from gtsam import (Cal3_S2, Cal3Bundler, CameraSetCal3_S2,
17  CameraSetCal3Bundler, PinholeCameraCal3_S2,
18  PinholeCameraCal3Bundler, Point2Vector, Point3, Pose3,
19  Pose3Vector, Rot3)
20 from gtsam.utils.test_case import GtsamTestCase
21 
22 
24  """ Tests for triangulation with shared and individual calibrations """
25 
26  def setUp(self):
27  """ Set up two camera poses """
28  # Looking along X-axis, 1 meter above ground plane (x-y)
29  upright = Rot3.Ypr(-np.pi / 2, 0., -np.pi / 2)
30  pose1 = Pose3(upright, Point3(0, 0, 1))
31 
32  # create second camera 1 meter to the right of first camera
33  pose2 = pose1.compose(Pose3(Rot3(), Point3(1, 0, 0)))
34  # twoPoses
35  self.poses = Pose3Vector()
36  self.poses.append(pose1)
37  self.poses.append(pose2)
38 
39  # landmark ~5 meters infront of camera
40  self.landmark = Point3(5, 0.5, 1.2)
41 
42  def generate_measurements(self, calibration, camera_model, cal_params, camera_set=None):
43  """
44  Generate vector of measurements for given calibration and camera model.
45 
46  Args:
47  calibration: Camera calibration e.g. Cal3_S2
48  camera_model: Camera model e.g. PinholeCameraCal3_S2
49  cal_params: Iterable of camera parameters for `calibration` e.g. [K1, K2]
50  camera_set: Cameraset object (for individual calibrations)
51  Returns:
52  list of measurements and list/CameraSet object for cameras
53  """
54  if camera_set is not None:
55  cameras = camera_set()
56  else:
57  cameras = []
58  measurements = Point2Vector()
59 
60  for k, pose in zip(cal_params, self.poses):
61  K = calibration(*k)
62  camera = camera_model(pose, K)
63  cameras.append(camera)
64  z = camera.project(self.landmark)
65  measurements.append(z)
66 
67  return measurements, cameras
68 
70  """ Tests triangulation with shared Cal3_S2 calibration"""
71  # Some common constants
72  sharedCal = (1500, 1200, 0, 640, 480)
73 
74  measurements, _ = self.generate_measurements(Cal3_S2,
75  PinholeCameraCal3_S2,
76  (sharedCal, sharedCal))
77 
78  triangulated_landmark = gtsam.triangulatePoint3(self.poses,
79  Cal3_S2(sharedCal),
80  measurements,
81  rank_tol=1e-9,
82  optimize=True)
83  self.gtsamAssertEquals(self.landmark, triangulated_landmark, 1e-9)
84 
85  # Add some noise and try again: result should be ~ (4.995, 0.499167, 1.19814)
86  measurements_noisy = Point2Vector()
87  measurements_noisy.append(measurements[0] - np.array([0.1, 0.5]))
88  measurements_noisy.append(measurements[1] - np.array([-0.2, 0.3]))
89 
90  triangulated_landmark = gtsam.triangulatePoint3(self.poses,
91  Cal3_S2(sharedCal),
92  measurements_noisy,
93  rank_tol=1e-9,
94  optimize=True)
95 
96  self.gtsamAssertEquals(self.landmark, triangulated_landmark, 1e-2)
97 
98  def test_distinct_Ks(self):
99  """ Tests triangulation with individual Cal3_S2 calibrations """
100  # two camera parameters
101  K1 = (1500, 1200, 0, 640, 480)
102  K2 = (1600, 1300, 0, 650, 440)
103 
104  measurements, cameras = self.generate_measurements(Cal3_S2,
105  PinholeCameraCal3_S2,
106  (K1, K2),
107  camera_set=CameraSetCal3_S2)
108 
109  triangulated_landmark = gtsam.triangulatePoint3(cameras,
110  measurements,
111  rank_tol=1e-9,
112  optimize=True)
113  self.gtsamAssertEquals(self.landmark, triangulated_landmark, 1e-9)
114 
116  """ Tests triangulation with individual Cal3Bundler calibrations"""
117  # two camera parameters
118  K1 = (1500, 0, 0, 640, 480)
119  K2 = (1600, 0, 0, 650, 440)
120 
121  measurements, cameras = self.generate_measurements(Cal3Bundler,
122  PinholeCameraCal3Bundler,
123  (K1, K2),
124  camera_set=CameraSetCal3Bundler)
125 
126  triangulated_landmark = gtsam.triangulatePoint3(cameras,
127  measurements,
128  rank_tol=1e-9,
129  optimize=True)
130  self.gtsamAssertEquals(self.landmark, triangulated_landmark, 1e-9)
131 
132 
133 if __name__ == "__main__":
134  unittest.main()
def gtsamAssertEquals(self, actual, expected, tol=1e-9)
Definition: test_case.py:18
std::vector< Pose3 > Pose3Vector
Definition: Pose3.h:395
def generate_measurements(self, calibration, camera_model, cal_params, camera_set=None)
std::vector< Point2, Eigen::aligned_allocator< Point2 > > Point2Vector
Definition: Point2.h:42
Vector3 Point3
Definition: Point3.h:35
Point3 triangulatePoint3(const CameraSet< PinholeCamera< CALIBRATION > > &cameras, const Point2Vector &measurements, double rank_tol=1e-9, bool optimize=false)
Pinhole-specific version.


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:46:04