|
| 1 | +""" |
| 2 | +Copyright (c) 2022 Ruilong Li, UC Berkeley. |
| 3 | +""" |
| 4 | +from typing import Tuple |
| 5 | + |
| 6 | +import torch |
| 7 | +import torch.nn.functional as F |
| 8 | +from torch import Tensor |
| 9 | + |
| 10 | +from . import cuda as _C |
| 11 | + |
| 12 | + |
| 13 | +def opencv_lens_undistortion( |
| 14 | + uv: Tensor, params: Tensor, eps: float = 1e-6, iters: int = 10 |
| 15 | +) -> Tensor: |
| 16 | + """Undistort the opencv distortion. |
| 17 | +
|
| 18 | + Note: |
| 19 | + This function is not differentiable to any inputs. |
| 20 | +
|
| 21 | + Args: |
| 22 | + uv: (..., 2) UV coordinates. |
| 23 | + params: (..., N) or (N) OpenCV distortion parameters. We support |
| 24 | + N = 0, 1, 2, 4, 8. If N = 0, we return the input uv directly. |
| 25 | + If N = 1, we assume the input is {k1}. If N = 2, we assume the |
| 26 | + input is {k1, k2}. If N = 4, we assume the input is {k1, k2, p1, p2}. |
| 27 | + If N = 8, we assume the input is {k1, k2, p1, p2, k3, k4, k5, k6}. |
| 28 | +
|
| 29 | + Returns: |
| 30 | + (..., 2) undistorted UV coordinates. |
| 31 | + """ |
| 32 | + assert uv.shape[-1] == 2 |
| 33 | + assert params.shape[-1] in [0, 1, 2, 4, 8] |
| 34 | + |
| 35 | + if params.shape[-1] == 0: |
| 36 | + return uv |
| 37 | + elif params.shape[-1] < 8: |
| 38 | + params = F.pad(params, (0, 8 - params.shape[-1]), "constant", 0) |
| 39 | + assert params.shape[-1] == 8 |
| 40 | + |
| 41 | + batch_shape = uv.shape[:-1] |
| 42 | + params = torch.broadcast_to(params, batch_shape + (params.shape[-1],)) |
| 43 | + |
| 44 | + return _C.opencv_lens_undistortion( |
| 45 | + uv.contiguous(), params.contiguous(), eps, iters |
| 46 | + ) |
| 47 | + |
| 48 | + |
| 49 | +def opencv_lens_undistortion_fisheye( |
| 50 | + uv: Tensor, params: Tensor, eps: float = 1e-6, iters: int = 10 |
| 51 | +) -> Tensor: |
| 52 | + """Undistort the opencv distortion of {k1, k2, k3, k4}. |
| 53 | +
|
| 54 | + Note: |
| 55 | + This function is not differentiable to any inputs. |
| 56 | +
|
| 57 | + Args: |
| 58 | + uv: (..., 2) UV coordinates. |
| 59 | + params: (..., 4) or (4) OpenCV distortion parameters. |
| 60 | +
|
| 61 | + Returns: |
| 62 | + (..., 2) undistorted UV coordinates. |
| 63 | + """ |
| 64 | + assert uv.shape[-1] == 2 |
| 65 | + assert params.shape[-1] == 4 |
| 66 | + batch_shape = uv.shape[:-1] |
| 67 | + params = torch.broadcast_to(params, batch_shape + (params.shape[-1],)) |
| 68 | + |
| 69 | + return _C.opencv_lens_undistortion_fisheye( |
| 70 | + uv.contiguous(), params.contiguous(), eps, iters |
| 71 | + ) |
| 72 | + |
| 73 | + |
| 74 | +def _opencv_lens_distortion(uv: Tensor, params: Tensor) -> Tensor: |
| 75 | + """The opencv camera distortion of {k1, k2, p1, p2, k3, k4, k5, k6}. |
| 76 | +
|
| 77 | + See https://docs.opencv.org/3.4/d9/d0c/group__calib3d.html for more details. |
| 78 | + """ |
| 79 | + k1, k2, p1, p2, k3, k4, k5, k6 = torch.unbind(params, dim=-1) |
| 80 | + s1, s2, s3, s4 = 0, 0, 0, 0 |
| 81 | + u, v = torch.unbind(uv, dim=-1) |
| 82 | + r2 = u * u + v * v |
| 83 | + r4 = r2**2 |
| 84 | + r6 = r4 * r2 |
| 85 | + ratial = (1 + k1 * r2 + k2 * r4 + k3 * r6) / ( |
| 86 | + 1 + k4 * r2 + k5 * r4 + k6 * r6 |
| 87 | + ) |
| 88 | + fx = 2 * p1 * u * v + p2 * (r2 + 2 * u * u) + s1 * r2 + s2 * r4 |
| 89 | + fy = 2 * p2 * u * v + p1 * (r2 + 2 * v * v) + s3 * r2 + s4 * r4 |
| 90 | + return torch.stack([u * ratial + fx, v * ratial + fy], dim=-1) |
| 91 | + |
| 92 | + |
| 93 | +def _opencv_lens_distortion_fisheye( |
| 94 | + uv: Tensor, params: Tensor, eps: float = 1e-10 |
| 95 | +) -> Tensor: |
| 96 | + """The opencv camera distortion of {k1, k2, k3, p1, p2}. |
| 97 | +
|
| 98 | + See https://docs.opencv.org/4.x/db/d58/group__calib3d__fisheye.html for more details. |
| 99 | +
|
| 100 | + Args: |
| 101 | + uv: (..., 2) UV coordinates. |
| 102 | + params: (..., 4) or (4) OpenCV distortion parameters. |
| 103 | +
|
| 104 | + Returns: |
| 105 | + (..., 2) distorted UV coordinates. |
| 106 | + """ |
| 107 | + assert params.shape[-1] == 4, f"Invalid params shape: {params.shape}" |
| 108 | + k1, k2, k3, k4 = torch.unbind(params, dim=-1) |
| 109 | + u, v = torch.unbind(uv, dim=-1) |
| 110 | + r = torch.sqrt(u * u + v * v) |
| 111 | + theta = torch.atan(r) |
| 112 | + theta_d = theta * ( |
| 113 | + 1 |
| 114 | + + k1 * theta**2 |
| 115 | + + k2 * theta**4 |
| 116 | + + k3 * theta**6 |
| 117 | + + k4 * theta**8 |
| 118 | + ) |
| 119 | + scale = theta_d / torch.clamp(r, min=eps) |
| 120 | + return uv * scale[..., None] |
| 121 | + |
| 122 | + |
| 123 | +@torch.jit.script |
| 124 | +def _compute_residual_and_jacobian( |
| 125 | + x: Tensor, y: Tensor, xd: Tensor, yd: Tensor, params: Tensor |
| 126 | +) -> Tuple[Tensor, Tensor, Tensor, Tensor, Tensor, Tensor]: |
| 127 | + assert params.shape[-1] == 8 |
| 128 | + |
| 129 | + k1, k2, p1, p2, k3, k4, k5, k6 = torch.unbind(params, dim=-1) |
| 130 | + |
| 131 | + # let r(x, y) = x^2 + y^2; |
| 132 | + # alpha(x, y) = 1 + k1 * r(x, y) + k2 * r(x, y) ^2 + k3 * r(x, y)^3; |
| 133 | + # beta(x, y) = 1 + k4 * r(x, y) + k5 * r(x, y) ^2 + k6 * r(x, y)^3; |
| 134 | + # d(x, y) = alpha(x, y) / beta(x, y); |
| 135 | + r = x * x + y * y |
| 136 | + alpha = 1.0 + r * (k1 + r * (k2 + r * k3)) |
| 137 | + beta = 1.0 + r * (k4 + r * (k5 + r * k6)) |
| 138 | + d = alpha / beta |
| 139 | + |
| 140 | + # The perfect projection is: |
| 141 | + # xd = x * d(x, y) + 2 * p1 * x * y + p2 * (r(x, y) + 2 * x^2); |
| 142 | + # yd = y * d(x, y) + 2 * p2 * x * y + p1 * (r(x, y) + 2 * y^2); |
| 143 | + # |
| 144 | + # Let's define |
| 145 | + # |
| 146 | + # fx(x, y) = x * d(x, y) + 2 * p1 * x * y + p2 * (r(x, y) + 2 * x^2) - xd; |
| 147 | + # fy(x, y) = y * d(x, y) + 2 * p2 * x * y + p1 * (r(x, y) + 2 * y^2) - yd; |
| 148 | + # |
| 149 | + # We are looking for a solution that satisfies |
| 150 | + # fx(x, y) = fy(x, y) = 0; |
| 151 | + fx = d * x + 2 * p1 * x * y + p2 * (r + 2 * x * x) - xd |
| 152 | + fy = d * y + 2 * p2 * x * y + p1 * (r + 2 * y * y) - yd |
| 153 | + |
| 154 | + # Compute derivative of alpha, beta over r. |
| 155 | + alpha_r = k1 + r * (2.0 * k2 + r * (3.0 * k3)) |
| 156 | + beta_r = k4 + r * (2.0 * k5 + r * (3.0 * k6)) |
| 157 | + |
| 158 | + # Compute derivative of d over [x, y] |
| 159 | + d_r = (alpha_r * beta - alpha * beta_r) / (beta * beta) |
| 160 | + d_x = 2.0 * x * d_r |
| 161 | + d_y = 2.0 * y * d_r |
| 162 | + |
| 163 | + # Compute derivative of fx over x and y. |
| 164 | + fx_x = d + d_x * x + 2.0 * p1 * y + 6.0 * p2 * x |
| 165 | + fx_y = d_y * x + 2.0 * p1 * x + 2.0 * p2 * y |
| 166 | + |
| 167 | + # Compute derivative of fy over x and y. |
| 168 | + fy_x = d_x * y + 2.0 * p2 * y + 2.0 * p1 * x |
| 169 | + fy_y = d + d_y * y + 2.0 * p2 * x + 6.0 * p1 * y |
| 170 | + |
| 171 | + return fx, fy, fx_x, fx_y, fy_x, fy_y |
| 172 | + |
| 173 | + |
| 174 | +@torch.jit.script |
| 175 | +def _opencv_lens_undistortion( |
| 176 | + uv: Tensor, params: Tensor, eps: float = 1e-6, iters: int = 10 |
| 177 | +) -> Tensor: |
| 178 | + """Same as opencv_lens_undistortion(), but native PyTorch. |
| 179 | +
|
| 180 | + Took from with bug fix and modification. |
| 181 | + https://github.com/nerfstudio-project/nerfstudio/blob/ec603634edbd61b13bdf2c598fda8c993370b8f7/nerfstudio/cameras/camera_utils.py |
| 182 | + """ |
| 183 | + assert uv.shape[-1] == 2 |
| 184 | + assert params.shape[-1] in [0, 1, 2, 4, 8] |
| 185 | + |
| 186 | + if params.shape[-1] == 0: |
| 187 | + return uv |
| 188 | + elif params.shape[-1] < 8: |
| 189 | + params = F.pad(params, (0, 8 - params.shape[-1]), "constant", 0.0) |
| 190 | + assert params.shape[-1] == 8 |
| 191 | + |
| 192 | + # Initialize from the distorted point. |
| 193 | + x, y = x0, y0 = torch.unbind(uv, dim=-1) |
| 194 | + |
| 195 | + zeros = torch.zeros_like(x) |
| 196 | + for _ in range(iters): |
| 197 | + fx, fy, fx_x, fx_y, fy_x, fy_y = _compute_residual_and_jacobian( |
| 198 | + x=x, y=y, xd=x0, yd=y0, params=params |
| 199 | + ) |
| 200 | + denominator = fy_x * fx_y - fx_x * fy_y |
| 201 | + mask = torch.abs(denominator) > eps |
| 202 | + |
| 203 | + x_numerator = fx * fy_y - fy * fx_y |
| 204 | + y_numerator = fy * fx_x - fx * fy_x |
| 205 | + step_x = torch.where(mask, x_numerator / denominator, zeros) |
| 206 | + step_y = torch.where(mask, y_numerator / denominator, zeros) |
| 207 | + |
| 208 | + x = x + step_x |
| 209 | + y = y + step_y |
| 210 | + |
| 211 | + return torch.stack([x, y], dim=-1) |
0 commit comments