Merge pull request #452 from stefanv/jni-morphology-functions

STY: Minor cleanup on small object removal.
This commit is contained in:
Johannes Schönberger
2013-03-04 08:03:34 -08:00
2 changed files with 16 additions and 9 deletions
+14 -7
View File
@@ -1,6 +1,7 @@
import numpy as np
import scipy.ndimage as nd
def remove_small_objects(ar, min_size=64, connectivity=1, in_place=False):
"""Remove connected components smaller than the specified size.
@@ -51,28 +52,34 @@ def remove_small_objects(ar, min_size=64, connectivity=1, in_place=False):
>>> d is a
True
"""
errmsg = "Only numpy.ndarrays of bool or integer type are supported. "
if type(ar) != np.ndarray:
raise TypeError(errmsg + "Got a %s." % type(ar))
elif ar.dtype != bool and not np.issubdtype(ar.dtype, np.integer):
raise TypeError(errmsg + "Got a numpy.ndarray of type %s" % ar.dtype)
# Should use `issubdtype` below, but there's a bug in numpy 1.7
if not (ar.dtype == bool or np.issubdtype(ar.dtype, int)):
raise ValueError("Only bool or integer image types are supported. "
"Got %s." % ar.dtype)
if in_place:
out = ar
else:
out = ar.copy()
if min_size == 0: # shortcut for efficiency
if min_size == 0: # shortcut for efficiency
return out
if out.dtype == bool:
selem = nd.generate_binary_structure(ar.ndim, connectivity)
ccs = nd.label(ar, selem)[0]
else:
ccs = out
try:
component_sizes = np.bincount(ccs.ravel())
except ValueError:
raise ValueError("Negative value labels are not supported. Try "
"relabeling the input with `scipy.ndimage.label`.")
"relabeling the input with `scipy.ndimage.label` or "
"`skimage.morphology.label`.")
too_small = component_sizes < min_size
too_small_mask = too_small[ccs]
out[too_small_mask] = 0
return out
+2 -2
View File
@@ -25,7 +25,7 @@ def test_two_connectivity():
def test_in_place():
observed = remove_small_objects(test_image, min_size=6, in_place=True)
assert_equal(observed is test_image, True,
assert_equal(observed is test_image, True,
"remove_small_objects in_place argument failed.")
@@ -44,7 +44,7 @@ def test_labeled_image():
def test_float_input():
float_test = np.random.rand(5, 5)
assert_raises(TypeError, remove_small_objects, float_test)
assert_raises(ValueError, remove_small_objects, float_test)
def test_negative_input():