Skip to content

Commit

Permalink
replacing argument min_size with area_threshold
Browse files Browse the repository at this point in the history
  • Loading branch information
nrweir committed Dec 16, 2017
1 parent e2a6094 commit 9ab36cc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
26 changes: 16 additions & 10 deletions skimage/morphology/misc.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Miscellaneous morphology functions."""
import numpy as np
import functools
from scipy import ndimage as ndi
Expand Down Expand Up @@ -29,6 +30,7 @@ def default_selem(func):
func_out : function
The function, using a default structuring element of same dimension
as the input image with connectivity 1.
"""
@functools.wraps(func)
def func_out(image, selem=None, *args, **kwargs):
Expand All @@ -38,12 +40,14 @@ def func_out(image, selem=None, *args, **kwargs):

return func_out


def _check_dtype_supported(ar):
# Should use `issubdtype` for bool below, but there's a bug in numpy 1.7
if not (ar.dtype == bool or np.issubdtype(ar.dtype, np.integer)):
raise TypeError("Only bool or integer image types are supported. "
"Got %s." % ar.dtype)


def remove_small_objects(ar, min_size=64, connectivity=1, in_place=False):
"""Remove connected components smaller than the specified size.
Expand Down Expand Up @@ -92,6 +96,7 @@ def remove_small_objects(ar, min_size=64, connectivity=1, in_place=False):
>>> d = morphology.remove_small_objects(a, 6, in_place=True)
>>> d is a
True
"""
# Raising type error if not int or bool
_check_dtype_supported(ar)
Expand Down Expand Up @@ -128,15 +133,16 @@ def remove_small_objects(ar, min_size=64, connectivity=1, in_place=False):

return out

def remove_small_holes(ar, min_size=64, connectivity=1, in_place=False):

def remove_small_holes(ar, area_threshold=64, connectivity=1, in_place=False):
"""Remove continguous holes smaller than the specified size.
Parameters
----------
ar : ndarray (arbitrary shape, int or bool type)
The array containing the connected components of interest.
min_size : int, optional (default: 64)
The hole component size.
area_threshold : int, optional (default: 64)
The maximum area, in pixels, of a contiguous hole that will be filled.
connectivity : int, {1, 2, ..., ar.ndim}, optional (default: 1)
The connectivity defining the neighborhood of a pixel.
in_place : bool, optional (default: False)
Expand Down Expand Up @@ -180,15 +186,15 @@ def remove_small_holes(ar, min_size=64, connectivity=1, in_place=False):
Notes
-----
If the array type is int, it is assumed that it contains already-labeled
objects. The labels are not kept in the output image (this function always
outputs a bool image). It is suggested that labeling is completed after
using this function.
"""
_check_dtype_supported(ar)

#Creates warning if image is an integer image
# Creates warning if image is an integer image
if ar.dtype != bool:
warn("Any labeled images will be returned as a boolean array. "
"Did you mean to use a boolean array?", UserWarning)
Expand All @@ -198,17 +204,17 @@ def remove_small_holes(ar, min_size=64, connectivity=1, in_place=False):
else:
out = ar.copy()

#Creating the inverse of ar
# Creating the inverse of ar
if in_place:
out = np.logical_not(out,out)
out = np.logical_not(out, out)
else:
out = np.logical_not(out)

#removing small objects from the inverse of ar
out = remove_small_objects(out, min_size, connectivity, in_place)
# removing small objects from the inverse of ar
out = remove_small_objects(out, area_threshold, connectivity, in_place)

if in_place:
out = np.logical_not(out,out)
out = np.logical_not(out, out)
else:
out = np.logical_not(out)

Expand Down
14 changes: 8 additions & 6 deletions skimage/morphology/tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def test_one_connectivity_holes():
[0, 0, 0, 0, 0, 0, 0, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 1, 1, 1]], np.bool_)
observed = remove_small_holes(test_holes_image, min_size=3)
observed = remove_small_holes(test_holes_image, area_threshold=3)
assert_array_equal(observed, expected)


Expand All @@ -111,12 +111,14 @@ def test_two_connectivity_holes():
[0, 0, 0, 0, 0, 0, 0, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 1, 1, 1]], np.bool_)
observed = remove_small_holes(test_holes_image, min_size=3, connectivity=2)
observed = remove_small_holes(test_holes_image, area_threshold=3,
connectivity=2)
assert_array_equal(observed, expected)


def test_in_place_holes():
observed = remove_small_holes(test_holes_image, min_size=3, in_place=True)
observed = remove_small_holes(test_holes_image, area_threshold=3,
in_place=True)
assert_equal(observed is test_holes_image, True,
"remove_small_holes in_place argument failed.")

Expand All @@ -139,7 +141,7 @@ def test_labeled_image_holes():
[0, 0, 0, 0, 0, 0, 0, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 1, 1, 1]], dtype=np.bool_)
observed = remove_small_holes(labeled_holes_image, min_size=3)
observed = remove_small_holes(labeled_holes_image, area_threshold=3)
assert_array_equal(observed, expected)


Expand All @@ -161,7 +163,7 @@ def test_uint_image_holes():
[0, 0, 0, 0, 0, 0, 0, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 1, 1, 1]], dtype=np.bool_)
observed = remove_small_holes(labeled_holes_image, min_size=3)
observed = remove_small_holes(labeled_holes_image, area_threshold=3)
assert_array_equal(observed, expected)


Expand All @@ -176,7 +178,7 @@ def test_label_warning_holes():
[0, 0, 0, 0, 0, 0, 0, 2, 2, 2]],
dtype=np.int_)
with expected_warnings(['use a boolean array?']):
remove_small_holes(labeled_holes_image, min_size=3)
remove_small_holes(labeled_holes_image, area_threshold=3)


def test_float_input_holes():
Expand Down

0 comments on commit 9ab36cc

Please sign in to comment.