From 42c72ab10609105425c95e98bbcb52c29951a067 Mon Sep 17 00:00:00 2001 From: Almar Date: Wed, 27 Mar 2013 21:52:07 +0100 Subject: [PATCH] Add ball selem. I verified in 2D that this usage of np.mgrid yields the exact same results as np.meshgrid (np.meshgrid is not available for 3D). --- skimage/morphology/selem.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/skimage/morphology/selem.py b/skimage/morphology/selem.py index a75c6d3e..2e17a4a9 100644 --- a/skimage/morphology/selem.py +++ b/skimage/morphology/selem.py @@ -140,3 +140,34 @@ def cube(width, dtype=np.uint8): """ return np.ones((width, width, width), dtype=dtype) + + +def ball(radius, dtype=np.uint8): + """ + Generates a ball-shaped structuring element of a given radius (the + 3D equivalent of a disk). A pixel is within the neighborhood if the + euclidean distance between it and the origin is no greater than + radius. + + Parameters + ---------- + radius : int + The radius of the ball-shaped structuring element. + + dtype : data-type + The data type of the structuring element. + + Returns + ------- + selem : ndarray + The structuring element where elements of the neighborhood + are 1 and 0 otherwise. + """ + n = 2 * radius + 1 + Z, Y, X = np.mgrid[ -radius:radius:n*1j, + -radius:radius:n*1j, + -radius:radius:n*1j] + s = X**2 + s += Y**2 + s += Z**2 + return np.array(s <= radius * radius, dtype=dtype)