diff --git a/skimage/_shared/interpolation.pyx b/skimage/_shared/interpolation.pyx index bcebd78d..fc247530 100644 --- a/skimage/_shared/interpolation.pyx +++ b/skimage/_shared/interpolation.pyx @@ -19,8 +19,8 @@ cdef inline double nearest_neighbour_interpolation(double* image, int rows, Shape of image. r, c : int Position at which to interpolate. - mode : {'C', 'W', 'R'} - Wrapping mode. Constant, Wrap or Reflect. + mode : {'C', 'W', 'R', 'N'} + Wrapping mode. Constant, Wrap, Reflect or Nearest. cval : double Constant value to use for constant mode. @@ -48,8 +48,8 @@ cdef inline double bilinear_interpolation(double* image, int rows, int cols, Shape of image. r, c : int Position at which to interpolate. - mode : {'C', 'W', 'R'} - Wrapping mode. Constant, Wrap or Reflect. + mode : {'C', 'W', 'R', 'N'} + Wrapping mode. Constant, Wrap, Reflect or Nearest. cval : double Constant value to use for constant mode. @@ -111,8 +111,8 @@ cdef inline double bicubic_interpolation(double* image, int rows, int cols, Shape of image. r, c : int Position at which to interpolate. - mode : {'C', 'W', 'R'} - Wrapping mode. Constant, Wrap or Reflect. + mode : {'C', 'W', 'R', 'N'} + Wrapping mode. Constant, Wrap, Reflect or Nearest. cval : double Constant value to use for constant mode. @@ -160,8 +160,8 @@ cdef inline double get_pixel(double* image, int rows, int cols, int r, int c, Shape of image. r, c : int Position at which to get the pixel. - mode : {'C', 'W', 'R'} - Wrapping mode. Constant, Wrap or Reflect. + mode : {'C', 'W', 'R', 'N'} + Wrapping mode. Constant, Wrap, Reflect or Nearest. cval : double Constant value to use for constant mode. @@ -190,28 +190,33 @@ cdef inline int coord_map(int dim, int coord, char mode): Maximum coordinate. coord : int Coord provided by user. May be < 0 or > dim. - mode : {'W', 'R'} + mode : {'W', 'R', 'N'} Whether to wrap or reflect the coordinate if it falls outside [0, dim). """ dim = dim - 1 if mode == 'R': # reflect - if (coord < 0): + if coord < 0: # How many times times does the coordinate wrap? - if ((-coord / dim) % 2 != 0): + if (-coord / dim) % 2 != 0: return dim - (-coord % dim) else: return (-coord % dim) - elif (coord > dim): - if ((coord / dim) % 2 != 0): + elif coord > dim: + if (coord / dim) % 2 != 0: return (dim - (coord % dim)) else: return (coord % dim) elif mode == 'W': # wrap - if (coord < 0): + if coord < 0: return (dim - (-coord % dim)) - elif (coord > dim): + elif coord > dim: return (coord % dim) + elif mode == 'N': # nearest + if coord < 0: + return 0 + elif coord > dim: + return dim return coord diff --git a/skimage/transform/_geometric.py b/skimage/transform/_geometric.py index 95af55d3..02b6c161 100644 --- a/skimage/transform/_geometric.py +++ b/skimage/transform/_geometric.py @@ -832,11 +832,8 @@ def warp(image, inverse_map=None, map_args={}, output_shape=None, order=1, ishape = np.array(image.shape) bands = ishape[2] - # use fast Cython version for specific parameters - fast_modes = ('constant', 'reflect', 'wrap') - fast_orders = (0, 1, 3) - - if order in fast_orders and mode in fast_modes and not map_args: + # use fast Cython version for specific interpolation orders + if order in (0, 1, 3) and not map_args: matrix = None if isinstance(inverse_map, HOMOGRAPHY_TRANSFORMS): matrix = inverse_map._matrix diff --git a/skimage/transform/_warps_cy.pyx b/skimage/transform/_warps_cy.pyx index 99eb2500..bedbfa61 100644 --- a/skimage/transform/_warps_cy.pyx +++ b/skimage/transform/_warps_cy.pyx @@ -86,9 +86,9 @@ def _warp_fast(np.ndarray image, np.ndarray H, output_shape=None, int order=1, cdef np.ndarray[dtype=np.double_t, ndim=2, mode="c"] M = \ np.ascontiguousarray(H) - if mode not in ('constant', 'wrap', 'reflect'): + if mode not in ('constant', 'wrap', 'reflect', 'nearest'): raise ValueError("Invalid mode specified. Please use " - "`constant`, `wrap` or `reflect`.") + "`constant`, `nearest`, `wrap` or `reflect`.") cdef char mode_c = ord(mode[0].upper()) cdef int out_r, out_c diff --git a/skimage/transform/tests/test_warps.py b/skimage/transform/tests/test_warps.py index 41d1a46a..65514073 100644 --- a/skimage/transform/tests/test_warps.py +++ b/skimage/transform/tests/test_warps.py @@ -58,7 +58,7 @@ def test_fast_homography(): coords = warp_coords(tform.inverse, (img.shape[0], img.shape[1])) for order in range(4): - for mode in ('constant', 'reflect', 'wrap'): + for mode in ('constant', 'reflect', 'wrap', 'nearest'): p0 = map_coordinates(img, coords, mode=mode, order=order) p1 = warp(img, tform, mode=mode, order=order)