Exemplo n.º 1
0
  def test_ray_project_random(self):
    """Tests that the end point of the ray projects at the good location."""
    tensor_size = np.random.randint(3)
    tensor_shape = np.random.randint(1, 10, size=(tensor_size)).tolist()
    random_point_2d = np.random.normal(size=tensor_shape + [2])
    random_focal = np.random.normal(size=tensor_shape + [2])
    random_principal_point = np.random.normal(size=tensor_shape + [2])

    ray_3d = perspective.ray(random_point_2d, random_focal,
                             random_principal_point)
    point_2d = perspective.project(ray_3d, random_focal, random_principal_point)

    self.assertAllClose(random_point_2d, point_2d, rtol=1e-3)
Exemplo n.º 2
0
    def test_unproject_project_random(self):
        """Tests that unprojecting and projecting gives and identity mapping."""
        tensor_size = np.random.randint(3)
        tensor_shape = np.random.randint(1, 10, size=(tensor_size)).tolist()
        random_point_2d = np.random.normal(size=tensor_shape + [2])
        random_focal = np.random.normal(size=tensor_shape + [2])
        random_principal_point = np.random.normal(size=tensor_shape + [2])
        random_depth = np.random.normal(size=tensor_shape + [1])

        point_3d = perspective.unproject(random_point_2d, random_depth,
                                         random_focal, random_principal_point)
        point_2d = perspective.project(point_3d, random_focal,
                                       random_principal_point)

        self.assertAllClose(random_point_2d, point_2d, rtol=1e-3)
Exemplo n.º 3
0
  def test_project_ray_random(self):
    """Tests that that ray is pointing toward the correct location."""
    tensor_size = np.random.randint(3)
    tensor_shape = np.random.randint(1, 10, size=(tensor_size)).tolist()
    random_point_3d = np.random.normal(size=tensor_shape + [3])
    random_focal = np.random.normal(size=tensor_shape + [2])
    random_principal_point = np.random.normal(size=tensor_shape + [2])
    random_depth = np.expand_dims(random_point_3d[..., 2], axis=-1)

    point_2d = perspective.project(random_point_3d, random_focal,
                                   random_principal_point)
    ray_3d = perspective.ray(point_2d, random_focal, random_principal_point)
    ray_3d = random_depth * ray_3d

    self.assertAllClose(random_point_3d, ray_3d, rtol=1e-3)
Exemplo n.º 4
0
def generate_ground_image(height,
                          width,
                          focal,
                          principal_point,
                          camera_rotation_matrix,
                          camera_translation_vector,
                          ground_color=(0.43, 0.43, 0.8)):
  """Generate an image depicting only the ground."""
  batch_size = camera_rotation_matrix.shape[0]
  background_image = np.ones((batch_size, height, width, 1, 1),
                             dtype=np.float32)
  background_image[:, -1, ...] = 0  # Zero the bottom line for proper sampling

  # The projection of the ground depends on the top right corner (approximation)
  plane_point_np = np.tile(np.array([[3.077984, 2.905388, 0.]],
                                    dtype=np.float32), (batch_size, 1))
  plane_point_rotated = rotation_matrix_3d.rotate(plane_point_np,
                                                  camera_rotation_matrix)
  plane_point_translated = plane_point_rotated + camera_translation_vector
  plane_point2d = \
    perspective.project(plane_point_translated, focal, principal_point)
  _, y = tf.split(plane_point2d, [1, 1], axis=-1)
  sfactor = height/y
  helper_matrix1 = np.tile(np.array([[[1, 0, 0],
                                      [0, 0, 0],
                                      [0, 0, 0]]]), (batch_size, 1, 1))
  helper_matrix2 = np.tile(np.array([[[0, 0, 0],
                                      [0, 1, 0],
                                      [0, 0, 1]]]), (batch_size, 1, 1))
  transformation_matrix = tf.multiply(tf.expand_dims(sfactor, -1),
                                      helper_matrix1) + helper_matrix2
  plane_points = grid.generate((0., 0., 0.),
                               (float(height), float(width), 0.),
                               (height, width, 1))
  plane_points = tf.reshape(plane_points, [-1, 3])
  transf_plane_points = tf.matmul(transformation_matrix,
                                  plane_points,
                                  transpose_b=True)
  interpolated_points = \
    trilinear.interpolate(background_image,
                          tf.linalg.matrix_transpose(transf_plane_points))
  ground_alpha = (1- tf.reshape(interpolated_points,
                                [batch_size, height, width, 1]))
  ground_image = tf.ones((batch_size, height, width, 3))*ground_color
  return ground_image, ground_alpha
Exemplo n.º 5
0
def generate_ground_image(height,
                          width,
                          focal,
                          principal_point,
                          camera_rotation_matrix,
                          camera_translation_vector,
                          ground_color=(0.43, 0.43, 0.8)):
    """Generate an image depicting only the ground."""
    background_image = np.ones((height, width, 1, 1), dtype=np.float32)
    background_image[-1,
                     ...] = 0  # Set the bottom line to 0 for proper sampling

    # The projection of the ground depends on the top right corner (approximation)
    plane_point_np = np.array([[3.077984, 2.905388, 0.]], dtype=np.float32)
    plane_point2d = \
      perspective.project(rotation_matrix_3d.rotate(plane_point_np,
                                                    camera_rotation_matrix) +
                          camera_translation_vector.T, focal, principal_point)
    _, y = tf.split(plane_point2d, [1, 1], axis=-1)
    sfactor = y / 256.
    transformation_matrix = (1/sfactor)*np.array([[1, 0, 0],
                                                  [0, 0, 0],
                                                  [0, 0, 0]]) + \
                            np.array([[0, 0, 0],
                                      [0, 1, 0],
                                      [0, 0, 1]])
    plane_points = grid.generate(
        (0., 0., 0.), (float(height), float(width), 0.), (height, width, 1))
    plane_points = tf.reshape(plane_points, [-1, 3])
    transf_plane_points = tf.matmul(transformation_matrix,
                                    plane_points,
                                    transpose_b=True)
    interpolated_points = \
      trilinear.interpolate(background_image, tf.transpose(transf_plane_points))
    ground_alpha = (1 - tf.reshape(interpolated_points, [256, 256, 1]))
    ground_image = tf.ones((256, 256, 3)) * ground_color
    return ground_image, ground_alpha