Merge pull request #73 from tonysyu/fix-ctmf

BUG: Force median filter parameters to be valid.
This commit is contained in:
Stefan van der Walt
2011-10-27 16:59:17 -07:00
3 changed files with 27 additions and 13 deletions
+4
View File
@@ -3,3 +3,7 @@ Version 0.3
- Remove ``as_grey``, ``dtype`` keyword from ImageCollection
- Remove ``dtype`` from imread
- Generalise ImageCollection to accept a load_func
Version 0.4
-----------
- Switch mask and radius arguments for median_filter
+7 -4
View File
@@ -16,20 +16,20 @@ from . import _ctmf
from rank_order import rank_order
def median_filter(image, mask=None, radius=2, percent=50):
def median_filter(image, radius=2, mask=None, percent=50):
'''Masked median filter with octagon shape.
Parameters
----------
image : (M,N) ndarray, dtype uint8
Input image.
radius : {int, 1}, optional
The radius of a circle inscribed into the filtering
octagon. Default radius is 1.
mask : (M,N) ndarray, dtype uint8, optional
A value of 1 indicates a significant pixel, 0
that a pixel is masked. By default, all pixels
are considered.
radius : {int, 1}, optional
The radius of a circle inscribed into the filtering
octagon. Default radius is 1.
percent : {int, 50}, optional
The unmasked pixels within the octagon are sorted, and the
value at the `percent`-th index chosen. For example, the
@@ -47,6 +47,9 @@ def median_filter(image, mask=None, radius=2, percent=50):
if image.ndim != 2:
raise TypeError("The input 'image' must be a two dimensional array.")
if radius < 2:
raise ValueError("The input 'radius' must be >= 2.")
if mask is None:
mask = np.ones(image.shape, dtype=np.bool)
mask = np.ascontiguousarray(mask, dtype=np.bool)
+16 -9
View File
@@ -1,11 +1,12 @@
import numpy as np
from nose.tools import raises
from skimage.filter import median_filter
def test_00_00_zeros():
'''The median filter on an array of all zeros should be zero'''
result = median_filter(np.zeros((10, 10)), np.ones((10, 10), bool), 3)
result = median_filter(np.zeros((10, 10)), 3, np.ones((10, 10), bool))
assert np.all(result == 0)
@@ -13,14 +14,14 @@ def test_00_01_all_masked():
'''Test a completely masked image
Regression test of IMG-1029'''
result = median_filter(np.zeros((10, 10)), np.zeros((10, 10), bool), 3)
result = median_filter(np.zeros((10, 10)), 3, np.zeros((10, 10), bool))
assert (np.all(result == 0))
def test_00_02_all_but_one_masked():
mask = np.zeros((10, 10), bool)
mask[5, 5] = True
median_filter(np.zeros((10, 10)), mask, 3)
median_filter(np.zeros((10, 10)), 3, mask)
def test_01_01_mask():
@@ -29,7 +30,7 @@ def test_01_01_mask():
img[5, 5] = 1
mask = np.ones((10, 10), bool)
mask[5, 5] = False
result = median_filter(img, mask, 3)
result = median_filter(img, 3, mask)
assert (np.all(result[mask] == 0))
np.testing.assert_equal(result[5, 5], 1)
@@ -38,7 +39,7 @@ def test_02_01_median():
'''A median filter larger than the image = median of image'''
np.random.seed(0)
img = np.random.uniform(size=(9, 9))
result = median_filter(img, np.ones((9, 9), bool), 20)
result = median_filter(img, 20, np.ones((9, 9), bool))
np.testing.assert_equal(result[0, 0], np.median(img))
assert (np.all(result == np.median(img)))
@@ -47,7 +48,7 @@ def test_02_02_median_bigger():
'''Use an image of more than 255 values to test approximation'''
np.random.seed(0)
img = np.random.uniform(size=(20, 20))
result = median_filter(img, np.ones((20, 20), bool), 40)
result = median_filter(img, 40, np.ones((20, 20), bool))
sorted = np.ravel(img)
sorted.sort()
min_acceptable = sorted[198]
@@ -77,7 +78,7 @@ def test_03_01_shape():
octagon[i - j > radius + a_2] = False
np.random.seed(0)
img = np.random.uniform(size=(21, 21))
result = median_filter(img, np.ones((21, 21), bool), radius)
result = median_filter(img, radius, np.ones((21, 21), bool))
sorted = img[octagon]
sorted.sort()
min_acceptable = sorted[len(sorted) / 2 - 1]
@@ -93,7 +94,7 @@ def test_04_01_half_masked():
mask[10:, :] = False
img[~ mask] = 2
img[1, 1] = 0 # to prevent short circuit for uniform data.
result = median_filter(img, mask, 5)
result = median_filter(img, 5, mask)
# in partial coverage areas, the result should be only
# from the masked pixels
assert (np.all(result[:14, :] == 1))
@@ -105,10 +106,16 @@ def test_04_01_half_masked():
def test_default_values():
img = (np.random.random((20, 20)) * 255).astype(np.uint8)
mask = np.ones((20, 20), dtype=np.uint8)
result1 = median_filter(img, mask, radius=2, percent=50)
result1 = median_filter(img, radius=2, mask=mask, percent=50)
result2 = median_filter(img)
np.testing.assert_array_equal(result1, result2)
@raises(ValueError)
def test_insufficient_size():
img = (np.random.random((20, 20)) * 255).astype(np.uint8)
median_filter(img, radius=1)
if __name__ == "__main__":
np.testing.run_module_suite()