Skip to content

Commit

Permalink
workaround for cv2.remap nan behaviour + remove convertMaps
Browse files Browse the repository at this point in the history
  • Loading branch information
dugalh committed Sep 26, 2023
1 parent 9a56bdb commit 81bfb7e
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
4 changes: 2 additions & 2 deletions simple_ortho/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ def _get_undistort_maps(self, alpha: float) -> Tuple[Tuple[np.ndarray, np.ndarra
undistort_maps = cv2.initUndistortRectifyMap(
self._K, self._dist_param, np.eye(3), K_undistort, im_size, cv2.CV_16SC2
)
undistort_maps = cv2.convertMaps(*undistort_maps, cv2.CV_16SC2)
# undistort_maps = cv2.convertMaps(*undistort_maps, cv2.CV_16SC2)
return undistort_maps, K_undistort

def _world_to_pixel(self, xyz: np.ndarray) -> np.ndarray:
Expand Down Expand Up @@ -550,7 +550,7 @@ def _get_undistort_maps(self, alpha: float) -> Tuple[Tuple[np.ndarray, np.ndarra
undistort_maps = cv2.fisheye.initUndistortRectifyMap(
self._K, self._dist_param, np.eye(3), K_undistort, im_size, cv2.CV_16SC2
)
undistort_maps = cv2.convertMaps(*undistort_maps, cv2.CV_16SC2)
# undistort_maps = cv2.convertMaps(*undistort_maps, cv2.CV_16SC2)
return undistort_maps, K_undistort

def _world_to_pixel(self, xyz: np.ndarray) -> np.ndarray:
Expand Down
6 changes: 4 additions & 2 deletions simple_ortho/ortho.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ def inv_transform(transform: rio.Affine, xy: np.array):
# masked / nan dem pixels)
# TODO: deal with boundary issues (round or remap)
dem_ji = inv_transform(dem_transform, ray_xyz[:2, :]).astype('float32', copy=False)
dem_ji = cv2.convertMaps(*dem_ji, cv2.CV_16SC2)
# dem_ji = cv2.convertMaps(*dem_ji, cv2.CV_16SC2)
dem_z = np.squeeze(cv2.remap(
dem_array, *dem_ji, dem_interp.to_cv(), borderMode=cv2.BORDER_CONSTANT, borderValue=float('nan')
), axis=1) # yapf: disable
Expand Down Expand Up @@ -427,9 +427,11 @@ def _remap_tile(
) # yapf: disable

# separate tile_ji into (j, i) grids, converting to float32 for compatibility with cv2.remap
# (nans are converted to -1 as cv2.remap maps nans to the first src pixel on some packages/platforms)
tile_ji[np.isnan(tile_ji)] = -1
tile_jgrid = tile_ji[0, :].reshape(tile_win.height, tile_win.width).astype('float32')
tile_igrid = tile_ji[1, :].reshape(tile_win.height, tile_win.width).astype('float32')
tile_jgrid, tile_igrid = cv2.convertMaps(tile_jgrid, tile_igrid, cv2.CV_16SC2)
# tile_jgrid, tile_igrid = cv2.convertMaps(tile_jgrid, tile_igrid, cv2.CV_16SC2)

# initialise ortho tile array
tile_array = np.full(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_ortho.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ def test_mask_dem(
src_im, dem_array.shape, dem_transform, dtype='uint8', compress=Compress.deflate
)
with rio.open(ortho_file, 'w', **ortho_profile) as ortho_im:
ortho._remap(src_im, ortho_im, dem_array, interp=Interp.bilinear, full_remap=True, write_mask=False)
ortho._remap(src_im, ortho_im, dem_array, full_remap=True, write_mask=False)

# create the dem mask
dem_array_mask, dem_transform_mask = ortho._mask_dem(
Expand Down

0 comments on commit 81bfb7e

Please sign in to comment.