mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-12-05 15:28:49 +08:00
fix camera distortions
This commit is contained in:
1 parent
0cc168381e
commit
2fff9308e0
7 files changed
+52321
-33926
No files matched your search
File diff suppressed because it is too large.
Load diff
@@ -116,9 +116,9 @@ class CameraOptimizer(nn.Module):
|
|||||||
if self.config.mode == "off":
|
if self.config.mode == "off":
|
||||||
pass
|
pass
|
||||||
elif self.config.mode == "SO3xR3":
|
elif self.config.mode == "SO3xR3":
|
||||||
outputs.append(exp_map_SO3xR3(self.pose_adjustment[indices, :]))
|
outputs.append(exp_map_SO3xR3(self.pose_adjustment)[indices])
|
||||||
elif self.config.mode == "SE3":
|
elif self.config.mode == "SE3":
|
||||||
outputs.append(exp_map_SE3(self.pose_adjustment[indices, :]))
|
outputs.append(exp_map_SE3(self.pose_adjustment)[indices])
|
||||||
else:
|
else:
|
||||||
assert_never(self.config.mode)
|
assert_never(self.config.mode)
|
||||||
|
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ from typing import List, Optional, Tuple
|
|||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import torch
|
import torch
|
||||||
|
import torch.nn.functional as F
|
||||||
from numpy.typing import ArrayLike
|
from numpy.typing import ArrayLike
|
||||||
from torchtyping import TensorType
|
from torchtyping import TensorType
|
||||||
from typing_extensions import Literal
|
from typing_extensions import Literal
|
||||||
@@ -271,29 +272,6 @@ def viewmatrix(lookat: torch.Tensor, up: torch.Tensor, pos: torch.Tensor) -> Ten
|
|||||||
return m
|
return m
|
||||||
|
|
||||||
|
|
||||||
def get_distortion_params(
|
|
||||||
k1: float = 0.0,
|
|
||||||
k2: float = 0.0,
|
|
||||||
k3: float = 0.0,
|
|
||||||
k4: float = 0.0,
|
|
||||||
p1: float = 0.0,
|
|
||||||
p2: float = 0.0,
|
|
||||||
) -> TensorType[...]:
|
|
||||||
"""Returns a distortion parameters matrix.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
k1: The first radial distortion parameter.
|
|
||||||
k2: The second radial distortion parameter.
|
|
||||||
k3: The third radial distortion parameter.
|
|
||||||
k4: The fourth radial distortion parameter.
|
|
||||||
p1: The first tangential distortion parameter.
|
|
||||||
p2: The second tangential distortion parameter.
|
|
||||||
Returns:
|
|
||||||
torch.Tensor: A distortion parameters matrix.
|
|
||||||
"""
|
|
||||||
return torch.Tensor([k1, k2, k3, k4, p1, p2])
|
|
||||||
|
|
||||||
|
|
||||||
@torch.jit.script
|
@torch.jit.script
|
||||||
def _compute_residual_and_jacobian(
|
def _compute_residual_and_jacobian(
|
||||||
x: torch.Tensor,
|
x: torch.Tensor,
|
||||||
@@ -311,24 +289,23 @@ def _compute_residual_and_jacobian(
|
|||||||
y: The updated y coordinates.
|
y: The updated y coordinates.
|
||||||
xd: The distorted x coordinates.
|
xd: The distorted x coordinates.
|
||||||
yd: The distorted y coordinates.
|
yd: The distorted y coordinates.
|
||||||
distortion_params: The distortion parameters [k1, k2, k3, k4, p1, p2].
|
distortion_params: The distortion parameters [k1, k2, p1, p2, k3, k4, k5, k6].
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
The residuals (fx, fy) and jacobians (fx_x, fx_y, fy_x, fy_y).
|
The residuals (fx, fy) and jacobians (fx_x, fx_y, fy_x, fy_y).
|
||||||
"""
|
"""
|
||||||
|
assert distortion_params.shape[-1] == 8
|
||||||
|
|
||||||
k1 = distortion_params[..., 0]
|
k1, k2, p1, p2, k3, k4, k5, k6 = torch.unbind(distortion_params, dim=-1)
|
||||||
k2 = distortion_params[..., 1]
|
|
||||||
k3 = distortion_params[..., 2]
|
|
||||||
k4 = distortion_params[..., 3]
|
|
||||||
p1 = distortion_params[..., 4]
|
|
||||||
p2 = distortion_params[..., 5]
|
|
||||||
|
|
||||||
# let r(x, y) = x^2 + y^2;
|
# let r(x, y) = x^2 + y^2;
|
||||||
# d(x, y) = 1 + k1 * r(x, y) + k2 * r(x, y) ^2 + k3 * r(x, y)^3 +
|
# alpha(x, y) = 1 + k1 * r(x, y) + k2 * r(x, y) ^2 + k3 * r(x, y)^3;
|
||||||
# k4 * r(x, y)^4;
|
# beta(x, y) = 1 + k4 * r(x, y) + k5 * r(x, y) ^2 + k6 * r(x, y)^3;
|
||||||
|
# d(x, y) = alpha(x, y) / beta(x, y);
|
||||||
r = x * x + y * y
|
r = x * x + y * y
|
||||||
d = 1.0 + r * (k1 + r * (k2 + r * (k3 + r * k4)))
|
alpha = 1.0 + r * (k1 + r * (k2 + r * k3))
|
||||||
|
beta = 1.0 + r * (k4 + r * (k5 + r * k6))
|
||||||
|
d = alpha / beta
|
||||||
|
|
||||||
# The perfect projection is:
|
# The perfect projection is:
|
||||||
# xd = x * d(x, y) + 2 * p1 * x * y + p2 * (r(x, y) + 2 * x^2);
|
# xd = x * d(x, y) + 2 * p1 * x * y + p2 * (r(x, y) + 2 * x^2);
|
||||||
@@ -344,8 +321,12 @@ def _compute_residual_and_jacobian(
|
|||||||
fx = d * x + 2 * p1 * x * y + p2 * (r + 2 * x * x) - xd
|
fx = d * x + 2 * p1 * x * y + p2 * (r + 2 * x * x) - xd
|
||||||
fy = d * y + 2 * p2 * x * y + p1 * (r + 2 * y * y) - yd
|
fy = d * y + 2 * p2 * x * y + p1 * (r + 2 * y * y) - yd
|
||||||
|
|
||||||
|
# Compute derivative of alpha, beta over r.
|
||||||
|
alpha_r = k1 + r * (2.0 * k2 + r * (3.0 * k3))
|
||||||
|
beta_r = k4 + r * (2.0 * k5 + r * (3.0 * k6))
|
||||||
|
|
||||||
# Compute derivative of d over [x, y]
|
# Compute derivative of d over [x, y]
|
||||||
d_r = k1 + r * (2.0 * k2 + r * (3.0 * k3 + r * 4.0 * k4))
|
d_r = (alpha_r * beta - alpha * beta_r) / (beta * beta)
|
||||||
d_x = 2.0 * x * d_r
|
d_x = 2.0 * x * d_r
|
||||||
d_y = 2.0 * y * d_r
|
d_y = 2.0 * y * d_r
|
||||||
|
|
||||||
@@ -373,13 +354,21 @@ def radial_and_tangential_undistort(
|
|||||||
|
|
||||||
Args:
|
Args:
|
||||||
coords: The distorted coordinates.
|
coords: The distorted coordinates.
|
||||||
distortion_params: The distortion parameters [k1, k2, k3, k4, p1, p2].
|
distortion_params: The distortion parameters. Supports 0, 1, 2, 4, 8 parameters, in
|
||||||
|
the order of [k1, k2, p1, p2, k3, k4, k5, k6].
|
||||||
eps: The epsilon for the convergence.
|
eps: The epsilon for the convergence.
|
||||||
max_iterations: The maximum number of iterations to perform.
|
max_iterations: The maximum number of iterations to perform.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
The undistorted coordinates.
|
The undistorted coordinates.
|
||||||
"""
|
"""
|
||||||
|
assert distortion_params.shape[-1] in [0, 1, 2, 4, 8]
|
||||||
|
|
||||||
|
if distortion_params.shape[-1] == 0:
|
||||||
|
return coords
|
||||||
|
elif distortion_params.shape[-1] < 8:
|
||||||
|
distortion_params = F.pad(distortion_params, (0, 8 - distortion_params.shape[-1]), "constant", 0.0)
|
||||||
|
assert distortion_params.shape[-1] == 8
|
||||||
|
|
||||||
# Initialize from the distorted point.
|
# Initialize from the distorted point.
|
||||||
x = coords[..., 0]
|
x = coords[..., 0]
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ from typing import Dict, List, Optional, Tuple, Union
|
|||||||
import cv2
|
import cv2
|
||||||
import torch
|
import torch
|
||||||
import torchvision
|
import torchvision
|
||||||
from nerfacc.cameras import opencv_lens_undistortion
|
from nerfacc.cameras import opencv_lens_undistortion, opencv_lens_undistortion_fisheye
|
||||||
from torch.nn import Parameter
|
from torch.nn import Parameter
|
||||||
from torchtyping import TensorType
|
from torchtyping import TensorType
|
||||||
|
|
||||||
@@ -59,6 +59,17 @@ CAMERA_MODEL_TO_TYPE = {
|
|||||||
"EQUIRECTANGULAR": CameraType.EQUIRECTANGULAR,
|
"EQUIRECTANGULAR": CameraType.EQUIRECTANGULAR,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# This should be consistent with the `nerfstudio/process_data/colmap_utils.py`
|
||||||
|
CAMERA_MODEL_DISTORTION_PARAMETERS = {
|
||||||
|
"SIMPLE_PINHOLE": [],
|
||||||
|
"PINHOLE": [],
|
||||||
|
"SIMPLE_RADIAL": ["k1"],
|
||||||
|
"RADIAL": ["k1", "k2"],
|
||||||
|
"OPENCV": ["k1", "k2", "p1", "p2"],
|
||||||
|
"OPENCV_FISHEYE": ["k1", "k2", "k3", "k4"],
|
||||||
|
"EQUIRECTANGULAR": [],
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@dataclass(init=False)
|
@dataclass(init=False)
|
||||||
class Cameras(TensorDataclass):
|
class Cameras(TensorDataclass):
|
||||||
@@ -78,7 +89,7 @@ class Cameras(TensorDataclass):
|
|||||||
cy: Principal point y
|
cy: Principal point y
|
||||||
width: Image width
|
width: Image width
|
||||||
height: Image height
|
height: Image height
|
||||||
distortion_params: OpenCV 6 radial distortion coefficients
|
distortion_params: OpenCV N distortion coefficients (N depends on camera type)
|
||||||
camera_type: Type of camera model. This will be an int corresponding to the CameraType enum.
|
camera_type: Type of camera model. This will be an int corresponding to the CameraType enum.
|
||||||
times: Timestamps for each camera
|
times: Timestamps for each camera
|
||||||
"""
|
"""
|
||||||
@@ -90,7 +101,7 @@ class Cameras(TensorDataclass):
|
|||||||
cy: TensorType["num_cameras":..., 1]
|
cy: TensorType["num_cameras":..., 1]
|
||||||
width: TensorType["num_cameras":..., 1]
|
width: TensorType["num_cameras":..., 1]
|
||||||
height: TensorType["num_cameras":..., 1]
|
height: TensorType["num_cameras":..., 1]
|
||||||
distortion_params: Optional[TensorType["num_cameras":..., 6]]
|
distortion_params: Optional[TensorType["num_cameras":..., "num_distortion_params"]]
|
||||||
camera_type: TensorType["num_cameras":..., 1]
|
camera_type: TensorType["num_cameras":..., 1]
|
||||||
times: Optional[TensorType["num_cameras", 1]]
|
times: Optional[TensorType["num_cameras", 1]]
|
||||||
|
|
||||||
@@ -103,7 +114,7 @@ class Cameras(TensorDataclass):
|
|||||||
cy: Union[TensorType["batch_cys":..., 1], float],
|
cy: Union[TensorType["batch_cys":..., 1], float],
|
||||||
width: Optional[Union[TensorType["batch_ws":..., 1], int]] = None,
|
width: Optional[Union[TensorType["batch_ws":..., 1], int]] = None,
|
||||||
height: Optional[Union[TensorType["batch_hs":..., 1], int]] = None,
|
height: Optional[Union[TensorType["batch_hs":..., 1], int]] = None,
|
||||||
distortion_params: Optional[TensorType["batch_dist_params":..., 6]] = None,
|
distortion_params: Optional[TensorType["batch_dist_params":..., "num_distortion_params"]] = None,
|
||||||
camera_type: Optional[
|
camera_type: Optional[
|
||||||
Union[
|
Union[
|
||||||
TensorType["batch_cam_types":..., 1],
|
TensorType["batch_cam_types":..., 1],
|
||||||
@@ -117,7 +128,7 @@ class Cameras(TensorDataclass):
|
|||||||
"""Initializes the Cameras object.
|
"""Initializes the Cameras object.
|
||||||
|
|
||||||
Note on Input Tensor Dimensions: All of these tensors have items of dimensions TensorType[3, 4]
|
Note on Input Tensor Dimensions: All of these tensors have items of dimensions TensorType[3, 4]
|
||||||
(in the case of the c2w matrices), TensorType[6] (in the case of distortion params), or
|
(in the case of the c2w matrices), TensorType[N] (in the case of distortion params with N parameters), or
|
||||||
TensorType[1] (in the case of the rest of the elements). The dimensions before that are
|
TensorType[1] (in the case of the rest of the elements). The dimensions before that are
|
||||||
considered the batch dimension of that tensor (batch_c2ws, batch_fxs, etc.). We will broadcast
|
considered the batch dimension of that tensor (batch_c2ws, batch_fxs, etc.). We will broadcast
|
||||||
all the tensors to be the same batch dimension. This means you can use any combination of the
|
all the tensors to be the same batch dimension. This means you can use any combination of the
|
||||||
@@ -310,13 +321,12 @@ class Cameras(TensorDataclass):
|
|||||||
return image_coords
|
return image_coords
|
||||||
|
|
||||||
@profiler.time_function
|
@profiler.time_function
|
||||||
@profile
|
|
||||||
def generate_rays( # pylint: disable=too-many-statements
|
def generate_rays( # pylint: disable=too-many-statements
|
||||||
self,
|
self,
|
||||||
camera_indices: Union[TensorType["num_rays":..., "num_cameras_batch_dims"], int],
|
camera_indices: Union[TensorType["num_rays":..., "num_cameras_batch_dims"], int],
|
||||||
coords: Optional[TensorType["num_rays":..., 2]] = None,
|
coords: Optional[TensorType["num_rays":..., 2]] = None,
|
||||||
camera_opt_to_camera: Optional[TensorType["num_rays":..., 3, 4]] = None,
|
camera_opt_to_camera: Optional[TensorType["num_rays":..., 3, 4]] = None,
|
||||||
distortion_params_delta: Optional[TensorType["num_rays":..., 6]] = None,
|
distortion_params_delta: Optional[TensorType["num_rays":..., "num_distortion_params"]] = None,
|
||||||
keep_shape: Optional[bool] = None,
|
keep_shape: Optional[bool] = None,
|
||||||
disable_distortion: bool = False,
|
disable_distortion: bool = False,
|
||||||
aabb_box: Optional[SceneBox] = None,
|
aabb_box: Optional[SceneBox] = None,
|
||||||
@@ -328,7 +338,7 @@ class Cameras(TensorDataclass):
|
|||||||
- camera_indices: (num_rays:..., num_cameras_batch_dims)
|
- camera_indices: (num_rays:..., num_cameras_batch_dims)
|
||||||
- coords: (num_rays:..., 2)
|
- coords: (num_rays:..., 2)
|
||||||
- camera_opt_to_camera: (num_rays:..., 3, 4) or None
|
- camera_opt_to_camera: (num_rays:..., 3, 4) or None
|
||||||
- distortion_params_delta: (num_rays:..., 6) or None
|
- distortion_params_delta: (num_rays:..., N) or None
|
||||||
|
|
||||||
Read the docstring for _generate_rays_from_coords for more information on how we generate the rays
|
Read the docstring for _generate_rays_from_coords for more information on how we generate the rays
|
||||||
after we have standardized the arguments.
|
after we have standardized the arguments.
|
||||||
@@ -436,8 +446,8 @@ class Cameras(TensorDataclass):
|
|||||||
if camera_opt_to_camera is not None
|
if camera_opt_to_camera is not None
|
||||||
else None
|
else None
|
||||||
)
|
)
|
||||||
distortion_params_delta = ( # (h, w, num_rays, 6) or None
|
distortion_params_delta = ( # (h, w, num_rays, N) or None
|
||||||
distortion_params_delta.broadcast_to(coords.shape[:-1] + (6,))
|
distortion_params_delta.broadcast_to(coords.shape[:-1] + (distortion_params_delta.shape[-1],))
|
||||||
if distortion_params_delta is not None
|
if distortion_params_delta is not None
|
||||||
else None
|
else None
|
||||||
)
|
)
|
||||||
@@ -489,13 +499,12 @@ class Cameras(TensorDataclass):
|
|||||||
return raybundle
|
return raybundle
|
||||||
|
|
||||||
# pylint: disable=too-many-statements
|
# pylint: disable=too-many-statements
|
||||||
@profile
|
|
||||||
def _generate_rays_from_coords(
|
def _generate_rays_from_coords(
|
||||||
self,
|
self,
|
||||||
camera_indices: TensorType["num_rays":..., "num_cameras_batch_dims"],
|
camera_indices: TensorType["num_rays":..., "num_cameras_batch_dims"],
|
||||||
coords: TensorType["num_rays":..., 2],
|
coords: TensorType["num_rays":..., 2],
|
||||||
camera_opt_to_camera: Optional[TensorType["num_rays":..., 3, 4]] = None,
|
camera_opt_to_camera: Optional[TensorType["num_rays":..., 3, 4]] = None,
|
||||||
distortion_params_delta: Optional[TensorType["num_rays":..., 6]] = None,
|
distortion_params_delta: Optional[TensorType["num_rays":..., "num_distortion_params"]] = None,
|
||||||
disable_distortion: bool = False,
|
disable_distortion: bool = False,
|
||||||
) -> RayBundle:
|
) -> RayBundle:
|
||||||
"""Generates rays for the given camera indices and coords where self isn't jagged
|
"""Generates rays for the given camera indices and coords where self isn't jagged
|
||||||
@@ -560,7 +569,7 @@ class Cameras(TensorDataclass):
|
|||||||
|
|
||||||
distortion_params_delta: Optional delta for the distortion parameters.
|
distortion_params_delta: Optional delta for the distortion parameters.
|
||||||
In terms of shape, it follows the same rules as coords, but indexing into it with [i:...] gets you
|
In terms of shape, it follows the same rules as coords, but indexing into it with [i:...] gets you
|
||||||
the 1D tensor with the 6 distortion parameters for the camera optimization at RayBundle[i:...].
|
the 1D tensor with the N distortion parameters for the camera optimization at RayBundle[i:...].
|
||||||
|
|
||||||
disable_distortion: If True, disables distortion.
|
disable_distortion: If True, disables distortion.
|
||||||
|
|
||||||
@@ -577,7 +586,9 @@ class Cameras(TensorDataclass):
|
|||||||
assert coords.shape == num_rays_shape + (2,)
|
assert coords.shape == num_rays_shape + (2,)
|
||||||
assert coords.shape[-1] == 2
|
assert coords.shape[-1] == 2
|
||||||
assert camera_opt_to_camera is None or camera_opt_to_camera.shape == num_rays_shape + (3, 4)
|
assert camera_opt_to_camera is None or camera_opt_to_camera.shape == num_rays_shape + (3, 4)
|
||||||
assert distortion_params_delta is None or distortion_params_delta.shape == num_rays_shape + (6,)
|
assert distortion_params_delta is None or distortion_params_delta.shape == num_rays_shape + (
|
||||||
|
distortion_params_delta.shape[-1],
|
||||||
|
)
|
||||||
|
|
||||||
# Here, we've broken our indices down along the num_cameras_batch_dims dimension allowing us to index by all
|
# Here, we've broken our indices down along the num_cameras_batch_dims dimension allowing us to index by all
|
||||||
# of our output rays at each dimension of our cameras object
|
# of our output rays at each dimension of our cameras object
|
||||||
@@ -632,26 +643,39 @@ class Cameras(TensorDataclass):
|
|||||||
|
|
||||||
# Do not apply distortion for equirectangular images
|
# Do not apply distortion for equirectangular images
|
||||||
if distortion_params is not None:
|
if distortion_params is not None:
|
||||||
mask = (self.camera_type[true_indices] != CameraType.EQUIRECTANGULAR.value).squeeze(-1) # (num_rays)
|
mask = (self.camera_type[true_indices] == CameraType.PERSPECTIVE.value).squeeze(-1) # (num_rays)
|
||||||
coord_mask = torch.stack([mask, mask, mask], dim=0)
|
coord_mask = torch.stack([mask, mask, mask], dim=0)
|
||||||
if mask.any():
|
if mask.any():
|
||||||
if distortion_params_delta is not None:
|
if distortion_params_delta is not None:
|
||||||
|
# if we need to optimize distortion params, we need to use the gradient version
|
||||||
coord_stack[coord_mask, :] = camera_utils.radial_and_tangential_undistort(
|
coord_stack[coord_mask, :] = camera_utils.radial_and_tangential_undistort(
|
||||||
coord_stack[coord_mask, :].reshape(3, -1, 2),
|
coord_stack[coord_mask, :].reshape(3, -1, 2),
|
||||||
distortion_params[mask, :],
|
distortion_params[mask, :],
|
||||||
).reshape(-1, 2)
|
).reshape(-1, 2)
|
||||||
else:
|
else:
|
||||||
# try to use nerfacc to accelerate if we don't need any gradient to optimize distortion params
|
# try to use nerfacc to accelerate if we don't need to optimize distortion params
|
||||||
try:
|
# try:
|
||||||
coord_stack[coord_mask, :] = opencv_lens_undistortion(
|
coord_stack[coord_mask, :] = opencv_lens_undistortion(
|
||||||
coord_stack[coord_mask, :].reshape(3, -1, 2),
|
coord_stack[coord_mask, :].reshape(3, -1, 2),
|
||||||
distortion_params[mask, :],
|
distortion_params[mask, :],
|
||||||
).reshape(-1, 2)
|
).reshape(-1, 2)
|
||||||
except:
|
# except:
|
||||||
coord_stack[coord_mask, :] = camera_utils.radial_and_tangential_undistort(
|
# coord_stack[coord_mask, :] = camera_utils.radial_and_tangential_undistort(
|
||||||
coord_stack[coord_mask, :].reshape(3, -1, 2),
|
# coord_stack[coord_mask, :].reshape(3, -1, 2),
|
||||||
distortion_params[mask, :],
|
# distortion_params[mask, :],
|
||||||
).reshape(-1, 2)
|
# ).reshape(-1, 2)
|
||||||
|
|
||||||
|
mask = (self.camera_type[true_indices] == CameraType.FISHEYE.value).squeeze(-1) # (num_rays)
|
||||||
|
coord_mask = torch.stack([mask, mask, mask], dim=0)
|
||||||
|
if mask.any():
|
||||||
|
if distortion_params_delta is not None:
|
||||||
|
raise NotImplementedError("Fisheye distortion optimization is not implemented")
|
||||||
|
else:
|
||||||
|
# use nerfacc with do not provide gradient
|
||||||
|
coord_stack[coord_mask, :] = opencv_lens_undistortion_fisheye(
|
||||||
|
coord_stack[coord_mask, :].reshape(3, -1, 2),
|
||||||
|
distortion_params[mask, :],
|
||||||
|
).reshape(-1, 2)
|
||||||
|
|
||||||
# Make sure after we have undistorted our images, the shapes are still correct
|
# Make sure after we have undistorted our images, the shapes are still correct
|
||||||
assert coord_stack.shape == (3,) + num_rays_shape + (2,)
|
assert coord_stack.shape == (3,) + num_rays_shape + (2,)
|
||||||
|
|||||||
@@ -100,14 +100,16 @@ class InstantNGP(DataParser):
|
|||||||
|
|
||||||
camera_to_world = torch.from_numpy(poses[:, :3]) # camera to world transform
|
camera_to_world = torch.from_numpy(poses[:, :3]) # camera to world transform
|
||||||
|
|
||||||
distortion_params = camera_utils.get_distortion_params(
|
distortion_params = torch.tensor([
|
||||||
k1=float(meta.get("k1", 0)),
|
float(meta.get("k1", 0)),
|
||||||
k2=float(meta.get("k2", 0)),
|
float(meta.get("k2", 0)),
|
||||||
k3=float(meta.get("k3", 0)),
|
float(meta.get("p1", 0)),
|
||||||
k4=float(meta.get("k4", 0)),
|
float(meta.get("p2", 0)),
|
||||||
p1=float(meta.get("p1", 0)),
|
float(meta.get("k3", 0)),
|
||||||
p2=float(meta.get("p2", 0)),
|
float(meta.get("k4", 0)),
|
||||||
)
|
float(meta.get("k5", 0)),
|
||||||
|
float(meta.get("k6", 0)),
|
||||||
|
])
|
||||||
|
|
||||||
# in x,y,z order
|
# in x,y,z order
|
||||||
# assumes that the scene is centered at the origin
|
# assumes that the scene is centered at the origin
|
||||||
|
|||||||
@@ -27,7 +27,12 @@ from rich.console import Console
|
|||||||
from typing_extensions import Literal
|
from typing_extensions import Literal
|
||||||
|
|
||||||
from nerfstudio.cameras import camera_utils
|
from nerfstudio.cameras import camera_utils
|
||||||
from nerfstudio.cameras.cameras import CAMERA_MODEL_TO_TYPE, Cameras, CameraType
|
from nerfstudio.cameras.cameras import (
|
||||||
|
CAMERA_MODEL_DISTORTION_PARAMETERS,
|
||||||
|
CAMERA_MODEL_TO_TYPE,
|
||||||
|
Cameras,
|
||||||
|
CameraType,
|
||||||
|
)
|
||||||
from nerfstudio.data.dataparsers.base_dataparser import (
|
from nerfstudio.data.dataparsers.base_dataparser import (
|
||||||
DataParser,
|
DataParser,
|
||||||
DataParserConfig,
|
DataParserConfig,
|
||||||
@@ -85,6 +90,8 @@ class Nerfstudio(DataParser):
|
|||||||
meta = load_from_json(self.config.data / "transforms.json")
|
meta = load_from_json(self.config.data / "transforms.json")
|
||||||
data_dir = self.config.data
|
data_dir = self.config.data
|
||||||
|
|
||||||
|
distortion_keys = CAMERA_MODEL_DISTORTION_PARAMETERS[meta.get("camera_model", "OPENCV")]
|
||||||
|
|
||||||
image_filenames = []
|
image_filenames = []
|
||||||
mask_filenames = []
|
mask_filenames = []
|
||||||
depth_filenames = []
|
depth_filenames = []
|
||||||
@@ -98,8 +105,8 @@ class Nerfstudio(DataParser):
|
|||||||
height_fixed = "h" in meta
|
height_fixed = "h" in meta
|
||||||
width_fixed = "w" in meta
|
width_fixed = "w" in meta
|
||||||
distort_fixed = False
|
distort_fixed = False
|
||||||
for distort_key in ["k1", "k2", "k3", "p1", "p2"]:
|
for key in distortion_keys:
|
||||||
if distort_key in meta:
|
if key in meta:
|
||||||
distort_fixed = True
|
distort_fixed = True
|
||||||
break
|
break
|
||||||
fx = []
|
fx = []
|
||||||
@@ -136,16 +143,7 @@ class Nerfstudio(DataParser):
|
|||||||
assert "w" in frame, "width not specified in frame"
|
assert "w" in frame, "width not specified in frame"
|
||||||
width.append(int(frame["w"]))
|
width.append(int(frame["w"]))
|
||||||
if not distort_fixed:
|
if not distort_fixed:
|
||||||
distort.append(
|
distort.append(torch.tensor([float(frame.get(key, 0.0)) for key in distortion_keys]))
|
||||||
camera_utils.get_distortion_params(
|
|
||||||
k1=float(frame["k1"]) if "k1" in frame else 0.0,
|
|
||||||
k2=float(frame["k2"]) if "k2" in frame else 0.0,
|
|
||||||
k3=float(frame["k3"]) if "k3" in frame else 0.0,
|
|
||||||
k4=float(frame["k4"]) if "k4" in frame else 0.0,
|
|
||||||
p1=float(frame["p1"]) if "p1" in frame else 0.0,
|
|
||||||
p2=float(frame["p2"]) if "p2" in frame else 0.0,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
image_filenames.append(fname)
|
image_filenames.append(fname)
|
||||||
poses.append(np.array(frame["transform_matrix"]))
|
poses.append(np.array(frame["transform_matrix"]))
|
||||||
@@ -264,14 +262,7 @@ class Nerfstudio(DataParser):
|
|||||||
height = int(meta["h"]) if height_fixed else torch.tensor(height, dtype=torch.int32)[idx_tensor]
|
height = int(meta["h"]) if height_fixed else torch.tensor(height, dtype=torch.int32)[idx_tensor]
|
||||||
width = int(meta["w"]) if width_fixed else torch.tensor(width, dtype=torch.int32)[idx_tensor]
|
width = int(meta["w"]) if width_fixed else torch.tensor(width, dtype=torch.int32)[idx_tensor]
|
||||||
if distort_fixed:
|
if distort_fixed:
|
||||||
distortion_params = camera_utils.get_distortion_params(
|
distortion_params = torch.tensor([float(meta.get(key, 0.0)) for key in distortion_keys])
|
||||||
k1=float(meta["k1"]) if "k1" in meta else 0.0,
|
|
||||||
k2=float(meta["k2"]) if "k2" in meta else 0.0,
|
|
||||||
k3=float(meta["k3"]) if "k3" in meta else 0.0,
|
|
||||||
k4=float(meta["k4"]) if "k4" in meta else 0.0,
|
|
||||||
p1=float(meta["p1"]) if "p1" in meta else 0.0,
|
|
||||||
p2=float(meta["p2"]) if "p2" in meta else 0.0,
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
distortion_params = torch.stack(distort, dim=0)[idx_tensor]
|
distortion_params = torch.stack(distort, dim=0)[idx_tensor]
|
||||||
|
|
||||||
|
|||||||
@@ -206,10 +206,6 @@ def parse_colmap_camera_params(camera) -> Dict[str, Any]: # pylint: disable=too
|
|||||||
out["fl_y"] = float(camera_params[0])
|
out["fl_y"] = float(camera_params[0])
|
||||||
out["cx"] = float(camera_params[1])
|
out["cx"] = float(camera_params[1])
|
||||||
out["cy"] = float(camera_params[2])
|
out["cy"] = float(camera_params[2])
|
||||||
out["k1"] = 0.0
|
|
||||||
out["k2"] = 0.0
|
|
||||||
out["p1"] = 0.0
|
|
||||||
out["p2"] = 0.0
|
|
||||||
camera_model = CameraModel.OPENCV
|
camera_model = CameraModel.OPENCV
|
||||||
elif camera.model == "PINHOLE":
|
elif camera.model == "PINHOLE":
|
||||||
# f, cx, cy, k
|
# f, cx, cy, k
|
||||||
@@ -220,10 +216,6 @@ def parse_colmap_camera_params(camera) -> Dict[str, Any]: # pylint: disable=too
|
|||||||
out["fl_y"] = float(camera_params[1])
|
out["fl_y"] = float(camera_params[1])
|
||||||
out["cx"] = float(camera_params[2])
|
out["cx"] = float(camera_params[2])
|
||||||
out["cy"] = float(camera_params[3])
|
out["cy"] = float(camera_params[3])
|
||||||
out["k1"] = 0.0
|
|
||||||
out["k2"] = 0.0
|
|
||||||
out["p1"] = 0.0
|
|
||||||
out["p2"] = 0.0
|
|
||||||
camera_model = CameraModel.OPENCV
|
camera_model = CameraModel.OPENCV
|
||||||
elif camera.model == "SIMPLE_RADIAL":
|
elif camera.model == "SIMPLE_RADIAL":
|
||||||
# f, cx, cy, k
|
# f, cx, cy, k
|
||||||
@@ -237,9 +229,6 @@ def parse_colmap_camera_params(camera) -> Dict[str, Any]: # pylint: disable=too
|
|||||||
out["cx"] = float(camera_params[1])
|
out["cx"] = float(camera_params[1])
|
||||||
out["cy"] = float(camera_params[2])
|
out["cy"] = float(camera_params[2])
|
||||||
out["k1"] = float(camera_params[3])
|
out["k1"] = float(camera_params[3])
|
||||||
out["k2"] = 0.0
|
|
||||||
out["p1"] = 0.0
|
|
||||||
out["p2"] = 0.0
|
|
||||||
camera_model = CameraModel.OPENCV
|
camera_model = CameraModel.OPENCV
|
||||||
elif camera.model == "RADIAL":
|
elif camera.model == "RADIAL":
|
||||||
# f, cx, cy, k1, k2
|
# f, cx, cy, k1, k2
|
||||||
@@ -254,8 +243,6 @@ def parse_colmap_camera_params(camera) -> Dict[str, Any]: # pylint: disable=too
|
|||||||
out["cy"] = float(camera_params[2])
|
out["cy"] = float(camera_params[2])
|
||||||
out["k1"] = float(camera_params[3])
|
out["k1"] = float(camera_params[3])
|
||||||
out["k2"] = float(camera_params[4])
|
out["k2"] = float(camera_params[4])
|
||||||
out["p1"] = 0.0
|
|
||||||
out["p2"] = 0.0
|
|
||||||
camera_model = CameraModel.OPENCV
|
camera_model = CameraModel.OPENCV
|
||||||
elif camera.model == "OPENCV":
|
elif camera.model == "OPENCV":
|
||||||
# fx, fy, cx, cy, k1, k2, p1, p2
|
# fx, fy, cx, cy, k1, k2, p1, p2
|
||||||
@@ -350,9 +337,6 @@ def parse_colmap_camera_params(camera) -> Dict[str, Any]: # pylint: disable=too
|
|||||||
out["cx"] = float(camera_params[1])
|
out["cx"] = float(camera_params[1])
|
||||||
out["cy"] = float(camera_params[2])
|
out["cy"] = float(camera_params[2])
|
||||||
out["k1"] = float(camera_params[3])
|
out["k1"] = float(camera_params[3])
|
||||||
out["k2"] = 0.0
|
|
||||||
out["k3"] = 0.0
|
|
||||||
out["k4"] = 0.0
|
|
||||||
camera_model = CameraModel.OPENCV_FISHEYE
|
camera_model = CameraModel.OPENCV_FISHEYE
|
||||||
elif camera.model == "RADIAL_FISHEYE":
|
elif camera.model == "RADIAL_FISHEYE":
|
||||||
# f, cx, cy, k1, k2
|
# f, cx, cy, k1, k2
|
||||||
@@ -374,8 +358,6 @@ def parse_colmap_camera_params(camera) -> Dict[str, Any]: # pylint: disable=too
|
|||||||
out["cy"] = float(camera_params[2])
|
out["cy"] = float(camera_params[2])
|
||||||
out["k1"] = float(camera_params[3])
|
out["k1"] = float(camera_params[3])
|
||||||
out["k2"] = float(camera_params[4])
|
out["k2"] = float(camera_params[4])
|
||||||
out["k3"] = 0
|
|
||||||
out["k4"] = 0
|
|
||||||
camera_model = CameraModel.OPENCV_FISHEYE
|
camera_model = CameraModel.OPENCV_FISHEYE
|
||||||
else:
|
else:
|
||||||
# THIN_PRISM_FISHEYE not supported!
|
# THIN_PRISM_FISHEYE not supported!
|
||||||
|
|||||||
Reference in new issue
Block a user