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*)
This commit is contained in:
Tony S Yu
2011-10-14 11:00:10 -04:00
parent 8c16be159b
commit 9adbca13e7
2 changed files with 26 additions and 6 deletions
+13 -2
View File
@@ -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)
+13 -4
View File
@@ -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