π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
add docs examples
edgarriba771ca1625e87
diff --git a/torchgeometry/conversions.py b/torchgeometry/conversions.pyindex cc1de0ba5..4b53dc670 100644--- a/torchgeometry/conversions.py+++ b/torchgeometry/conversions.py@@ -82,6 +82,14 @@ def convert_points_from_homogeneous(points, eps=1e-6):Returns:Tensor: tensor of N-1-dimensional points of size (B, D, N-1).++ Shape:+ - Input: :math:`(B, D, N)`+ - Output: :math:`(B, D, N - 1)`++ Example:+ >>> input = torch.rand(2, 4, 3) # BxNx3+ >>> output = tgm.convert_points_from_homogeneous(input) # BxNx2"""if not torch.is_tensor(points):raise TypeError("Input type is not a torch.Tensor. Got {}".format(@@ -99,10 +107,18 @@ def convert_points_to_homogeneous(points):"""Converts points from Euclidean to homogeneous space.Args:- points (Tensor): tensor of N-dimensional points of size (B, D, N).+ points (Tensor): tensor of N-dimensional points of size.Returns:- Tensor: tensor of N+1-dimensional points of size (B, D, N+1).+ Tensor: tensor of N+1-dimensional points.++ Shape:+ - Input: :math:`(B, D, N)`+ - Output: :math:`(B, D, N + 1)`++ Example:+ >>> input = torch.rand(2, 4, 3) # BxNx3+ >>> output = tgm.convert_points_to_homogeneous(input) # BxNx4"""if not torch.is_tensor(points):raise TypeError("Input type is not a torch.Tensor. Got {}".format(@@ -120,12 +136,20 @@ def transform_points(dst_pose_src, points_src):"""Applies batch of transformations to batch of sets of points.Args:- dst_pose_src (Tensor): tensor for transformations of size (B, D+1, D+1).- points_src (Tensor): tensor of points of size (B, N, D).+ dst_pose_src (Tensor): tensor for transformations.+ points_src (Tensor): tensor of points.Returns:- Tensor: tensor of N-dimensional points of size (B, D, N).+ Tensor: tensor of N-dimensional points.++ Shape:+ - Input: :math:`(B, D+1, D+1)` and :math:`(B, D, N)`+ - Output: :math:`(B, N, D)`+ Example:+ >>> input = torch.rand(2, 4, 3) # BxNx3+ >>> pose = torch.eye(4).view(1, 4, 4) # Bx4x4+ >>> output = tgm.transform_points(pose, input) # BxNx3"""if not torch.is_tensor(dst_pose_src) or not torch.is_tensor(points_src):raise TypeError("Input type is not a torch.Tensor")@@ -151,12 +175,18 @@ def angle_axis_to_rotation_matrix(angle_axis):"""Convert 3d vector of axis-angle rotation to 4x4 rotation matrixArgs:- angle_axis (Tensor): tensor of 3d vector of axis-angle rotations- of size (N, 3).+ angle_axis (Tensor): tensor of 3d vector of axis-angle rotations.Returns:- Tensor: tensor of 4x4 rotation matrices of size (N, 4, 4).+ Tensor: tensor of 4x4 rotation matrices.+ Shape:+ - Input: :math:`(N, 3)`+ - Output: :math:`(N, 4, 4)`++ Example:+ >>> input = torch.rand(1, 3) # Nx3+ >>> output = tgm.angle_axis_to_rotation_matrix(input) # Nx4x4"""def _compute_rotation_matrix(angle_axis, theta2, eps=1e-6):# We want to be careful to only evaluate the square root if the@@ -220,11 +250,18 @@ def rtvec_to_pose(rtvec):Convert axis-angle rotation and translation vector to 4x4 pose matrixArgs:- rtvec (Tensor): Rodrigues vector transformations of size (N, 6).+ rtvec (Tensor): Rodrigues vector transformationsReturns:- Tensor: transformation matrices of size (N, 4, 4).+ Tensor: transformation matrices+ Shape:+ - Input: :math:`(N, 6)`+ - Output: :math:`(N, 4, 4)`++ Example:+ >>> input = torch.rand(3, 6) # Nx6+ >>> output = tgm.rtvec_to_pose(input) # Nx4x4"""assert rtvec.shape[-1] == 6, 'rtvec=[rx, ry, rz, tx, ty, tz]'pose = angle_axis_to_rotation_matrix(rtvec[..., :3])@@ -233,14 +270,21 @@ def rtvec_to_pose(rtvec):def rotation_matrix_to_angle_axis(rotation_matrix):- """Convert 4x4 rotation matrix to 4d quaternion vector+ """Convert 4x4 rotation matrix to Rodrigues vectorArgs:- rotation_matrix (Tensor): rotation matrix of size (4, 4).+ rotation_matrix (Tensor): rotation matrix.Returns:- Tensor: Rodrigues vector transformation of size (1, 6).+ Tensor: Rodrigues vector transformation.+ Shape:+ - Input: :math:`(N, 4, 4)`+ - Output: :math:`(N, 3)`++ Example:+ >>> input = torch.rand(2, 4, 4) # Nx4x4+ >>> output = tgm.rotation_matrix_to_angle_axis(input) # Nx3"""# todo add check that matrix is a valid rotation matrixquaternion = rotation_matrix_to_quaternion(rotation_matrix)@@ -259,6 +303,13 @@ def rotation_matrix_to_quaternion(rotation_matrix, eps=1e-6):Return:Tensor: the rotation in quaternion+ Shape:+ - Input: :math:`(N, 4, 4)`+ - Output: :math:`(N, 4)`++ Example:+ >>> input = torch.rand(4, 4, 4) # Nx4x4+ >>> output = tgm.rotation_matrix_to_quaternion(input) # Nx4"""rmat_t = torch.transpose(rotation_matrix, 1, 2)@@ -313,10 +364,18 @@ def quaternion_to_angle_axis(quaternion, eps=1e-6):Adapted from ceres C++ library: ceres-solver/include/ceres/rotation.hArgs:- quaternion (Tensor): vector of length 4+ quaternion (Tensor): batch with quaternionsReturn:- Tensor: angle axis of rotation (vector of length 3)+ Tensor: batch with angle axis of rotation++ Shape:+ - Input: :math:`(N, 4)`+ - Output: :math:`(N, 3)`++ Example:+ >>> input = torch.rand(2, 4) # Nx4+ >>> output = tgm.quaternion_to_angle_axis(input) # Nx3"""assert quaternion.size(1) == 4, 'Input must be a vector of length 4'normalizer = 1 / torch.norm(quaternion, dim=1)diff --git a/torchgeometry/pinhole.py b/torchgeometry/pinhole.pyindex 6f948a7a5..d4b9e69aa 100644--- a/torchgeometry/pinhole.py+++ b/torchgeometry/pinhole.py@@ -24,10 +24,18 @@ def inverse_pose(pose):"""Inverts a 4x4 pose.Args:- points (Tensor): tensor of either size (4, 4) or (B, 4, 4).+ points (Tensor): tensor with poses.Returns:- Tensor: tensor with same size as input.+ Tensor: tensor with inverted poses.++ Shape:+ - Input: :math:`(N, 4, 4)`+ - Output: :math:`(N, 4, 4)`++ Example:+ >>> pose = torch.rand(1, 4, 4) # Nx4x4+ >>> pose_inv = tgm.inverse_pose(pose) # Nx4x4"""if not torch.is_tensor(pose):raise TypeError("Input type is not a torch.Tensor. Got {}".format(@@ -51,15 +59,21 @@ def inverse_pose(pose):def pinhole_matrix(pinholes):- """Converts vector pinhole representation to 4x4 Tensor+ """Returns the pinhole matrix from a pinhole modelArgs:- pinholes (Tensor): tensor of form [fx fy cx cy h w rx ry rz tx ty tz]- of size (N, 12).+ pinholes (Tensor): tensor of pinhole models.Returns:- Tensor: tensor of pinhole matrices of size (N, 4, 4).+ Tensor: tensor of pinhole matrices.+ Shape:+ - Input: :math:`(N, 12)`+ - Output: :math:`(N, 4, 4)`++ Example:+ >>> pinhole = torch.rand(1, 12) # Nx12+ >>> pinhole_matrix = tgm.pinhole_matrix(pinhole) # Nx4x4"""assert len(pinholes.shape) == 2 and pinholes.shape[1] == 12, pinholes.shape# unpack pinhole values@@ -76,15 +90,21 @@ def pinhole_matrix(pinholes):def inverse_pinhole_matrix(pinhole, eps=1e-6):- """Invert a set of pinholes+ """Returns the inverted pinhole matrix from a pinhole modelArgs:- pinholes (Tensor): tensor of form [fx fy cx cy h w rx ry rz tx ty tz]- of size (N, 12).+ pinholes (Tensor): tensor with pinhole models.Returns:- Tensor: tensor of inverted pinhole matrices of size (N, 4, 4).+ Tensor: tensor of inverted pinhole matrices.+ Shape:+ - Input: :math:`(N, 12)`+ - Output: :math:`(N, 4, 4)`++ Example:+ >>> pinhole = torch.rand(1, 12) # Nx12+ >>> pinhole_matrix_inv = tgm.inverse_pinhole_matrix(pinhole) # Nx4x4"""assert len(pinhole.shape) == 2 and pinhole.shape[1] == 12, pinhole.shape# unpack pinhole values@@ -104,14 +124,20 @@ def scale_pinhole(pinholes, scale):"""Scales the pinhole matrix for each pinhole model.Args:- pinholes (Tensor): tensor of form [fx fy cx cy h w rx ry rz tx ty tz]- of size (N, 12).- scale (Tensor): tensor of scales of form [N, 1]+ pinholes (Tensor): tensor with the pinhole model.+ scale (Tensor): tensor of scales.Returns:- Tensor: tensor of scaled pinholes of form- [fx fy cx cy h w rx ry rz tx ty tz] of size (N, 12).+ Tensor: tensor of scaled pinholes.++ Shape:+ - Input: :math:`(N, 12)` and :math:`(N, 1)`+ - Output: :math:`(N, 12)`+ Example:+ >>> pinhole_i = torch.rand(1, 12) # Nx12+ >>> scales = 2.0 * torch.ones(1, 1) # Nx1+ >>> pinhole_i_scaled = tgm.scale_pinhole(pinhole_i) # Nx12"""assert len(pinholes.shape) == 2 and pinholes.shape[1] == 12, pinholes.shapeassert len(scale.shape) == 2 and scale.shape[1] == 1, scale.shape@@ -138,18 +164,27 @@ def get_optical_pose_base(pinholes):def homography_i_H_ref(pinhole_i, pinhole_ref):"""- Homography from pinhole_ref to pinhole_i+ Homography from reference to ith pinhole++ .. math::+ H_{ref}^{i} = K_{i} * T_{ref}^{i} * K_{ref}^{-1}Args:- pinhole_i (Tensor): tensor of form [fx fy cx cy h w rx ry rz tx ty tz]- of size (N, 12).- pinhole_ref (Tensor): tensor of form [fx fy cx cy h w rx ry rz tx ty tz]- of size (N, 12).+ pinhole_i (Tensor): tensor with pinhole model for ith frame.+ pinhole_ref (Tensor): tensor with pinhole model for reference frame.- Returns:+ Return:Tensor: tensors that convert depth points (u, v, d) from pinhole_ref topinhole_i (N, 4, 4).+ Shape:+ - Input: :math:`(N, 12)` and :math:`(N, 12)`+ - Output: :math:`(N, 4, 4)`++ Example:+ >>> pinhole_i = torch.rand(1, 12) # Nx12+ >>> pinhole_ref = torch.rand(1, 12) # Nx12+ >>> i_H_ref = tgm.homography_i_H_ref(pinhole_i, pinhole_ref) # Nx4x4"""assert len(pinhole_i.shape) == 2 and pinhole_i.shape[1] == 12, pinhole.shape