mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-12-06 07:48:50 +08:00
Undistortion should not occur for already-undistorted images (#2726)
* undistortion should not occur if pinhole (smoothes the images), and accordingly fix rounding of downscaled image size * rounding downscaling, more consistent with other resizing methods in public datasets * reverting to cpu image caching * re-allow camera unimplemented * compatibility with mask/depth * reverted scale factor rounding, corrected mask undistort condition * moved distortion_params update inside PERSPECTIVE type if-clause * bumping opencv to 4.8, to deal with undistortion bug with distortion parameters of zero --------- Co-authored-by: liorf <liorf@amazon.com> Co-authored-by: alshan <alshan@amazon.com> Co-authored-by: Justin Kerr <justin.g.kerr@gmail.com>
This commit is contained in:
2 files changed
+17
-11
No files matched your search
@@ -133,7 +133,6 @@ class FullImageDatamanager(DataManager, Generic[TDataset]):
|
|||||||
continue
|
continue
|
||||||
distortion_params = camera.distortion_params.numpy()
|
distortion_params = camera.distortion_params.numpy()
|
||||||
image = data["image"].numpy()
|
image = data["image"].numpy()
|
||||||
|
|
||||||
if camera.camera_type.item() == CameraType.PERSPECTIVE.value:
|
if camera.camera_type.item() == CameraType.PERSPECTIVE.value:
|
||||||
distortion_params = np.array(
|
distortion_params = np.array(
|
||||||
[
|
[
|
||||||
@@ -147,13 +146,15 @@ class FullImageDatamanager(DataManager, Generic[TDataset]):
|
|||||||
0,
|
0,
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
newK, roi = cv2.getOptimalNewCameraMatrix(K, distortion_params, (image.shape[1], image.shape[0]), 0)
|
if np.any(distortion_params):
|
||||||
image = cv2.undistort(image, K, distortion_params, None, newK) # type: ignore
|
newK, roi = cv2.getOptimalNewCameraMatrix(K, distortion_params, (image.shape[1], image.shape[0]), 0)
|
||||||
|
image = cv2.undistort(image, K, distortion_params, None, newK) # type: ignore
|
||||||
|
else:
|
||||||
|
newK = K
|
||||||
|
roi = 0, 0, image.shape[1], image.shape[0]
|
||||||
# crop the image and update the intrinsics accordingly
|
# crop the image and update the intrinsics accordingly
|
||||||
x, y, w, h = roi
|
x, y, w, h = roi
|
||||||
image = image[y : y + h, x : x + w]
|
image = image[y : y + h, x : x + w]
|
||||||
if "mask" in data:
|
|
||||||
data["mask"] = data["mask"][y : y + h, x : x + w]
|
|
||||||
if "depth_image" in data:
|
if "depth_image" in data:
|
||||||
data["depth_image"] = data["depth_image"][y : y + h, x : x + w]
|
data["depth_image"] = data["depth_image"][y : y + h, x : x + w]
|
||||||
# update the width, height
|
# update the width, height
|
||||||
@@ -162,7 +163,8 @@ class FullImageDatamanager(DataManager, Generic[TDataset]):
|
|||||||
if "mask" in data:
|
if "mask" in data:
|
||||||
mask = data["mask"].numpy()
|
mask = data["mask"].numpy()
|
||||||
mask = mask.astype(np.uint8) * 255
|
mask = mask.astype(np.uint8) * 255
|
||||||
mask = cv2.undistort(mask, K, distortion_params, None, newK) # type: ignore
|
if np.any(distortion_params):
|
||||||
|
mask = cv2.undistort(mask, K, distortion_params, None, newK) # type: ignore
|
||||||
mask = mask[y : y + h, x : x + w]
|
mask = mask[y : y + h, x : x + w]
|
||||||
data["mask"] = torch.from_numpy(mask).bool()
|
data["mask"] = torch.from_numpy(mask).bool()
|
||||||
K = newK
|
K = newK
|
||||||
@@ -206,7 +208,6 @@ class FullImageDatamanager(DataManager, Generic[TDataset]):
|
|||||||
continue
|
continue
|
||||||
distortion_params = camera.distortion_params.numpy()
|
distortion_params = camera.distortion_params.numpy()
|
||||||
image = data["image"].numpy()
|
image = data["image"].numpy()
|
||||||
|
|
||||||
if camera.camera_type.item() == CameraType.PERSPECTIVE.value:
|
if camera.camera_type.item() == CameraType.PERSPECTIVE.value:
|
||||||
distortion_params = np.array(
|
distortion_params = np.array(
|
||||||
[
|
[
|
||||||
@@ -220,8 +221,12 @@ class FullImageDatamanager(DataManager, Generic[TDataset]):
|
|||||||
0,
|
0,
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
newK, roi = cv2.getOptimalNewCameraMatrix(K, distortion_params, (image.shape[1], image.shape[0]), 0)
|
if np.any(distortion_params):
|
||||||
image = cv2.undistort(image, K, distortion_params, None, newK) # type: ignore
|
newK, roi = cv2.getOptimalNewCameraMatrix(K, distortion_params, (image.shape[1], image.shape[0]), 0)
|
||||||
|
image = cv2.undistort(image, K, distortion_params, None, newK) # type: ignore
|
||||||
|
else:
|
||||||
|
newK = K
|
||||||
|
roi = 0, 0, image.shape[1], image.shape[0]
|
||||||
# crop the image and update the intrinsics accordingly
|
# crop the image and update the intrinsics accordingly
|
||||||
x, y, w, h = roi
|
x, y, w, h = roi
|
||||||
image = image[y : y + h, x : x + w]
|
image = image[y : y + h, x : x + w]
|
||||||
@@ -231,7 +236,8 @@ class FullImageDatamanager(DataManager, Generic[TDataset]):
|
|||||||
if "mask" in data:
|
if "mask" in data:
|
||||||
mask = data["mask"].numpy()
|
mask = data["mask"].numpy()
|
||||||
mask = mask.astype(np.uint8) * 255
|
mask = mask.astype(np.uint8) * 255
|
||||||
mask = cv2.undistort(mask, K, distortion_params, None, newK) # type: ignore
|
if np.any(distortion_params):
|
||||||
|
mask = cv2.undistort(mask, K, distortion_params, None, newK) # type: ignore
|
||||||
mask = mask[y : y + h, x : x + w]
|
mask = mask[y : y + h, x : x + w]
|
||||||
data["mask"] = torch.from_numpy(mask).bool()
|
data["mask"] = torch.from_numpy(mask).bool()
|
||||||
K = newK
|
K = newK
|
||||||
|
|||||||
+1
-1
@@ -33,7 +33,7 @@ dependencies = [
|
|||||||
"msgpack_numpy>=0.4.8",
|
"msgpack_numpy>=0.4.8",
|
||||||
"nerfacc==0.5.2",
|
"nerfacc==0.5.2",
|
||||||
"open3d>=0.16.0",
|
"open3d>=0.16.0",
|
||||||
"opencv-python==4.6.0.66",
|
"opencv-python==4.8.0.76",
|
||||||
"Pillow>=9.3.0",
|
"Pillow>=9.3.0",
|
||||||
"plotly>=5.7.0",
|
"plotly>=5.7.0",
|
||||||
"protobuf<=3.20.3,!=3.20.0",
|
"protobuf<=3.20.3,!=3.20.0",
|
||||||
|
|||||||
Reference in new issue
Block a user