Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

compeleted todo: optimized _distort_points_kannala_brandt (#3220)

Sidd6a46ca579e60
diff --git a/kornia/geometry/camera/distortion_kannala_brandt.py b/kornia/geometry/camera/distortion_kannala_brandt.py
index c93cd2a7f..efcbb15f3 100644
--- a/kornia/geometry/camera/distortion_kannala_brandt.py
+++ b/kornia/geometry/camera/distortion_kannala_brandt.py
@@ -16,6 +16,7 @@
#
# inspired by: shttps://github.com/farm-ng/sophus-rs/blob/main/src/sensor/kannala_brandt.rs
+
import kornia.core as ops
from kornia.core import Tensor
from kornia.core.check import KORNIA_CHECK_SHAPE
@@ -25,6 +26,7 @@ from kornia.geometry.camera.distortion_affine import distort_points_affine
def _distort_points_kannala_brandt_impl(
projected_points_in_camera_z1_plane: Tensor,
params: Tensor,
+ radius_sq: Tensor,
) -> Tensor:
# https://github.com/farm-ng/sophus-rs/blob/20f6cac68f17fe1ac41d0aa8a27489e2b886806f/src/sensor/kannala_brandt.rs#L51-L67
x = projected_points_in_camera_z1_plane[..., 0]
@@ -38,8 +40,6 @@ def _distort_points_kannala_brandt_impl(
k2 = params[..., 6]
k3 = params[..., 7]
- radius_sq = x**2 + y**2
-
radius = radius_sq.sqrt()
radius_inverse = 1.0 / radius
theta = radius.atan2(ops.ones_like(radius))
@@ -83,12 +83,12 @@ def distort_points_kannala_brandt(projected_points_in_camera_z1_plane: Tensor, p
radius_sq = x**2 + y**2
- # TODO: we can optimize this by passing the radius_sq to the impl functions. Check if it's worth it.
distorted_points = ops.where(
radius_sq[..., None] > 1e-8,
_distort_points_kannala_brandt_impl(
projected_points_in_camera_z1_plane,
params,
+ radius_sq,
),
distort_points_affine(projected_points_in_camera_z1_plane, params[..., :4]),
)
@@ -153,7 +153,6 @@ def undistort_points_kannala_brandt(distorted_points_in_camera: Tensor, params:
th8 = th4**2
thd = th * (1.0 + k0 * th2 + k1 * th4 + k2 * th6 + k3 * th8)
-
d_thd_wtr_th = 1.0 + 3.0 * k0 * th2 + 5.0 * k1 * th4 + 7.0 * k2 * th6 + 9.0 * k3 * th8
step = (thd - rth) / d_thd_wtr_th