4

the morphology operators differ in Scipy ndimage and Scikit image. I suppose, boundary conditions are treated in different way:

import numpy as np
from scipy import ndimage
from skimage import morphology

scp = ndimage.binary_erosion(np.ones((10,10),dtype="uint8"),).astype("uint8")
sci = morphology.binary_erosion(np.ones((10,10),dtype="uint8"),morphology.disk(1))

scp results as expected, but sci does not:

>>>> scp
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
   [0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
   [0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
   [0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
   [0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
   [0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
   [0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
   [0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
   [0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
   [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=uint8)

>>>> sci
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, 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],
   [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, 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]], dtype=uint8)

How can I set boundary condition in scikit-image morphology operators?

Best regards

Saullo G. P. Castro
  • 56,802
  • 26
  • 179
  • 234
MarcinBurz
  • 347
  • 3
  • 16
  • did you try using `morphology.square(10)` instead of `.disk(1)`? – Saullo G. P. Castro Jul 25 '14 at 14:30
  • morphology.suqare and .disk are just structuring elements, slightly different. The radius or shape of structuring elements has no effect on border behavior I described above. – MarcinBurz Jul 25 '14 at 15:56
  • It seems "border_value" parameter is missing in scikit-image morphology operators. This parameter control pixels value outside border. – MarcinBurz Jul 25 '14 at 16:14

1 Answers1

3

Ok, it is not about "border_value" parameter. I found in skimage/morphology/binary.py:

import numpy as np
from scipy import ndimage

def binary_erosion(image, selem, out=None):
    conv = ndimage.convolve(image > 0, selem, output=out,
                            mode='constant', cval=1) <---Here!
    if conv is not None:
        out = conv
    return np.equal(out, np.sum(selem), out=out)

From Scipy reference guide:

scipy.ndimage.filters.convolve(input, weights, output=None, mode='reflect', cval=0.0, origin=0):

mode : {‘reflect’,’constant’,’nearest’,’mirror’, ‘wrap’}, optional the mode parameter determines how the array borders are handled. For ‘constant’ mode, values beyond borders are set to be cval. Default is ‘reflect’. cval : scalar, optional Value to fill past edges of input if mode is ‘constant’. Default is 0.0 <-----Here!

Mystery solved!

MarcinBurz
  • 347
  • 3
  • 16