From 9adbca13e774849751405c4fc804af08b2470eed Mon Sep 17 00:00:00 2001 From: Tony S Yu Date: Fri, 14 Oct 2011 11:00:10 -0400 Subject: [PATCH] Add parameter to flip structuring element about its origin This parameter only affects structuring elements (selem) with even-numbered sides, since an odd-numbered selem is centered about its origin. Note the selem isn't actually flipped, but shifted (i.e. I assume a selem that is symmetric in *shape*) --- scikits/image/morphology/cmorph.pyx | 15 +++++++++++++-- scikits/image/morphology/grey.py | 17 +++++++++++++---- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/scikits/image/morphology/cmorph.pyx b/scikits/image/morphology/cmorph.pyx index 5572102c..8ca24752 100644 --- a/scikits/image/morphology/cmorph.pyx +++ b/scikits/image/morphology/cmorph.pyx @@ -8,6 +8,7 @@ import numpy as np cimport numpy as np cimport cython +from cpython cimport bool STREL_DTYPE = np.uint8 ctypedef np.uint8_t STREL_DTYPE_t @@ -21,9 +22,14 @@ cdef inline int int_min(int a, int b): return a if a <= b else b @cython.boundscheck(False) def dilate(np.ndarray[IMAGE_DTYPE_t, ndim=2] image not None, np.ndarray[IMAGE_DTYPE_t, ndim=2] selem not None, - np.ndarray[IMAGE_DTYPE_t, ndim=2] out): + np.ndarray[IMAGE_DTYPE_t, ndim=2] out, + bool flip): cdef int hw = selem.shape[0] // 2 cdef int hh = selem.shape[1] // 2 + if flip: + hw -= 1 + hh -= 1 + cdef int width = image.shape[0], height = image.shape[1] if out is None: out = np.zeros([width, height], dtype=IMAGE_DTYPE) @@ -64,9 +70,14 @@ def dilate(np.ndarray[IMAGE_DTYPE_t, ndim=2] image not None, @cython.boundscheck(False) def erode(np.ndarray[IMAGE_DTYPE_t, ndim=2] image not None, np.ndarray[IMAGE_DTYPE_t, ndim=2] selem not None, - np.ndarray[IMAGE_DTYPE_t, ndim=2] out): + np.ndarray[IMAGE_DTYPE_t, ndim=2] out, + bool flip): cdef int hw = selem.shape[0] // 2 cdef int hh = selem.shape[1] // 2 + if flip: + hw -= 1 + hh -= 1 + cdef int width = image.shape[0], height = image.shape[1] if out is None: out = np.zeros([width, height], dtype=IMAGE_DTYPE) diff --git a/scikits/image/morphology/grey.py b/scikits/image/morphology/grey.py index 2eb94024..5592850f 100644 --- a/scikits/image/morphology/grey.py +++ b/scikits/image/morphology/grey.py @@ -9,7 +9,7 @@ import numpy as np eps = np.finfo(float).eps -def greyscale_erode(image, selem, out=None): +def greyscale_erode(image, selem, out=None, flip=False): """ Performs a greyscale morphological erosion on an image given a flat structuring element. The eroded pixel at (i,j) is the minimum over all @@ -27,6 +27,10 @@ def greyscale_erode(image, selem, out=None): The array to store the result of the morphology. If None is passed, a new array will be allocated. + flip : bool + Flip structuring element about center point. This only affects + eccentric structuring elements (i.e. selem with even numbered sides). + Returns ------- eroded : ndarray @@ -36,12 +40,12 @@ def greyscale_erode(image, selem, out=None): raise NotImplementedError("In-place erosion not supported!") try: import scikits.image.morphology.cmorph as cmorph - out = cmorph.erode(image, selem, out=out) + out = cmorph.erode(image, selem, out=out, flip=flip) return out; except ImportError: raise ImportError("cmorph extension not available.") -def greyscale_dilate(image, selem, out=None): +def greyscale_dilate(image, selem, out=None, flip=False): """ Performs a greyscale morphological dilation on an image given a flat structuring element. The dilated pixel at (i,j) is the maximum over all @@ -60,6 +64,10 @@ def greyscale_dilate(image, selem, out=None): The array to store the result of the morphology. If None, is passed, a new array will be allocated. + flip : bool + Flip structuring element about center point. This only affects + eccentric structuring elements (i.e. selem with even numbered sides). + Returns ------- dilated : ndarray @@ -69,7 +77,7 @@ def greyscale_dilate(image, selem, out=None): raise NotImplementedError("In-place dilation not supported!") try: from . import cmorph - out = cmorph.dilate(image, selem, out=out) + out = cmorph.dilate(image, selem, out=out, flip=flip) return out; except ImportError: raise ImportError("cmorph extension not available.") @@ -79,6 +87,7 @@ def greyscale_open(image, selem, out=None): Performs a greyscale morphological opening on an image given a flat structuring element defined as a erosion followed by a dilation. + Parameters ---------- image : ndarray