loli / medpy

Medical image processing in Python
http://loli.github.io/medpy/
GNU General Public License v3.0
571 stars 138 forks source link

medpy.filter.utilities.pad() creates 0 line when mode = 'mirror' #8

Closed AlexanderRuesch closed 9 years ago

AlexanderRuesch commented 9 years ago

When the pad function is called with mode='mirror' and size >> image.shape the returned array starts with lines of zeros.

Example:

In [1]: import numpy

In [2]: from medpy.filter.utilities import pad

In [3]: test = numpy.ones([3,3])

In [4]: pad(test, size=3,mode='mirror')
Out[4]: 
array([[ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.]])

In [5]: pad(test, size=7,mode='mirror')
Out[5]: 
array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 0.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 0.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 0.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 0.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 0.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 0.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 0.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.]])

In [6]: pad(test, size=9,mode='mirror')
Out[6]: 
array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 0.,  0.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 0.,  0.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 0.,  0.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 0.,  0.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 0.,  0.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 0.,  0.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 0.,  0.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.],
       [ 0.,  0.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.]])
loli commented 9 years ago

Thank you, I took a look into this.

The bug stems from the way the padded areas are filled in the case of the modes mirror, reflect and warp: Since the values from the original array are used for filling, they run out of values for size > 2*array.shape for any dimension.

This will be very seldom the case. For now, I've changed the function to raise an Exception when the requirement is not met. You can find the fix in the latest version of MedPy on GitHub.

Since the function is to be replaced by numpy's native pad function, I won't fix this bug.

Best, loli