Repository navigation
Closed
scipy/scipy
#12767Closed
Copy link
Description
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:

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

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

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")Metadata
Metadata
Assignees
Labels
No labels
Activity
emmanuelle commentedon Aug 27, 2015
@grlee77 Was this issue solved by #1583? Can we close it?
grlee77 commentedon Aug 28, 2015
Not quite yet. Now that #1583 has been merged I need to submit a small PR to change the default mode from
constanttosymmetricas discussed in #1514.grlee77 commentedon Aug 28, 2015
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
ndimagein a simple 1D example with equivalent x-coordinate range as the 2D example above:outputs
I think that first value should be a 0
If I extend the coordinate range further, this becomes more apparent
stefanv commentedon Aug 28, 2015
Wouldn't fixing this directly in
scipy.ndimagebe the easiestapproach? I'd be happy to help review and merge such a PR.
sciunto commentedon Aug 21, 2016
@grlee77 Would you like to open a bug report to scipy?
grlee77 commentedon Aug 23, 2016
@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.
sciunto commentedon Aug 24, 2016
Thanks for the link.
rfezzani commentedon Feb 19, 2020
I can't reproduce this one. Closing! ;-)
stefanv commentedon Feb 21, 2020
Checking in with @grlee77 as to whether he considers this resolved.
grlee77 commentedon Feb 21, 2020
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 withnumpy.padthat took place in #1583.An updated code to reproduce the issue is given below by swapping out the name 'reflect' for 'symmetric'
There is still the related upstream issue that
scipy.ndimage's wrap differs fromnp.pador OpenCV as illustrated in this comment: #1583 (comment)rfezzani commentedon Feb 22, 2020
OK, sorry, I see the problem now, it comes from the mismatch between scipy and skimage behavior on 'symmetric' mode. Isn't the
wrapmode also affected according to your scipy/scipy#2640 (comment)?grlee77 commentedon Nov 13, 2020
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!)
jni commentedon Nov 14, 2020
So happy about this one @grlee77! ❤️