def test_deprecation(): selem = disk(3) image = img_as_ubyte(data.camera()[:50, :50]) with expected_warnings(['rank.tophat is deprecated.']): rank.tophat(image, selem) with expected_warnings(['rank.bottomhat is deprecated.']): rank.bottomhat(image, selem)
def check_all(): selem = morphology.disk(1) refs = np.load( os.path.join(skimage.data_dir, "rank_filter_tests.npz")) assert_equal(refs["autolevel"], rank.autolevel(self.image, selem)) assert_equal(refs["autolevel_percentile"], rank.autolevel_percentile(self.image, selem)) assert_equal(refs["bottomhat"], rank.bottomhat(self.image, selem)) assert_equal(refs["equalize"], rank.equalize(self.image, selem)) assert_equal(refs["gradient"], rank.gradient(self.image, selem)) assert_equal(refs["gradient_percentile"], rank.gradient_percentile(self.image, selem)) assert_equal(refs["maximum"], rank.maximum(self.image, selem)) assert_equal(refs["mean"], rank.mean(self.image, selem)) assert_equal(refs["geometric_mean"], rank.geometric_mean(self.image, selem)), assert_equal(refs["mean_percentile"], rank.mean_percentile(self.image, selem)) assert_equal(refs["mean_bilateral"], rank.mean_bilateral(self.image, selem)) assert_equal(refs["subtract_mean"], rank.subtract_mean(self.image, selem)) assert_equal(refs["subtract_mean_percentile"], rank.subtract_mean_percentile(self.image, selem)) assert_equal(refs["median"], rank.median(self.image, selem)) assert_equal(refs["minimum"], rank.minimum(self.image, selem)) assert_equal(refs["modal"], rank.modal(self.image, selem)) assert_equal(refs["enhance_contrast"], rank.enhance_contrast(self.image, selem)) assert_equal(refs["enhance_contrast_percentile"], rank.enhance_contrast_percentile(self.image, selem)) assert_equal(refs["pop"], rank.pop(self.image, selem)) assert_equal(refs["pop_percentile"], rank.pop_percentile(self.image, selem)) assert_equal(refs["pop_bilateral"], rank.pop_bilateral(self.image, selem)) assert_equal(refs["sum"], rank.sum(self.image, selem)) assert_equal(refs["sum_bilateral"], rank.sum_bilateral(self.image, selem)) assert_equal(refs["sum_percentile"], rank.sum_percentile(self.image, selem)) assert_equal(refs["threshold"], rank.threshold(self.image, selem)) assert_equal(refs["threshold_percentile"], rank.threshold_percentile(self.image, selem)) assert_equal(refs["tophat"], rank.tophat(self.image, selem)) assert_equal(refs["noise_filter"], rank.noise_filter(self.image, selem)) assert_equal(refs["entropy"], rank.entropy(self.image, selem)) assert_equal(refs["otsu"], rank.otsu(self.image, selem)) assert_equal(refs["percentile"], rank.percentile(self.image, selem)) assert_equal(refs["windowed_histogram"], rank.windowed_histogram(self.image, selem))
def check_all(): np.random.seed(0) image = np.random.rand(25, 25) selem = morphology.disk(1) refs = np.load(os.path.join(skimage.data_dir, "rank_filter_tests.npz")) assert_equal(refs["autolevel"], rank.autolevel(image, selem)) assert_equal(refs["autolevel_percentile"], rank.autolevel_percentile(image, selem)) assert_equal(refs["bottomhat"], rank.bottomhat(image, selem)) assert_equal(refs["equalize"], rank.equalize(image, selem)) assert_equal(refs["gradient"], rank.gradient(image, selem)) assert_equal(refs["gradient_percentile"], rank.gradient_percentile(image, selem)) assert_equal(refs["maximum"], rank.maximum(image, selem)) assert_equal(refs["mean"], rank.mean(image, selem)) assert_equal(refs["mean_percentile"], rank.mean_percentile(image, selem)) assert_equal(refs["mean_bilateral"], rank.mean_bilateral(image, selem)) assert_equal(refs["subtract_mean"], rank.subtract_mean(image, selem)) assert_equal(refs["subtract_mean_percentile"], rank.subtract_mean_percentile(image, selem)) assert_equal(refs["median"], rank.median(image, selem)) assert_equal(refs["minimum"], rank.minimum(image, selem)) assert_equal(refs["modal"], rank.modal(image, selem)) assert_equal(refs["enhance_contrast"], rank.enhance_contrast(image, selem)) assert_equal(refs["enhance_contrast_percentile"], rank.enhance_contrast_percentile(image, selem)) assert_equal(refs["pop"], rank.pop(image, selem)) assert_equal(refs["pop_percentile"], rank.pop_percentile(image, selem)) assert_equal(refs["pop_bilateral"], rank.pop_bilateral(image, selem)) assert_equal(refs["sum"], rank.sum(image, selem)) assert_equal(refs["sum_bilateral"], rank.sum_bilateral(image, selem)) assert_equal(refs["sum_percentile"], rank.sum_percentile(image, selem)) assert_equal(refs["threshold"], rank.threshold(image, selem)) assert_equal(refs["threshold_percentile"], rank.threshold_percentile(image, selem)) assert_equal(refs["tophat"], rank.tophat(image, selem)) assert_equal(refs["noise_filter"], rank.noise_filter(image, selem)) assert_equal(refs["entropy"], rank.entropy(image, selem)) assert_equal(refs["otsu"], rank.otsu(image, selem)) assert_equal(refs["percentile"], rank.percentile(image, selem)) assert_equal(refs["windowed_histogram"], rank.windowed_histogram(image, selem))
from skimage import data, color import matplotlib.pyplot as plt from skimage.morphology import disk import skimage.filters.rank as sfr img = color.rgb2gray(data.camera()) bh = sfr.bottomhat(img, disk(5)) # 半径为5的圆形滤波器 th = sfr.tophat(img, disk(5)) fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(8, 8)) axes = axes.ravel() ax0, ax1, ax2, ax3 = axes ax0.imshow(img, cmap=plt.cm.gray, interpolation='nearest') ax0.set_title("Original") ax2.imshow(bh, cmap=plt.cm.gray, interpolation='nearest') ax2.set_title("Bottomhat") ax3.imshow(th, cmap=plt.cm.gray, interpolation='nearest') ax3.set_title("Tophat") for ax in axes: ax.axis('off') fig.tight_layout() plt.show()
from skimage import data, color import matplotlib.pyplot as plt from skimage.morphology import disk import skimage.filters.rank as sfr original_image = color.rgb2gray(data.camera()) plt.imshow(original_image, cmap=plt.cm.gray) filtered_images = [] filtered_images.append(sfr.autolevel(original_image, disk(5))) filtered_images.append(sfr.bottomhat(original_image, disk(5))) filtered_images.append(sfr.tophat(original_image, disk(5))) filtered_images.append(sfr.enhance_contrast(original_image, disk(5))) filtered_images.append(sfr.entropy(original_image, disk(5))) filtered_images.append(sfr.equalize(original_image, disk(5))) filtered_images.append(sfr.gradient(original_image, disk(5))) filtered_images.append(sfr.maximum(original_image, disk(5))) filtered_images.append(sfr.minimum(original_image, disk(5))) filtered_images.append(sfr.mean(original_image, disk(5))) filtered_images.append(sfr.median(original_image, disk(5))) filtered_images.append(sfr.modal(original_image, disk(5))) filtered_images.append(sfr.otsu(original_image, disk(5))) filtered_images.append(sfr.threshold(original_image, disk(5))) filtered_images.append(sfr.subtract_mean(original_image, disk(5))) filtered_images.append(sfr.sum(original_image, disk(5))) name_list = [ 'autolevel', 'bottomhat', 'tophat', 'enhance_contrast', 'entropy', 'equalize', 'gradient', 'maximum', 'minimum', 'mean', 'median', 'modal', 'otsu', 'threshold', 'subtract_mean', 'sum' ]
plt.figure(3) plt.imshow(edges, cmap="gray") # apply noise removal (using median filter) then thresholding (using minimum filter) then edge detection # can find edges of large and far away objects med = median(image, disk(5)) thresh_min = threshold_minimum(med) binary_min = med > thresh_min edges = sobel(binary_min) plt.figure(4) plt.imshow(edges, cmap="gray") #%% background removal (useful when there are different intensities in the background) # apply background removal using tophat filter background = tophat(image, disk(5)) plt.figure(1) plt.imshow(background, cmap="gray") # apply noise removal (using median filter) then background substraction (using tophat filter) med = median(image, disk(5)) background = tophat(med, disk(5)) plt.figure(2) plt.imshow(background, cmap="gray") #%% post processing thresholding using binary closing (fill holes) and binary openning # apply noise removal med = median(image, disk(5)) # apply thresholding