Applies radial or OpenCV distortion to the given 2D points. Args: extra_params (torch.Tensor or numpy.ndarray): Distortion parameters of shape BxN, where N can be 1, 2, or 4. u (torch.Tensor or numpy.ndarray): Normalized x coordinates of shape Bxnum_tracks. v (torch
(extra_params, u, v)
| 97 | |
| 98 | |
| 99 | def apply_distortion(extra_params, u, v): |
| 100 | """ |
| 101 | Applies radial or OpenCV distortion to the given 2D points. |
| 102 | |
| 103 | Args: |
| 104 | extra_params (torch.Tensor or numpy.ndarray): Distortion parameters of shape BxN, where N can be 1, 2, or 4. |
| 105 | u (torch.Tensor or numpy.ndarray): Normalized x coordinates of shape Bxnum_tracks. |
| 106 | v (torch.Tensor or numpy.ndarray): Normalized y coordinates of shape Bxnum_tracks. |
| 107 | |
| 108 | Returns: |
| 109 | points2D (torch.Tensor): Distorted 2D points of shape BxNx2. |
| 110 | """ |
| 111 | extra_params = _ensure_torch(extra_params) |
| 112 | u = _ensure_torch(u) |
| 113 | v = _ensure_torch(v) |
| 114 | |
| 115 | num_params = extra_params.shape[1] |
| 116 | |
| 117 | if num_params == 1: |
| 118 | # Simple radial distortion |
| 119 | k = extra_params[:, 0] |
| 120 | u2 = u * u |
| 121 | v2 = v * v |
| 122 | r2 = u2 + v2 |
| 123 | radial = k[:, None] * r2 |
| 124 | du = u * radial |
| 125 | dv = v * radial |
| 126 | |
| 127 | elif num_params == 2: |
| 128 | # RadialCameraModel distortion |
| 129 | k1, k2 = extra_params[:, 0], extra_params[:, 1] |
| 130 | u2 = u * u |
| 131 | v2 = v * v |
| 132 | r2 = u2 + v2 |
| 133 | radial = k1[:, None] * r2 + k2[:, None] * r2 * r2 |
| 134 | du = u * radial |
| 135 | dv = v * radial |
| 136 | |
| 137 | elif num_params == 4: |
| 138 | # OpenCVCameraModel distortion |
| 139 | k1, k2, p1, p2 = (extra_params[:, 0], extra_params[:, 1], extra_params[:, 2], extra_params[:, 3]) |
| 140 | u2 = u * u |
| 141 | v2 = v * v |
| 142 | uv = u * v |
| 143 | r2 = u2 + v2 |
| 144 | radial = k1[:, None] * r2 + k2[:, None] * r2 * r2 |
| 145 | du = u * radial + 2 * p1[:, None] * uv + p2[:, None] * (r2 + 2 * u2) |
| 146 | dv = v * radial + 2 * p2[:, None] * uv + p1[:, None] * (r2 + 2 * v2) |
| 147 | else: |
| 148 | raise ValueError("Unsupported number of distortion parameters") |
| 149 | |
| 150 | u = u.clone() + du |
| 151 | v = v.clone() + dv |
| 152 | |
| 153 | return u, v |
| 154 | |
| 155 | |
| 156 | if __name__ == "__main__": |
no test coverage detected