From 3ec10f8ea74567e9a20a81b8cc5631a0d5e6187d Mon Sep 17 00:00:00 2001 From: liorfritz Date: Wed, 10 Jan 2024 22:44:57 +0200 Subject: [PATCH] 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 Co-authored-by: alshan Co-authored-by: Justin Kerr --- .../datamanagers/full_images_datamanager.py | 26 ++++++++++++------- pyproject.toml | 2 +- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/nerfstudio/data/datamanagers/full_images_datamanager.py b/nerfstudio/data/datamanagers/full_images_datamanager.py index 92d65478c..1533b3245 100644 --- a/nerfstudio/data/datamanagers/full_images_datamanager.py +++ b/nerfstudio/data/datamanagers/full_images_datamanager.py @@ -133,7 +133,6 @@ class FullImageDatamanager(DataManager, Generic[TDataset]): continue distortion_params = camera.distortion_params.numpy() image = data["image"].numpy() - if camera.camera_type.item() == CameraType.PERSPECTIVE.value: distortion_params = np.array( [ @@ -147,13 +146,15 @@ class FullImageDatamanager(DataManager, Generic[TDataset]): 0, ] ) - 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 + if np.any(distortion_params): + 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 x, y, w, h = roi 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: data["depth_image"] = data["depth_image"][y : y + h, x : x + w] # update the width, height @@ -162,7 +163,8 @@ class FullImageDatamanager(DataManager, Generic[TDataset]): if "mask" in data: mask = data["mask"].numpy() 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] data["mask"] = torch.from_numpy(mask).bool() K = newK @@ -206,7 +208,6 @@ class FullImageDatamanager(DataManager, Generic[TDataset]): continue distortion_params = camera.distortion_params.numpy() image = data["image"].numpy() - if camera.camera_type.item() == CameraType.PERSPECTIVE.value: distortion_params = np.array( [ @@ -220,8 +221,12 @@ class FullImageDatamanager(DataManager, Generic[TDataset]): 0, ] ) - 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 + if np.any(distortion_params): + 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 x, y, w, h = roi image = image[y : y + h, x : x + w] @@ -231,7 +236,8 @@ class FullImageDatamanager(DataManager, Generic[TDataset]): if "mask" in data: mask = data["mask"].numpy() 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] data["mask"] = torch.from_numpy(mask).bool() K = newK diff --git a/pyproject.toml b/pyproject.toml index b00deccff..cb432a1a6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -33,7 +33,7 @@ dependencies = [ "msgpack_numpy>=0.4.8", "nerfacc==0.5.2", "open3d>=0.16.0", - "opencv-python==4.6.0.66", + "opencv-python==4.8.0.76", "Pillow>=9.3.0", "plotly>=5.7.0", "protobuf<=3.20.3,!=3.20.0",