VisualISAM2Example.py
Go to the documentation of this file.
1 """
2 GTSAM Copyright 2010-2018, Georgia Tech Research Corporation,
3 Atlanta, Georgia 30332-0415
4 All Rights Reserved
5 Authors: Frank Dellaert, et al. (see THANKS for the full author list)
6 
7 See LICENSE for the license information
8 
9 An example of running visual SLAM using iSAM2.
10 Author: Duy-Nguyen Ta (C++), Frank Dellaert (Python)
11 """
12 # pylint: disable=invalid-name, E1101
13 
14 from __future__ import print_function
15 
16 import gtsam
17 import gtsam.utils.plot as gtsam_plot
18 import matplotlib.pyplot as plt
19 import numpy as np
20 from gtsam.symbol_shorthand import L, X
21 from gtsam.examples import SFMdata
22 from mpl_toolkits.mplot3d import Axes3D # pylint: disable=W0611
23 
24 
25 def visual_ISAM2_plot(result):
26  """
27  VisualISAMPlot plots current state of ISAM2 object
28  Author: Ellon Paiva
29  Based on MATLAB version by: Duy Nguyen Ta and Frank Dellaert
30  """
31 
32  # Declare an id for the figure
33  fignum = 0
34 
35  fig = plt.figure(fignum)
36  if not fig.axes:
37  axes = fig.add_subplot(projection='3d')
38  else:
39  axes = fig.axes[0]
40  plt.cla()
41 
42  # Plot points
43  # Can't use data because current frame might not see all points
44  # marginals = Marginals(isam.getFactorsUnsafe(), isam.calculateEstimate())
45  # gtsam.plot_3d_points(result, [], marginals)
46  gtsam_plot.plot_3d_points(fignum, result, 'rx')
47 
48  # Plot cameras
49  i = 0
50  while result.exists(X(i)):
51  pose_i = result.atPose3(X(i))
52  gtsam_plot.plot_pose3(fignum, pose_i, 10)
53  i += 1
54 
55  # draw
56  axes.set_xlim3d(-40, 40)
57  axes.set_ylim3d(-40, 40)
58  axes.set_zlim3d(-40, 40)
59  plt.pause(1)
60 
61 
63  plt.ion()
64 
65  # Define the camera calibration parameters
66  K = gtsam.Cal3_S2(50.0, 50.0, 0.0, 50.0, 50.0)
67 
68  # Define the camera observation noise model
69  measurement_noise = gtsam.noiseModel.Isotropic.Sigma(
70  2, 1.0) # one pixel in u and v
71 
72  # Create the set of ground-truth landmarks
73  points = SFMdata.createPoints()
74 
75  # Create the set of ground-truth poses
76  poses = SFMdata.createPoses()
77 
78  # Create an iSAM2 object. Unlike iSAM1, which performs periodic batch steps
79  # to maintain proper linearization and efficient variable ordering, iSAM2
80  # performs partial relinearization/reordering at each step. A parameter
81  # structure is available that allows the user to set various properties, such
82  # as the relinearization threshold and type of linear solver. For this
83  # example, we we set the relinearization threshold small so the iSAM2 result
84  # will approach the batch result.
85  parameters = gtsam.ISAM2Params()
86  parameters.setRelinearizeThreshold(0.01)
87  parameters.relinearizeSkip = 1
88  isam = gtsam.ISAM2(parameters)
89 
90  # Create a Factor Graph and Values to hold the new data
92  initial_estimate = gtsam.Values()
93 
94  # Loop over the different poses, adding the observations to iSAM incrementally
95  for i, pose in enumerate(poses):
96 
97  # Add factors for each landmark observation
98  for j, point in enumerate(points):
99  camera = gtsam.PinholeCameraCal3_S2(pose, K)
100  measurement = camera.project(point)
101  graph.push_back(gtsam.GenericProjectionFactorCal3_S2(
102  measurement, measurement_noise, X(i), L(j), K))
103 
104  # Add an initial guess for the current pose
105  # Intentionally initialize the variables off from the ground truth
106  initial_estimate.insert(X(i), pose.compose(gtsam.Pose3(
107  gtsam.Rot3.Rodrigues(-0.1, 0.2, 0.25), gtsam.Point3(0.05, -0.10, 0.20))))
108 
109  # If this is the first iteration, add a prior on the first pose to set the
110  # coordinate frame and a prior on the first landmark to set the scale.
111  # Also, as iSAM solves incrementally, we must wait until each is observed
112  # at least twice before adding it to iSAM.
113  if i == 0:
114  # Add a prior on pose x0
115  pose_noise = gtsam.noiseModel.Diagonal.Sigmas(np.array(
116  [0.1, 0.1, 0.1, 0.3, 0.3, 0.3])) # 30cm std on x,y,z 0.1 rad on roll,pitch,yaw
117  graph.push_back(gtsam.PriorFactorPose3(X(0), poses[0], pose_noise))
118 
119  # Add a prior on landmark l0
120  point_noise = gtsam.noiseModel.Isotropic.Sigma(3, 0.1)
121  graph.push_back(gtsam.PriorFactorPoint3(
122  L(0), points[0], point_noise)) # add directly to graph
123 
124  # Add initial guesses to all observed landmarks
125  # Intentionally initialize the variables off from the ground truth
126  for j, point in enumerate(points):
127  initial_estimate.insert(L(j), gtsam.Point3(
128  point[0]-0.25, point[1]+0.20, point[2]+0.15))
129  else:
130  # Update iSAM with the new factors
131  isam.update(graph, initial_estimate)
132  # Each call to iSAM2 update(*) performs one iteration of the iterative nonlinear solver.
133  # If accuracy is desired at the expense of time, update(*) can be called additional
134  # times to perform multiple optimizer iterations every step.
135  isam.update()
136  current_estimate = isam.calculateEstimate()
137  print("****************************************************")
138  print("Frame", i, ":")
139  for j in range(i + 1):
140  print(X(j), ":", current_estimate.atPose3(X(j)))
141 
142  for j in range(len(points)):
143  print(L(j), ":", current_estimate.atPoint3(L(j)))
144 
145  visual_ISAM2_plot(current_estimate)
146 
147  # Clear the factor graph and values for the next iteration
148  graph.resize(0)
149  initial_estimate.clear()
150 
151  plt.ioff()
152  plt.show()
153 
154 
155 if __name__ == '__main__':
gtsam::ISAM2Params
Definition: ISAM2Params.h:136
gtsam::ISAM2
Definition: ISAM2.h:45
gtsam::symbol_shorthand
Definition: inference/Symbol.h:147
gtsam::noiseModel::Diagonal::Sigmas
static shared_ptr Sigmas(const Vector &sigmas, bool smart=true)
Definition: NoiseModel.cpp:291
gtsam.examples
Definition: python/gtsam/examples/__init__.py:1
X
#define X
Definition: icosphere.cpp:20
gtsam.examples.VisualISAM2Example.visual_ISAM2_plot
def visual_ISAM2_plot(result)
Definition: VisualISAM2Example.py:25
gtsam::range
Double_ range(const Point2_ &p, const Point2_ &q)
Definition: slam/expressions.h:30
gtsam::print
void print(const Matrix &A, const string &s, ostream &stream)
Definition: Matrix.cpp:155
gtsam::Pose3
Definition: Pose3.h:37
gtsam::Rot3::Rodrigues
static Rot3 Rodrigues(const Vector3 &w)
Definition: Rot3.h:240
gtsam::PinholeCamera
Definition: PinholeCamera.h:33
gtsam::NonlinearFactorGraph
Definition: NonlinearFactorGraph.h:55
L
MatrixXd L
Definition: LLT_example.cpp:6
gtsam::Values
Definition: Values.h:65
gtsam::Cal3_S2
The most common 5DOF 3D->2D calibration.
Definition: Cal3_S2.h:34
gtsam::Point3
Vector3 Point3
Definition: Point3.h:38
gtsam::noiseModel::Isotropic::Sigma
static shared_ptr Sigma(size_t dim, double sigma, bool smart=true)
Definition: NoiseModel.cpp:624
len
size_t len(handle h)
Get the length of a Python object.
Definition: pytypes.h:2446
gtsam::utils.plot
Definition: plot.py:1
gtsam.examples.VisualISAM2Example.visual_ISAM2_example
def visual_ISAM2_example()
Definition: VisualISAM2Example.py:62


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:09:43