π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
allow to pass a second K to distort and undistort points
Luis Ferraz9880829f7b93
diff --git a/kornia/geometry/calibration/distort.py b/kornia/geometry/calibration/distort.pyindex 4b01933bc..de94a8a81 100644--- a/kornia/geometry/calibration/distort.py+++ b/kornia/geometry/calibration/distort.py@@ -1,3 +1,5 @@+from typing import Optional+import torch@@ -54,7 +56,8 @@ def tilt_projection(taux: torch.Tensor, tauy: torch.Tensor, return_inverse: boolreturn tilt-def distort_points(points: torch.Tensor, K: torch.Tensor, dist: torch.Tensor) -> torch.Tensor:+def distort_points(points: torch.Tensor, K: torch.Tensor, dist: torch.Tensor, new_K: Optional[torch.Tensor] = None+ ) -> torch.Tensor:r"""Distortion of a set of 2D points based on the lens distortion model.Radial :math:`(k_1, k_2, k_3, k_4, k_4, k_6)`,@@ -67,6 +70,7 @@ def distort_points(points: torch.Tensor, K: torch.Tensor, dist: torch.Tensor) ->dist: Distortion coefficients:math:`(k_1,k_2,p_1,p_2[,k_3[,k_4,k_5,k_6[,s_1,s_2,s_3,s_4[,\tau_x,\tau_y]]]])`. This isa vector with 4, 5, 8, 12 or 14 elements with shape :math:`(*, n)`.+ new_K: New intrinsic camera matrix with shape :math:`(*, 3, 3)`. Default: None, in this case K is used.Returns:Undistorted 2D points with shape :math:`(*, N, 2)`.@@ -84,6 +88,11 @@ def distort_points(points: torch.Tensor, K: torch.Tensor, dist: torch.Tensor) ->if K.shape[-2:] != (3, 3):raise ValueError(f'K matrix shape is invalid. Got {K.shape}.')+ if new_K is None:+ new_K = K+ elif new_K.shape[-2:] != (3, 3):+ raise ValueError(f'new_K matrix shape is invalid. Got {new_K.shape}.')+if dist.shape[-1] not in [4, 5, 8, 12, 14]:raise ValueError(f'Invalid number of distortion coefficients. Got {dist.shape[-1]}')@@ -96,9 +105,14 @@ def distort_points(points: torch.Tensor, K: torch.Tensor, dist: torch.Tensor) ->cy: torch.Tensor = K[..., 1:2, 2] # princial point in y (Bx1)fx: torch.Tensor = K[..., 0:1, 0] # focal in x (Bx1)fy: torch.Tensor = K[..., 1:2, 1] # focal in y (Bx1)+ new_cx: torch.Tensor = new_K[..., 0:1, 2] # princial point in x (Bx1)+ new_cy: torch.Tensor = new_K[..., 1:2, 2] # princial point in y (Bx1)+ new_fx: torch.Tensor = new_K[..., 0:1, 0] # focal in x (Bx1)+ new_fy: torch.Tensor = new_K[..., 1:2, 1] # focal in y (Bx1)+# This is equivalent to K^-1 [u,v,1]^T- x: torch.Tensor = (points[..., 0] - cx) / fx # (BxN - Bx1)/Bx1 -> BxN or (N,)- y: torch.Tensor = (points[..., 1] - cy) / fy # (BxN - Bx1)/Bx1 -> BxN or (N,)+ x: torch.Tensor = (points[..., 0] - new_cx) / new_fx # (BxN - Bx1)/Bx1 -> BxN or (N,)+ y: torch.Tensor = (points[..., 1] - new_cy) / new_fy # (BxN - Bx1)/Bx1 -> BxN or (N,)# Distort pointsr2 = x * x + y * ydiff --git a/kornia/geometry/calibration/undistort.py b/kornia/geometry/calibration/undistort.pyindex ddb317a46..3956d8dda 100644--- a/kornia/geometry/calibration/undistort.py+++ b/kornia/geometry/calibration/undistort.py@@ -1,3 +1,5 @@+from typing import Optional+import torchfrom kornia.geometry.linalg import transform_points@@ -8,7 +10,8 @@ from .distort import distort_points, tilt_projection# Based on https://github.com/opencv/opencv/blob/master/modules/calib3d/src/undistort.dispatch.cpp#L384-def undistort_points(points: torch.Tensor, K: torch.Tensor, dist: torch.Tensor) -> torch.Tensor:+def undistort_points(points: torch.Tensor, K: torch.Tensor, dist: torch.Tensor,+ new_K: Optional[torch.Tensor] = None, iters: int = 5) -> torch.Tensor:r"""Compensate for lens distortion a set of 2D image points.Radial :math:`(k_1, k_2, k_3, k_4, k_4, k_6)`,@@ -21,7 +24,8 @@ def undistort_points(points: torch.Tensor, K: torch.Tensor, dist: torch.Tensor)dist: Distortion coefficients:math:`(k_1,k_2,p_1,p_2[,k_3[,k_4,k_5,k_6[,s_1,s_2,s_3,s_4[,\tau_x,\tau_y]]]])`. This isa vector with 4, 5, 8, 12 or 14 elements with shape :math:`(*, n)`.-+ new_K: New intrinsic camera matrix with shape :math:`(*, 3, 3)`. Default: None, in this case K is used.+ iters: Number of undistortion iterations. Default: 5.Returns:Undistorted 2D points with shape :math:`(*, N, 2)`.@@ -35,6 +39,7 @@ def undistort_points(points: torch.Tensor, K: torch.Tensor, dist: torch.Tensor)[ 0.0711, 0.1100],[-0.0697, 0.0228],[-0.1843, -0.1606]]])+"""if points.dim() < 2 and points.shape[-1] != 2:raise ValueError(f'points shape is invalid. Got {points.shape}.')@@ -42,6 +47,11 @@ def undistort_points(points: torch.Tensor, K: torch.Tensor, dist: torch.Tensor)if K.shape[-2:] != (3, 3):raise ValueError(f'K matrix shape is invalid. Got {K.shape}.')+ if new_K is None:+ new_K = K+ elif new_K.shape[-2:] != (3, 3):+ raise ValueError(f'new_K matrix shape is invalid. Got {new_K.shape}.')+if dist.shape[-1] not in [4, 5, 8, 12, 14]:raise ValueError(f"Invalid number of distortion coefficients. Got {dist.shape[-1]}")@@ -54,6 +64,10 @@ def undistort_points(points: torch.Tensor, K: torch.Tensor, dist: torch.Tensor)cy: torch.Tensor = K[..., 1:2, 2] # princial point in y (Bx1)fx: torch.Tensor = K[..., 0:1, 0] # focal in x (Bx1)fy: torch.Tensor = K[..., 1:2, 1] # focal in y (Bx1)+ new_cx: torch.Tensor = new_K[..., 0:1, 2] # princial point in x (Bx1)+ new_cy: torch.Tensor = new_K[..., 1:2, 2] # princial point in y (Bx1)+ new_fx: torch.Tensor = new_K[..., 0:1, 0] # focal in x (Bx1)+ new_fy: torch.Tensor = new_K[..., 1:2, 1] # focal in y (Bx1)# This is equivalent to K^-1 [u,v,1]^Tx: torch.Tensor = (points[..., 0] - cx) / fx # (BxN - Bx1)/Bx1 -> BxNy: torch.Tensor = (points[..., 1] - cy) / fy # (BxN - Bx1)/Bx1 -> BxN@@ -67,7 +81,7 @@ def undistort_points(points: torch.Tensor, K: torch.Tensor, dist: torch.Tensor)# Iteratively undistort pointsx0, y0 = x, y- for _ in range(5):+ for _ in range(iters):r2 = x * x + y * yinv_rad_poly = (1 + dist[..., 5:6] * r2 + dist[..., 6:7] * r2 * r2 + dist[..., 7:8] * r2 ** 3) / (@@ -90,9 +104,8 @@ def undistort_points(points: torch.Tensor, K: torch.Tensor, dist: torch.Tensor)y = (y0 - deltaY) * inv_rad_poly# Convert points from normalized camera coordinates to pixel coordinates- x = fx * x + cx- y = fy * y + cy-+ x = new_fx * x + new_cx+ y = new_fy * y + new_cyreturn torch.stack([x, y], -1)