Skip to content

Dilation and erosion image boundary extension #6665

@crisluengo

Description

@crisluengo

Description:

Currently, skimage.morphology.dilation calls scipy.ndimage.grey_dilation with the default mode value (boundary extension, this is what the filter does outside the image bounds). The default is 'reflect', which fills in values outside the image using values from inside the image. The same applies to erosion and other morphological filters.

With dilations and erosions, if the structuring element is compact, this will not matter at all. It samples some of the pixels twice. But if the structuring element is not compact, then it will sample some pixels we didn't intend to sample. See for example this Stack Overflow answer I just posted, where the structuring element doesn't include the central pixel.

Ideally, we'd extend the boundary with the minimum allowed value for the data type (or the maximum in the case of the erosion). This way, pixels outside the image will not influence the result at all. This can be accomplished by setting the mode and cval arguments to scipy.ndimage.grey_dilation appropriately:

ndi.grey_dilation(image, footprint=footprint, output=out, mode="constant", cval=np.iinfo(image.dtype).min)

(to be tested that this works OK in ndimage with 64-bit integer arrays and so forth).

Alternatively, one could consider allowing the user to select a boundary extension method.

BTW: I'm happy to send a PR, but I didn't know which solution this community preferred.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions