简体   繁体   中英

Morphology erosion - difference betwen Scipy ndimage and Scikit image

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

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM