Skip to content

Inconsistent 2D vs 3D interpolation behavior for mode 'reflect' #1585

Closed
scipy/scipy
#12767

Description

@grlee77

Currently, interpolation gives different results than one would expect in the 3D case for mode reflect. Likely this is a problem in ndi.map_coordinates rather than scikit-image itself, but a potential workaround I propose below is to call map_coordinates with mode 'mirror' instead of reflect.

Here is the output of test_resize2d from skimage/transforms/tests/test_warps.py if I change the mode from 'constant' to 'reflect'. This tests still passes and looks as expected:
reflect_2d

However, for test_resize3d_resize, the output is not as expected, and the test fails:
reflect_3d

If I change the mode passed to ndi.map_coordinates to 'mirror', rather than 'reflect', the expected result is restored.
mirror_3d

The code producing the above figures is as follows:

import numpy as np
from skimage.transform import resize
from matplotlib import pyplot as plt


""" 2D reflect test """

# copied from test_resize2d()
x = np.zeros((5, 5), dtype=np.double)
x[1, 1] = 1
resized = resize(x, (10, 10), order=0, mode='reflect')
ref = np.zeros((10, 10))
ref[2:4, 2:4] = 1

fig, axes = plt.subplots(1, 3)
axes[0].imshow(x, interpolation='nearest', cmap=plt.cm.gray)
axes[0].set_axis_off()
axes[0].set_title("original")
axes[1].imshow(resized, interpolation='nearest', cmap=plt.cm.gray)
axes[1].set_axis_off()
axes[1].set_title("resized")
axes[2].imshow(ref, interpolation='nearest', cmap=plt.cm.gray)
axes[2].set_axis_off()
axes[2].set_title("expected result")


""" 3D reflect test """

# copied from test_resize3d_resize()
x = np.zeros((5, 5, 3), dtype=np.double)
x[1, 1, :] = 1
resized = resize(x, (10, 10, 1), order=0, mode='reflect')
ref = np.zeros((10, 10, 1))
ref[2:4, 2:4] = 1

fig, axes = plt.subplots(1, 3)
axes[0].imshow(x[:, :, 1], interpolation='nearest', cmap=plt.cm.gray)
axes[0].set_axis_off()
axes[0].set_title("original[:, :, 1]")
axes[1].imshow(resized[:, :, 0], interpolation='nearest', cmap=plt.cm.gray)
axes[1].set_axis_off()
axes[1].set_title("resized")
axes[2].imshow(ref[:, :, 0], interpolation='nearest', cmap=plt.cm.gray)
axes[2].set_axis_off()
axes[2].set_title("expected result")


""" 3D mirror test """

# copied from test_resize3d_resize()
x = np.zeros((5, 5, 3), dtype=np.double)
x[1, 1, :] = 1
resized = resize(x, (10, 10, 1), order=0, mode='mirror')
ref = np.zeros((10, 10, 1))
ref[2:4, 2:4] = 1

fig, axes = plt.subplots(1, 3)
axes[0].imshow(x[:, :, 1], interpolation='nearest', cmap=plt.cm.gray)
axes[0].set_axis_off()
axes[0].set_title("original[:, :, 1]")
axes[1].imshow(resized[:, :, 0], interpolation='nearest', cmap=plt.cm.gray)
axes[1].set_axis_off()
axes[1].set_title("resized")
axes[2].imshow(ref[:, :, 0], interpolation='nearest', cmap=plt.cm.gray)
axes[2].set_axis_off()
axes[2].set_title("expected result")

Activity

  1. emmanuelle commented on Aug 27, 2015

    @emmanuelle
    Member

    @grlee77 Was this issue solved by #1583? Can we close it?

  2. grlee77 commented on Aug 28, 2015

    @grlee77
    ContributorAuthor

    Not quite yet. Now that #1583 has been merged I need to submit a small PR to change the default mode from constant to symmetric as discussed in #1514.

  3. grlee77 commented on Aug 28, 2015

    @grlee77
    ContributorAuthor

    I think due to some bugs in ndimage.map_coordinates, it will not be possible to resolve this for all edge cases. The cleanest solution would be to update the skimage interpolation code to work for 3D as well, but I don't have the free time to work on that in the immediate future.

    From current master, edge mode 'symmetric' results in disagreement for 2D and 3D images in the example above. For the 'symmetric' case (equivalent of 'reflect' in ndimage) I get the following questionable output from ndimage in a simple 1D example with equivalent x-coordinate range as the 2D example above:

    import scipy.ndimage as ndi
    import numpy as np
    x = np.arange(5.)
    coords =  [np.arange(-0.25, 4.5, 0.25), ]    # 0.5 * (x - 0.5) + 0.5
    ndi.map_coordinates(x, coords, order=0, mode='reflect')

    outputs

    array([1, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4])

    I think that first value should be a 0

    If I extend the coordinate range further, this becomes more apparent

    In[57]:  ndi.map_coordinates(np.arange(5.), [np.arange(-2.25, 4.5, 0.25)], order=0, mode='reflect')
    Out[57]: 
    array([ 1.,  1.,  1.,  1.,  0.,  0.,  0.,  0.,  1.,  0.,  0.,  1.,  1.,
            1.,  1.,  2.,  2.,  2.,  2.,  3.,  3.,  3.,  3.,  4.,  4.,  4.,  4.])
  4. stefanv commented on Aug 28, 2015

    @stefanv
    Member

    Wouldn't fixing this directly in scipy.ndimage be the easiest
    approach? I'd be happy to help review and merge such a PR.

  5. sciunto commented on Aug 21, 2016

    @sciunto
    Member

    @grlee77 Would you like to open a bug report to scipy?

  6. grlee77 commented on Aug 23, 2016

    @grlee77
    ContributorAuthor

    @sciunto. There is a related issue open at scipy/scipy#2640
    I did spend a few hours looking at it some time ago, but did not come up with a fix that didn't also break some other existing tests. I doubt I will have the time to revisit it in the near future, but would definitely like to sort it out at some point.

  7. sciunto commented on Aug 24, 2016

    @sciunto
    Member

    Thanks for the link.

  8. rfezzani commented on Feb 19, 2020

    @rfezzani
    Member

    I can't reproduce this one. Closing! ;-)

  9. stefanv commented on Feb 21, 2020

    @stefanv
    Member

    Checking in with @grlee77 as to whether he considers this resolved.

  10. grlee77 commented on Feb 21, 2020

    @grlee77
    ContributorAuthor

    Actually, this particular inconsistency between 2D and 3D still exists, it just no longer occurs for the default "reflect" mode due to the renaming for consistency with numpy.pad that took place in #1583.

    An updated code to reproduce the issue is given below by swapping out the name 'reflect' for 'symmetric'

    # 2D symmetric test
    
    x = np.zeros((5, 5), dtype=np.double)
    x[1, 1] = 1
    resized = resize(x, (10, 10), order=0, mode='symmetric')
    ref = np.zeros((10, 10))
    ref[2:4, 2:4] = 1
    
    fig, axes = plt.subplots(1, 3)
    axes[0].imshow(x, interpolation='nearest', cmap=plt.cm.gray)
    axes[0].set_axis_off()
    axes[0].set_title("original")
    axes[1].imshow(resized, interpolation='nearest', cmap=plt.cm.gray)
    axes[1].set_axis_off()
    axes[1].set_title("resized")
    axes[2].imshow(ref, interpolation='nearest', cmap=plt.cm.gray)
    axes[2].set_axis_off()
    axes[2].set_title("expected result")
    
    
    """ 3D symmetric test """
    
    # copied from test_resize3d_resize()
    x = np.zeros((5, 5, 3), dtype=np.double)
    x[1, 1, :] = 1
    resized = resize(x, (10, 10, 1), order=0, mode='symmetric')
    ref = np.zeros((10, 10, 1))
    ref[2:4, 2:4] = 1
    
    fig, axes = plt.subplots(1, 3)
    axes[0].imshow(x[:, :, 1], interpolation='nearest', cmap=plt.cm.gray)
    axes[0].set_axis_off()
    axes[0].set_title("original[:, :, 1]")
    axes[1].imshow(resized[:, :, 0], interpolation='nearest', cmap=plt.cm.gray)
    axes[1].set_axis_off()
    axes[1].set_title("resized")
    axes[2].imshow(ref[:, :, 0], interpolation='nearest', cmap=plt.cm.gray)
    axes[2].set_axis_off()
    axes[2].set_title("expected result")

    There is still the related upstream issue that scipy.ndimage's wrap differs from np.pad or OpenCV as illustrated in this comment: #1583 (comment)

  11. rfezzani commented on Feb 22, 2020

    @rfezzani
    Member

    OK, sorry, I see the problem now, it comes from the mismatch between scipy and skimage behavior on 'symmetric' mode. Isn't the wrap mode also affected according to your scipy/scipy#2640 (comment)?

  12. grlee77 commented on Nov 13, 2020

    @grlee77
    ContributorAuthor

    I have confirmed that this issue is closed by scipy/scipy#12767 which will be included in the upcoming 1.6.0 release in a couple of weeks.

    This one has been a long time coming (5.5 years!)

  13. jni commented on Nov 14, 2020

    @jni
    Member

    So happy about this one @grlee77! ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions