mirror of
https://github.com/wassname/scikit-image.git
synced 2026-07-21 12:50:27 +08:00
This is currently working with **kwargs except for inplace which I cannot get working. It works with arrays of type int but returns an array of type bool. Possibly in future add labelling for arrays of type int. A UserWarning is produced when using arrays of type int which seems to work normally but the test created for this does not pick up the warning. Any assistance on these issues would be helpful. I started this at EuroScipy 2015 and this fixes issue #1642
173 lines
7.0 KiB
Python
173 lines
7.0 KiB
Python
import numpy as np
|
|
from numpy.testing import (assert_array_equal, assert_equal, assert_raises,
|
|
assert_warns)
|
|
from skimage.morphology import remove_small_objects, remove_small_holes
|
|
|
|
test_image = np.array([[0, 0, 0, 1, 0],
|
|
[1, 1, 1, 0, 0],
|
|
[1, 1, 1, 0, 1]], bool)
|
|
|
|
|
|
def test_one_connectivity():
|
|
expected = np.array([[0, 0, 0, 0, 0],
|
|
[1, 1, 1, 0, 0],
|
|
[1, 1, 1, 0, 0]], bool)
|
|
observed = remove_small_objects(test_image, min_size=6)
|
|
assert_array_equal(observed, expected)
|
|
|
|
|
|
def test_two_connectivity():
|
|
expected = np.array([[0, 0, 0, 1, 0],
|
|
[1, 1, 1, 0, 0],
|
|
[1, 1, 1, 0, 0]], bool)
|
|
observed = remove_small_objects(test_image, min_size=7, connectivity=2)
|
|
assert_array_equal(observed, expected)
|
|
|
|
|
|
def test_in_place():
|
|
observed = remove_small_objects(test_image, min_size=6, in_place=True)
|
|
assert_equal(observed is test_image, True,
|
|
"remove_small_objects in_place argument failed.")
|
|
|
|
|
|
def test_labeled_image():
|
|
labeled_image = np.array([[2, 2, 2, 0, 1],
|
|
[2, 2, 2, 0, 1],
|
|
[2, 0, 0, 0, 0],
|
|
[0, 0, 3, 3, 3]], dtype=int)
|
|
expected = np.array([[2, 2, 2, 0, 0],
|
|
[2, 2, 2, 0, 0],
|
|
[2, 0, 0, 0, 0],
|
|
[0, 0, 3, 3, 3]], dtype=int)
|
|
observed = remove_small_objects(labeled_image, min_size=3)
|
|
assert_array_equal(observed, expected)
|
|
|
|
|
|
def test_uint_image():
|
|
labeled_image = np.array([[2, 2, 2, 0, 1],
|
|
[2, 2, 2, 0, 1],
|
|
[2, 0, 0, 0, 0],
|
|
[0, 0, 3, 3, 3]], dtype=np.uint8)
|
|
expected = np.array([[2, 2, 2, 0, 0],
|
|
[2, 2, 2, 0, 0],
|
|
[2, 0, 0, 0, 0],
|
|
[0, 0, 3, 3, 3]], dtype=np.uint8)
|
|
observed = remove_small_objects(labeled_image, min_size=3)
|
|
assert_array_equal(observed, expected)
|
|
|
|
|
|
def test_single_label_warning():
|
|
image = np.array([[0, 0, 0, 1, 0],
|
|
[1, 1, 1, 0, 0],
|
|
[1, 1, 1, 0, 0]], int)
|
|
assert_warns(UserWarning, remove_small_objects, image, min_size=6)
|
|
|
|
|
|
def test_float_input():
|
|
float_test = np.random.rand(5, 5)
|
|
assert_raises(TypeError, remove_small_objects, float_test)
|
|
|
|
|
|
def test_negative_input():
|
|
negative_int = np.random.randint(-4, -1, size=(5, 5))
|
|
assert_raises(ValueError, remove_small_objects, negative_int)
|
|
|
|
test_holes_image = np.array([[0,0,0,0,0,0,1,0,0,0],
|
|
[0,1,1,1,1,1,0,0,0,0],
|
|
[0,1,0,0,1,1,0,0,0,0],
|
|
[0,1,1,1,0,1,0,0,0,0],
|
|
[0,1,1,1,1,1,0,0,0,0],
|
|
[0,0,0,0,0,0,0,1,1,1],
|
|
[0,0,0,0,0,0,0,1,0,1],
|
|
[0,0,0,0,0,0,0,1,1,1]], bool)
|
|
|
|
def test_one_connectivity_holes():
|
|
expected = np.array([[0,0,0,0,0,0,1,0,0,0],
|
|
[0,1,1,1,1,1,0,0,0,0],
|
|
[0,1,1,1,1,1,0,0,0,0],
|
|
[0,1,1,1,1,1,0,0,0,0],
|
|
[0,1,1,1,1,1,0,0,0,0],
|
|
[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]], bool)
|
|
observed = remove_small_holes(test_holes_image, min_size=3)
|
|
assert_array_equal(observed, expected)
|
|
|
|
|
|
def test_two_connectivity_holes():
|
|
expected = np.array([[0,0,0,0,0,0,1,0,0,0],
|
|
[0,1,1,1,1,1,0,0,0,0],
|
|
[0,1,0,0,1,1,0,0,0,0],
|
|
[0,1,1,1,0,1,0,0,0,0],
|
|
[0,1,1,1,1,1,0,0,0,0],
|
|
[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]], bool)
|
|
observed = remove_small_holes(test_holes_image, min_size=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)
|
|
assert_equal(observed is test_holes_image, True,
|
|
"remove_small_holes in_place argument failed.")
|
|
|
|
def test_labeled_image_holes():
|
|
labeled_holes_image = np.array([[0,0,0,0,0,0,1,0,0,0],
|
|
[0,1,1,1,1,1,0,0,0,0],
|
|
[0,1,0,0,1,1,0,0,0,0],
|
|
[0,1,1,1,0,1,0,0,0,0],
|
|
[0,1,1,1,1,1,0,0,0,0],
|
|
[0,0,0,0,0,0,0,2,2,2],
|
|
[0,0,0,0,0,0,0,2,0,2],
|
|
[0,0,0,0,0,0,0,2,2,2]], dtype=int)
|
|
expected = np.array([[0,0,0,0,0,0,1,0,0,0],
|
|
[0,1,1,1,1,1,0,0,0,0],
|
|
[0,1,1,1,1,1,0,0,0,0],
|
|
[0,1,1,1,1,1,0,0,0,0],
|
|
[0,1,1,1,1,1,0,0,0,0],
|
|
[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=bool)
|
|
observed = remove_small_holes(labeled_holes_image, min_size=3)
|
|
assert_array_equal(observed, expected)
|
|
|
|
|
|
def test_uint_image_holes():
|
|
labeled_holes_image = np.array([[0,0,0,0,0,0,1,0,0,0],
|
|
[0,1,1,1,1,1,0,0,0,0],
|
|
[0,1,0,0,1,1,0,0,0,0],
|
|
[0,1,1,1,0,1,0,0,0,0],
|
|
[0,1,1,1,1,1,0,0,0,0],
|
|
[0,0,0,0,0,0,0,2,2,2],
|
|
[0,0,0,0,0,0,0,2,0,2],
|
|
[0,0,0,0,0,0,0,2,2,2]], dtype=np.uint8)
|
|
expected = np.array([[0,0,0,0,0,0,1,0,0,0],
|
|
[0,1,1,1,1,1,0,0,0,0],
|
|
[0,1,1,1,1,1,0,0,0,0],
|
|
[0,1,1,1,1,1,0,0,0,0],
|
|
[0,1,1,1,1,1,0,0,0,0],
|
|
[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=bool)
|
|
observed = remove_small_holes(labeled_holes_image, min_size=3)
|
|
assert_array_equal(observed, expected)
|
|
|
|
|
|
def test_label_warning_holes():
|
|
labeled_holes_image = np.array([[0,0,0,0,0,0,1,0,0,0],
|
|
[0,1,1,1,1,1,0,0,0,0],
|
|
[0,1,0,0,1,1,0,0,0,0],
|
|
[0,1,1,1,0,1,0,0,0,0],
|
|
[0,1,1,1,1,1,0,0,0,0],
|
|
[0,0,0,0,0,0,0,2,2,2],
|
|
[0,0,0,0,0,0,0,2,0,2],
|
|
[0,0,0,0,0,0,0,2,2,2]], dtype=int)
|
|
assert_warns(UserWarning, remove_small_holes, labeled_holes_image, min_size=3)
|
|
|
|
def test_float_input_holes():
|
|
float_test = np.random.rand(5, 5)
|
|
assert_raises(TypeError, remove_small_holes, float_test)
|
|
|
|
if __name__ == "__main__":
|
|
np.testing.run_module_suite()
|