From 1cd72e67ab7fb2956c9795ec1edc778ff41740b6 Mon Sep 17 00:00:00 2001 From: "Gregory R. Lee" Date: Tue, 7 Jul 2015 17:45:47 -0400 Subject: [PATCH] add visual example of edge modes --- doc/examples/plot_edge_modes.py | 36 +++++++++++++++++++++ skimage/_shared/_interpolation_test.pyx | 28 +++++++++++++++- skimage/_shared/tests/test_interpolation.py | 3 +- 3 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 doc/examples/plot_edge_modes.py diff --git a/doc/examples/plot_edge_modes.py b/doc/examples/plot_edge_modes.py new file mode 100644 index 00000000000..bb99ac68c43 --- /dev/null +++ b/doc/examples/plot_edge_modes.py @@ -0,0 +1,36 @@ +""" +========================= +Interpolation: Edge Modes +========================= + +This example illustrates the different edge modes available during +interpolation in routines such as ``skimage.transform.rescale`` and +``skimage.transform.resize``. +""" +from skimage._shared._interpolation_test import extend_image +import skimage.data +import matplotlib.pyplot as plt +import numpy as np + +img = np.zeros((16, 16)) +img[:8, :8] += 1 +img[:4, :4] += 1 +img[:2, :2] += 1 +img[:1, :1] += 2 + +modes = ['constant', 'nearest', 'wrap', 'reflect'] +fig, axes = plt.subplots(1, 4, figsize=(12, 3)) +for n, mode in enumerate(modes): + img_extended = extend_image(img, pad=img.shape[0], mode=mode) + axes[n].imshow(img_extended, cmap=plt.cm.gray, interpolation='nearest') + axes[n].plot([15.5, 15.5], [15.5, 31.5], 'y--', linewidth=0.5) + axes[n].plot([31.5, 31.5], [15.5, 31.5], 'y--', linewidth=0.5) + axes[n].plot([15.5, 31.5], [15.5, 15.5], 'y--', linewidth=0.5) + axes[n].plot([15.5, 31.5], [31.5, 31.5], 'y--', linewidth=0.5) + axes[n].set_axis_off() + axes[n].set_aspect('equal') + axes[n].set_title(mode) + +plt.tight_layout() + +plt.show() \ No newline at end of file diff --git a/skimage/_shared/_interpolation_test.pyx b/skimage/_shared/_interpolation_test.pyx index 1c24392642d..fa11382ffd3 100644 --- a/skimage/_shared/_interpolation_test.pyx +++ b/skimage/_shared/_interpolation_test.pyx @@ -1,5 +1,31 @@ from interpolation cimport coord_map as _coord_map +from interpolation cimport get_pixel2d +import numpy as np +cimport numpy as cnp + def coord_map(Py_ssize_t dim, long coord, mode): + """ interpolation.coord_map python wrapper """ cdef char mode_c = ord(mode[0].upper()) - return _coord_map(dim, coord, mode_c) \ No newline at end of file + return _coord_map(dim, coord, mode_c) + + +def extend_image(image, pad=10, mode='C', cval=0): + """ can be used to verify proper get_pixel2d behavior. """ + cdef: + Py_ssize_t rows = image.shape[0] + Py_ssize_t cols = image.shape[1] + long ro, co + char mode_c = ord(mode[0].upper()) + + image = np.ascontiguousarray(image.astype(np.float64)) + output_shape = np.asarray(image.shape) + 2*pad + image_out = np.zeros(output_shape, dtype=image.dtype) + for r in range(-pad, rows+pad): + for c in range(-pad, cols+pad): + ro = r + pad + co = c + pad + image_out[ro, co] = get_pixel2d( cnp.PyArray_DATA(image), + rows, cols, r, c, + mode_c, cval) + return image_out diff --git a/skimage/_shared/tests/test_interpolation.py b/skimage/_shared/tests/test_interpolation.py index 98bafb5b135..261fbe99ccc 100644 --- a/skimage/_shared/tests/test_interpolation.py +++ b/skimage/_shared/tests/test_interpolation.py @@ -1,6 +1,7 @@ from skimage._shared._interpolation_test import coord_map from numpy.testing import assert_array_equal + def test_coord_map(): reflect = [coord_map(4, n, 'R') for n in range(-6, 6)] @@ -16,4 +17,4 @@ def test_coord_map(): assert_array_equal(nearest, expected_neareset) other = [coord_map(4, n, 'undefined') for n in range(-6, 6)] - assert_array_equal(other, list(range(-6, 6))) \ No newline at end of file + assert_array_equal(other, list(range(-6, 6)))