def test_intrinsics_from_matrix_to_intrinsics_random(self): """Tests that converting intrinsics to a matrix and back is consistent.""" tensor_size = np.random.randint(3) tensor_shape = np.random.randint(1, 10, size=(tensor_size)).tolist() random_focal = np.random.normal(size=tensor_shape + [2]) random_principal_point = np.random.normal(size=tensor_shape + [2]) matrix = perspective.matrix_from_intrinsics(random_focal, random_principal_point) focal, principal_point = perspective.intrinsics_from_matrix(matrix) self.assertAllClose(random_focal, focal, rtol=1e-3) self.assertAllClose(random_principal_point, principal_point, rtol=1e-3)
def test_matrix_from_intrinsics_to_matrix_random(self): """Tests that converting a matrix to intrinsics and back is consistent.""" tensor_size = np.random.randint(3) tensor_shape = np.random.randint(1, 10, size=(tensor_size)).tolist() random_focal = np.random.normal(size=tensor_shape + [2]) random_principal_point = np.random.normal(size=tensor_shape + [2]) fx = random_focal[..., 0] fy = random_focal[..., 1] cx = random_principal_point[..., 0] cy = random_principal_point[..., 1] zero = np.zeros_like(fx) one = np.ones_like(fx) random_matrix = np.stack((fx, zero, cx, zero, fy, cy, zero, zero, one), axis=-1).reshape(tensor_shape + [3, 3]) focal, principal_point = perspective.intrinsics_from_matrix(random_matrix) matrix = perspective.matrix_from_intrinsics(focal, principal_point) self.assertAllClose(random_matrix, matrix, rtol=1e-3)