def test_random_sizes(): # make sure the size is not a problem niter = 10 elem = np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]], dtype=np.uint8) for m, n in np.random.random_integers(1, 100, size=(10, 2)): mask = np.ones((m, n), dtype=np.uint8) image8 = np.ones((m, n), dtype=np.uint8) out8 = np.empty_like(image8) rank.mean(image=image8, selem=elem, mask=mask, out=out8, shift_x=0, shift_y=0) assert_array_equal(image8.shape, out8.shape) rank.mean(image=image8, selem=elem, mask=mask, out=out8, shift_x=+1, shift_y=+1) assert_array_equal(image8.shape, out8.shape) image16 = np.ones((m, n), dtype=np.uint16) out16 = np.empty_like(image8, dtype=np.uint16) rank.mean(image=image16, selem=elem, mask=mask, out=out16, shift_x=0, shift_y=0) assert_array_equal(image16.shape, out16.shape) rank.mean(image=image16, selem=elem, mask=mask, out=out16, shift_x=+1, shift_y=+1) assert_array_equal(image16.shape, out16.shape) rank.percentile_mean(image=image16, mask=mask, out=out16, selem=elem, shift_x=0, shift_y=0, p0=.1, p1=.9) assert_array_equal(image16.shape, out16.shape) rank.percentile_mean(image=image16, mask=mask, out=out16, selem=elem, shift_x=+1, shift_y=+1, p0=.1, p1=.9) assert_array_equal(image16.shape, out16.shape)
def test_selem_dtypes(): image = np.zeros((5, 5), dtype=np.uint8) out = np.zeros_like(image) mask = np.ones_like(image, dtype=np.uint8) image[2, 2] = 255 image[2, 3] = 128 image[1, 2] = 16 for dtype in (np.uint8, np.uint16, np.int32, np.int64, np.float32, np.float64): elem = np.array([[0, 0, 0], [0, 1, 0], [0, 0, 0]], dtype=dtype) rank.mean(image=image, selem=elem, out=out, mask=mask, shift_x=0, shift_y=0) assert_array_equal(image, out) rank.percentile_mean(image=image, selem=elem, out=out, mask=mask, shift_x=0, shift_y=0) assert_array_equal(image, out)
def test_bitdepth(): # test the different bit depth for rank16 elem = np.ones((3, 3), dtype=np.uint8) out = np.empty((100, 100), dtype=np.uint16) mask = np.ones((100, 100), dtype=np.uint8) for i in range(5): image = np.ones((100, 100), dtype=np.uint16) * 255 * 2 ** i r = rank.percentile_mean(image=image, selem=elem, mask=mask, out=out, shift_x=0, shift_y=0, p0=.1, p1=.9)
def test_bitdepth(): # test the different bit depth for rank16 elem = np.ones((3, 3), dtype=np.uint8) out = np.empty((100, 100), dtype=np.uint16) mask = np.ones((100, 100), dtype=np.uint8) for i in range(5): image = np.ones((100, 100), dtype=np.uint16) * 255 * 2**i r = rank.percentile_mean(image=image, selem=elem, mask=mask, out=out, shift_x=0, shift_y=0, p0=.1, p1=.9)
complete image (background and details). Bilateral mean exhibits a high filtering rate for continuous area (i.e. background) while higher image frequencies remain untouched. """ import numpy as np import matplotlib.pyplot as plt from skimage import data from skimage.morphology import disk import skimage.filter.rank as rank a16 = (data.coins()).astype(np.uint16) * 16 selem = disk(20) f1 = rank.percentile_mean(a16, selem=selem, p0=.1, p1=.9) f2 = rank.bilateral_mean(a16, selem=selem, s0=500, s1=500) f3 = rank.mean(a16, selem=selem) # display results fig, axes = plt.subplots(nrows=3, figsize=(15, 10)) ax0, ax1, ax2 = axes ax0.imshow(np.hstack((a16, f1))) ax0.set_title('percentile mean') ax1.imshow(np.hstack((a16, f2))) ax1.set_title('bilateral mean') ax2.imshow(np.hstack((a16, f3))) ax2.set_title('local mean') plt.show()