From 39267d103cd9b8f4ba9cf2ebfc9729141b2624e2 Mon Sep 17 00:00:00 2001 From: Pratap Vardhan Date: Tue, 7 Oct 2014 17:55:44 +0530 Subject: [PATCH] CLN: Improve performance of disk and diamond selems --- skimage/morphology/selem.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/skimage/morphology/selem.py b/skimage/morphology/selem.py index 5e0606c0..ae287304 100644 --- a/skimage/morphology/selem.py +++ b/skimage/morphology/selem.py @@ -85,10 +85,10 @@ def diamond(radius, dtype=np.uint8): The structuring element where elements of the neighborhood are 1 and 0 otherwise. """ - half = radius - (I, J) = np.meshgrid(range(0, radius * 2 + 1), range(0, radius * 2 + 1)) - s = np.abs(I - half) + np.abs(J - half) - return np.array(s <= radius, dtype=dtype) + L = np.arange(0, radius * 2 + 1) + I, J = np.meshgrid(L, L) + return np.array(np.abs(I - radius) + np.abs(J - radius) <= radius, + dtype=dtype) def disk(radius, dtype=np.uint8): @@ -113,11 +113,9 @@ def disk(radius, dtype=np.uint8): The structuring element where elements of the neighborhood are 1 and 0 otherwise. """ - L = np.linspace(-radius, radius, 2 * radius + 1) - (X, Y) = np.meshgrid(L, L) - s = X**2 - s += Y**2 - return np.array(s <= radius * radius, dtype=dtype) + L = np.arange(-radius, radius + 1) + X, Y = np.meshgrid(L, L) + return np.array((X ** 2 + Y ** 2) <= radius ** 2, dtype=np.uint8) def cube(width, dtype=np.uint8):