def test_threshold_local_gaussian(self, ndim): ref = np.array([[False, False, False, False, True], [False, False, True, False, True], [False, False, True, True, False], [False, True, True, False, False], [True, True, False, False, False]]) if ndim == 2: image = self.image block_sizes = [3, (3, ) * image.ndim] else: image = np.stack((self.image, ) * 5, axis=-1) ref = np.stack((ref, ) * 5, axis=-1) block_sizes = [ 3, (3, ) * image.ndim, (3, ) * (image.ndim - 1) + (1, ) ] for block_size in block_sizes: out = threshold_local(image, block_size, method='gaussian', mode='reflect') assert_equal(ref, image > out) out = threshold_local(image, 3, method='gaussian', mode='reflect', param=1 / 3) assert_equal(ref, image > out)
def test_threshold_local_gaussian(self): ref = np.array([[False, False, False, False, True], [False, False, True, False, True], [False, False, True, True, False], [False, True, True, False, False], [True, True, False, False, False]]) out = threshold_local(self.image, 3, method='gaussian') assert_equal(ref, self.image > out) out = threshold_local(self.image, 3, method='gaussian', param=1. / 3.) assert_equal(ref, self.image > out)
def test_threshold_local_gaussian(self): ref = np.array( [[False, False, False, False, True], [False, False, True, False, True], [False, False, True, True, False], [False, True, True, False, False], [ True, True, False, False, False]] ) out = threshold_local(self.image, 3, method='gaussian') assert_equal(ref, self.image > out) out = threshold_local(self.image, 3, method='gaussian', param=1./3.) assert_equal(ref, self.image > out)
def test_threshold_local_median(self): ref = np.array([[False, False, False, False, True], [False, False, True, False, False], [False, False, True, False, False], [False, False, True, True, False], [False, True, False, False, False]]) out = threshold_local(self.image, 3, method='median') assert_equal(ref, self.image > out)
def test_threshold_local_median_constant_mode(self): out = threshold_local(self.image, 3, method='median', mode='constant', cval=20) expected = np.array([[20., 1., 3., 4., 20.], [ 1., 1., 3., 4., 4.], [ 2., 2., 4., 4., 4.], [ 4., 4., 4., 1., 2.], [20., 5., 5., 2., 20.]]) assert_equal(expected, out)
def test_threshold_local_median_constant_mode(self): out = threshold_local(self.image, 3, method='median', mode='constant', cval=20) expected = np.array([[20., 1., 3., 4., 20.], [ 1., 1., 3., 4., 4.], [ 2., 2., 4., 4., 4.], [ 4., 4., 4., 1., 2.], [20., 5., 5., 2., 20.]]) assert_equal(expected, out)
def test_threshold_local_equals_adaptive(self): def func(arr): return arr.sum() / arr.shape[0] with expected_warnings(['deprecated', 'return value']): thresholded_original = threshold_adaptive(self.image, 3, method='generic', param=func) threshold_new = threshold_local(self.image, 3, method='generic', param=func) assert_equal(thresholded_original, self.image > threshold_new)
def test_threshold_local_equals_adaptive(self): def func(arr): return arr.sum() / arr.shape[0] with expected_warnings(['deprecated', 'return value']): thresholded_original = threshold_adaptive(self.image, 3, method='generic', param=func) threshold_new = threshold_local(self.image, 3, method='generic', param=func) assert_equal(thresholded_original, self.image > threshold_new)
def test_threshold_local_median(self): ref = np.array( [[False, False, False, False, True], [False, False, True, False, False], [False, False, True, False, False], [False, False, True, True, False], [False, True, False, False, False]] ) out = threshold_local(self.image, 3, method='median') assert_equal(ref, self.image > out)
def test_threshold_local_median(self, ndim): ref = np.array([[False, False, False, False, True], [False, False, True, False, False], [False, False, True, False, False], [False, False, True, True, False], [False, True, False, False, False]]) if ndim == 2: image = self.image else: image = np.stack((self.image, ) * 5, axis=-1) ref = np.stack((ref, ) * 5, axis=-1) out = threshold_local(image, 3, method='median', mode='reflect') assert_equal(ref, image > out)
def test_threshold_local_mean(self, ndim): ref = np.array( [[False, False, False, False, True], [False, False, True, False, True], [False, False, True, True, False], [False, True, True, False, False], [ True, True, False, False, False]] ) if ndim == 2: image = self.image block_sizes = [3, (3,) * image.ndim] else: image = np.stack((self.image, ) * 5, axis=-1) ref = np.stack((ref, ) * 5, axis=-1) # Given the same data at each z location, the following block sizes # will all give an equivalent result. block_sizes = [3, (3,) * image.ndim, (3,) * (image.ndim - 1) + (1,)] for block_size in block_sizes: out = threshold_local(image, block_size, method='mean', mode='reflect') assert_equal(ref, image > out)
def test_adaptive_even_block_size_error(): img = data.camera() with pytest.raises(ValueError): threshold_local(img, block_size=4)
def test_adaptive_even_block_size_error(): img = data.camera() with pytest.raises(ValueError): threshold_local(img, block_size=4)
from skimage.color import rgb2gray import os #image = data.camera() filename = "fake_images/Screen Shot 2019-08-22 at 11.06.38 AM.png" from skimage import io moon = io.imread(filename) image = rgb2gray(moon) global_thresh = threshold_otsu(image) binary_global = image > global_thresh block_size = 13 binary_adaptive = threshold_local(image, block_size, offset=10) fig, axes = plt.subplots(nrows=3, figsize=(7, 8)) ax0, ax1, ax2 = axes plt.gray() ax0.imshow(image) ax0.set_title('Image') ax1.imshow(binary_global) ax1.set_title('Global thresholding') ax2.imshow(binary_adaptive) ax2.set_title('Adaptive thresholding') for ax in axes:
def test_threshold_local_invalid_block_size(self, block_size): # len(block_size) != image.ndim with pytest.raises(ValueError): threshold_local(self.image, block_size, method='mean')
def test_local_even_block_size_error(): img = data.camera() with testing.raises(ValueError): threshold_local(img, block_size=4)
def test_local_even_block_size_error(): img = data.camera() with testing.raises(ValueError): threshold_local(img, block_size=4)