mirror of
https://github.com/wassname/scikit-image.git
synced 2026-07-05 01:59:55 +08:00
pep8
This commit is contained in:
@@ -15,6 +15,7 @@ import numpy as np
|
||||
from . import _ctmf
|
||||
from rank_order import rank_order
|
||||
|
||||
|
||||
def median_filter(data, mask=None, radius=2, percent=50):
|
||||
'''Masked median filter with octagon shape.
|
||||
|
||||
@@ -43,7 +44,7 @@ def median_filter(data, mask=None, radius=2, percent=50):
|
||||
|
||||
'''
|
||||
|
||||
if data.ndim!=2:
|
||||
if data.ndim != 2:
|
||||
raise TypeError("The input 'data' must be a two dimensional array.")
|
||||
|
||||
if mask is None:
|
||||
@@ -57,7 +58,7 @@ def median_filter(data, mask=None, radius=2, percent=50):
|
||||
#
|
||||
if (not np.issubdtype(data.dtype, np.int) or
|
||||
np.min(data) < 0 or np.max(data) > 255):
|
||||
ranked_data,translation = rank_order(data[mask])
|
||||
ranked_data, translation = rank_order(data[mask])
|
||||
max_ranked_data = np.max(ranked_data)
|
||||
if max_ranked_data == 0:
|
||||
return data
|
||||
@@ -67,7 +68,7 @@ def median_filter(data, mask=None, radius=2, percent=50):
|
||||
else:
|
||||
ranked_data = data[mask]
|
||||
was_ranked = False
|
||||
input = np.zeros(data.shape, np.uint8 )
|
||||
input = np.zeros(data.shape, np.uint8)
|
||||
input[mask] = ranked_data
|
||||
|
||||
mask.dtype = np.uint8
|
||||
@@ -81,7 +82,8 @@ def median_filter(data, mask=None, radius=2, percent=50):
|
||||
# use the translation to look up the original value in the data.
|
||||
#
|
||||
if max_ranked_data > 255:
|
||||
result = translation[output.astype(np.uint32) * max_ranked_data // 255]
|
||||
result = translation[output.astype(np.uint32) *
|
||||
max_ranked_data // 255]
|
||||
else:
|
||||
result = translation[output]
|
||||
else:
|
||||
|
||||
@@ -2,46 +2,52 @@ import numpy as np
|
||||
|
||||
from scikits.image.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)), np.ones((10, 10), bool), 3)
|
||||
assert np.all(result == 0)
|
||||
|
||||
|
||||
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)), np.zeros((10, 10), bool), 3)
|
||||
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)
|
||||
mask = np.zeros((10, 10), bool)
|
||||
mask[5, 5] = True
|
||||
median_filter(np.zeros((10, 10)), mask, 3)
|
||||
|
||||
|
||||
def test_01_01_mask():
|
||||
'''The median filter, masking a single value'''
|
||||
img = np.zeros((10,10))
|
||||
img[5,5] = 1
|
||||
mask = np.ones((10,10),bool)
|
||||
mask[5,5] = False
|
||||
img = np.zeros((10, 10))
|
||||
img[5, 5] = 1
|
||||
mask = np.ones((10, 10), bool)
|
||||
mask[5, 5] = False
|
||||
result = median_filter(img, mask, 3)
|
||||
assert (np.all(result[mask] == 0))
|
||||
np.testing.assert_equal(result[5,5], 1)
|
||||
np.testing.assert_equal(result[5, 5], 1)
|
||||
|
||||
|
||||
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)
|
||||
np.testing.assert_equal(result[0,0], np.median(img))
|
||||
img = np.random.uniform(size=(9, 9))
|
||||
result = median_filter(img, np.ones((9, 9), bool), 20)
|
||||
np.testing.assert_equal(result[0, 0], np.median(img))
|
||||
assert (np.all(result == np.median(img)))
|
||||
|
||||
|
||||
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)
|
||||
img = np.random.uniform(size=(20, 20))
|
||||
result = median_filter(img, np.ones((20, 20), bool), 40)
|
||||
sorted = np.ravel(img)
|
||||
sorted.sort()
|
||||
min_acceptable = sorted[198]
|
||||
@@ -49,48 +55,53 @@ def test_02_02_median_bigger():
|
||||
assert (np.all(result >= min_acceptable))
|
||||
assert (np.all(result <= max_acceptable))
|
||||
|
||||
|
||||
def test_03_01_shape():
|
||||
'''Make sure the median filter is the expected octagonal shape'''
|
||||
|
||||
radius = 5
|
||||
a_2 = int(radius / 2.414213)
|
||||
i,j = np.mgrid[-10:11,-10:11]
|
||||
octagon = np.ones((21,21), bool)
|
||||
i, j = np.mgrid[-10:11, -10:11]
|
||||
octagon = np.ones((21, 21), bool)
|
||||
#
|
||||
# constrain the octagon mask to be the points that are on
|
||||
# the correct side of the 8 edges
|
||||
#
|
||||
octagon[i < -radius] = False
|
||||
octagon[i > radius] = False
|
||||
octagon[i > radius] = False
|
||||
octagon[j < -radius] = False
|
||||
octagon[j > radius] = False
|
||||
octagon[i+j < -radius-a_2] = False
|
||||
octagon[j-i > radius+a_2] = False
|
||||
octagon[i+j > radius+a_2] = False
|
||||
octagon[i-j > radius+a_2] = False
|
||||
octagon[j > radius] = False
|
||||
octagon[i + j < -radius - a_2] = False
|
||||
octagon[j - i > radius + a_2] = False
|
||||
octagon[i + j > radius + a_2] = False
|
||||
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)
|
||||
img = np.random.uniform(size=(21, 21))
|
||||
result = median_filter(img, np.ones((21, 21), bool), radius)
|
||||
sorted = img[octagon]
|
||||
sorted.sort()
|
||||
min_acceptable = sorted[len(sorted)/2-1]
|
||||
max_acceptable = sorted[len(sorted)/2+1]
|
||||
assert (result[10,10] >= min_acceptable)
|
||||
assert (result[10,10] <= max_acceptable)
|
||||
min_acceptable = sorted[len(sorted) / 2 - 1]
|
||||
max_acceptable = sorted[len(sorted) / 2 + 1]
|
||||
assert (result[10, 10] >= min_acceptable)
|
||||
assert (result[10, 10] <= max_acceptable)
|
||||
|
||||
|
||||
def test_04_01_half_masked():
|
||||
'''Make sure that the median filter can handle large masked areas.'''
|
||||
img = np.ones((20, 20))
|
||||
mask = np.ones((20, 20),bool)
|
||||
mask = np.ones((20, 20), bool)
|
||||
mask[10:, :] = False
|
||||
img[~ mask] = 2
|
||||
img[1, 1] = 0 # to prevent short circuit for uniform data.
|
||||
img[1, 1] = 0 # to prevent short circuit for uniform data.
|
||||
result = median_filter(img, mask, 5)
|
||||
# in partial coverage areas, the result should be only from the masked pixels
|
||||
# in partial coverage areas, the result should be only
|
||||
# from the masked pixels
|
||||
assert (np.all(result[:14, :] == 1))
|
||||
# in zero coverage areas, the result should be the lowest valud in the valid area
|
||||
# in zero coverage areas, the result should be the lowest
|
||||
# value in the valid area
|
||||
assert (np.all(result[15:, :] == np.min(img[mask])))
|
||||
|
||||
|
||||
def test_default_values():
|
||||
img = (np.random.random((20, 20)) * 255).astype(np.uint8)
|
||||
mask = np.ones((20, 20), dtype=np.uint8)
|
||||
@@ -98,5 +109,6 @@ def test_default_values():
|
||||
result2 = median_filter(img)
|
||||
np.testing.assert_array_equal(result1, result2)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
np.testing.run_module_suite()
|
||||
|
||||
Reference in New Issue
Block a user