visual_data_generator.py
Go to the documentation of this file.
1 from __future__ import print_function
2 
3 import numpy as np
4 import math
5 from math import pi
6 import gtsam
7 from gtsam import Point3, Pose3, PinholeCameraCal3_S2, Cal3_S2
8 
9 
10 class Options:
11  """
12  Options to generate test scenario
13  """
14 
15  def __init__(self, triangle=False, nrCameras=3, K=Cal3_S2()):
16  """
17  Options to generate test scenario
18  @param triangle: generate a triangle scene with 3 points if True, otherwise
19  a cube with 8 points
20  @param nrCameras: number of cameras to generate
21  @param K: camera calibration object
22  """
23  self.triangle = triangle
24  self.nrCameras = nrCameras
25 
26 
28  """
29  Object holding generated ground-truth data
30  """
31 
32  def __init__(self, K=Cal3_S2(), nrCameras=3, nrPoints=4):
33  self.K = K
34  self.cameras = [Pose3()] * nrCameras
35  self.points = [Point3(0, 0, 0)] * nrPoints
36 
37  def print_(self, s=""):
38  print(s)
39  print("K = ", self.K)
40  print("Cameras: ", len(self.cameras))
41  for camera in self.cameras:
42  print("\t", camera)
43  print("Points: ", len(self.points))
44  for point in self.points:
45  print("\t", point)
46  pass
47 
48 
49 class Data:
50  """
51  Object holding generated measurement data
52  """
53 
54  class NoiseModels:
55  pass
56 
57  def __init__(self, K=Cal3_S2(), nrCameras=3, nrPoints=4):
58  self.K = K
59  self.Z = [x[:] for x in [[gtsam.Point2()] * nrPoints] * nrCameras]
60  self.J = [x[:] for x in [[0] * nrPoints] * nrCameras]
61  self.odometry = [Pose3()] * nrCameras
62 
63  # Set Noise parameters
65  self.noiseModels.posePrior = gtsam.noiseModel.Diagonal.Sigmas(
66  np.array([0.001, 0.001, 0.001, 0.1, 0.1, 0.1]))
67  # noiseModels.odometry = gtsam.noiseModel.Diagonal.Sigmas(
68  # np.array([0.001,0.001,0.001,0.1,0.1,0.1]))
69  self.noiseModels.odometry = gtsam.noiseModel.Diagonal.Sigmas(
70  np.array([0.05, 0.05, 0.05, 0.2, 0.2, 0.2]))
71  self.noiseModels.pointPrior = gtsam.noiseModel.Isotropic.Sigma(3, 0.1)
72  self.noiseModels.measurement = gtsam.noiseModel.Isotropic.Sigma(2, 1.0)
73 
74 
75 def generate_data(options):
76  """ Generate ground-truth and measurement data. """
77 
78  K = Cal3_S2(500, 500, 0, 640. / 2., 480. / 2.)
79  nrPoints = 3 if options.triangle else 8
80 
81  truth = GroundTruth(K=K, nrCameras=options.nrCameras, nrPoints=nrPoints)
82  data = Data(K, nrCameras=options.nrCameras, nrPoints=nrPoints)
83 
84  # Generate simulated data
85  if options.triangle: # Create a triangle target, just 3 points on a plane
86  r = 10
87  for j in range(len(truth.points)):
88  theta = j * 2 * pi / nrPoints
89  truth.points[j] = Point3(r * math.cos(theta), r * math.sin(theta), 0)
90  else: # 3D landmarks as vertices of a cube
91  truth.points = [
92  Point3(10, 10, 10), Point3(-10, 10, 10),
93  Point3(-10, -10, 10), Point3(10, -10, 10),
94  Point3(10, 10, -10), Point3(-10, 10, -10),
95  Point3(-10, -10, -10), Point3(10, -10, -10)
96  ]
97 
98  # Create camera cameras on a circle around the triangle
99  height = 10
100  r = 40
101  for i in range(options.nrCameras):
102  theta = i * 2 * pi / options.nrCameras
103  t = Point3(r * math.cos(theta), r * math.sin(theta), height)
104  truth.cameras[i] = PinholeCameraCal3_S2.Lookat(t,
105  Point3(0, 0, 0),
106  Point3(0, 0, 1),
107  truth.K)
108  # Create measurements
109  for j in range(nrPoints):
110  # All landmarks seen in every frame
111  data.Z[i][j] = truth.cameras[i].project(truth.points[j])
112  data.J[i][j] = j
113 
114  # Calculate odometry between cameras
115  for i in range(1, options.nrCameras):
116  data.odometry[i] = truth.cameras[i - 1].pose().between(
117  truth.cameras[i].pose())
118 
119  return data, truth
void print(const Matrix &A, const string &s, ostream &stream)
Definition: Matrix.cpp:155
Point2_ project(const Point3_ &p_cam)
Expression version of PinholeBase::Project.
Vector2 Point2
Definition: Point2.h:27
def __init__(self, triangle=False, nrCameras=3, K=Cal3_S2())
static const Pose3 pose(Rot3(Vector3(1,-1,-1).asDiagonal()), Point3(0, 0, 0.5))
static shared_ptr Sigmas(const Vector &sigmas, bool smart=true)
Definition: NoiseModel.cpp:270
Vector3 Point3
Definition: Point3.h:35
size_t len(handle h)
Definition: pytypes.h:1514
def __init__(self, K=Cal3_S2(), nrCameras=3, nrPoints=4)
Expression< T > between(const Expression< T > &t1, const Expression< T > &t2)
def __init__(self, K=Cal3_S2(), nrCameras=3, nrPoints=4)
static shared_ptr Sigma(size_t dim, double sigma, bool smart=true)
Definition: NoiseModel.cpp:567


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:51:25