mirror of
https://github.com/wassname/scikit-image.git
synced 2026-07-03 06:19:39 +08:00
remove duplicate examples
This commit is contained in:
@@ -1,173 +0,0 @@
|
||||
"""
|
||||
==============================
|
||||
Compare execution time for
|
||||
- skimage.rank.median,
|
||||
- skimage.filter import median_filter
|
||||
- scipy.ndimage.filters import percentile_filter,
|
||||
|
||||
and
|
||||
|
||||
- skimage.cmorph.dilate
|
||||
- skimage.rank.maximum
|
||||
|
||||
==============================
|
||||
|
||||
to complete
|
||||
|
||||
"""
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
import time
|
||||
|
||||
from scipy.ndimage.filters import percentile_filter
|
||||
|
||||
from skimage import data
|
||||
from skimage.morphology import dilation,disk
|
||||
from skimage.filter import median_filter
|
||||
import skimage.filter.rank as rank
|
||||
|
||||
def exec_and_timeit(func):
|
||||
""" Decorator that returns both function results and execution time
|
||||
(result, ms)
|
||||
"""
|
||||
def wrapper(*arg):
|
||||
t1 = time.time()
|
||||
res = func(*arg)
|
||||
t2 = time.time()
|
||||
ms = (t2-t1)*1000.0
|
||||
return (res,ms)
|
||||
return wrapper
|
||||
|
||||
|
||||
@exec_and_timeit
|
||||
def cr_med(image,selem):
|
||||
return rank.median(image=image,selem = selem)
|
||||
|
||||
@exec_and_timeit
|
||||
def cr_max(image,selem):
|
||||
return rank.maximum(image=image,selem = selem)
|
||||
|
||||
@exec_and_timeit
|
||||
def cm_dil(image,selem):
|
||||
return dilation(image=image,selem = selem)
|
||||
|
||||
@exec_and_timeit
|
||||
def ctmf_med(image,radius):
|
||||
return median_filter(image=image,radius=radius)
|
||||
|
||||
@exec_and_timeit
|
||||
def ndi_med(image,n):
|
||||
return percentile_filter(image,50,size=n*2-1)
|
||||
|
||||
def compare_dilate():
|
||||
""" Comparison between
|
||||
- crank.maximum rankfilter implementation
|
||||
- cmorph.dilate cython implementation
|
||||
|
||||
on increasing structuring element size and increasing image size
|
||||
"""
|
||||
a = data.camera()
|
||||
|
||||
rec = []
|
||||
e_range = range(1,20,1)
|
||||
for r in e_range:
|
||||
elem = disk(r+1)
|
||||
# elem = (np.random.random((r,r))>.5).astype('uint8')
|
||||
rc,ms_rc = cr_max(a,elem)
|
||||
rcm,ms_rcm = cm_dil(a,elem)
|
||||
rec.append((ms_rc,ms_rcm))
|
||||
# same structuring element, the results must match
|
||||
assert (rc==rcm).all()
|
||||
|
||||
rec = np.asarray(rec)
|
||||
|
||||
plt.figure()
|
||||
plt.title('increasing element size')
|
||||
plt.plot(e_range,rec)
|
||||
plt.legend(['crank.maximum','cmorph.dilate'])
|
||||
|
||||
r = 9
|
||||
elem = disk(r+1)
|
||||
|
||||
rec = []
|
||||
s_range = range(100,1000,100)
|
||||
for s in s_range:
|
||||
a = (np.random.random((s,s))*256).astype('uint8')
|
||||
(rc,ms_rc) = cr_max(a,elem)
|
||||
(rcm,ms_rcm) = cm_dil(a,elem)
|
||||
rec.append((ms_rc,ms_rcm))
|
||||
# same structuring element, the results must match
|
||||
assert (rc==rcm).all()
|
||||
|
||||
rec = np.asarray(rec)
|
||||
|
||||
plt.figure()
|
||||
plt.title('increasing image size')
|
||||
plt.plot(s_range,rec)
|
||||
plt.legend(['crank.maximum','cmorph.dilate'])
|
||||
plt.figure()
|
||||
plt.imshow(np.hstack((rc,rcm)))
|
||||
|
||||
|
||||
def compare_median():
|
||||
""" Comparison between
|
||||
- crank.median rankfilter implementation
|
||||
- ctmf.median_filter filter
|
||||
|
||||
on increasing structuring element size and increasing image size
|
||||
"""
|
||||
a = data.camera()
|
||||
|
||||
rec = []
|
||||
e_range = range(2,30,4)
|
||||
for r in e_range:
|
||||
elem = disk(r+1)
|
||||
rc,ms_rc = cr_med(a,elem)
|
||||
rctmf,ms_rctmf = ctmf_med(a,r)
|
||||
rndi,ms_ndi = ndi_med(a,r)
|
||||
rec.append((ms_rc,ms_rctmf,ms_ndi))
|
||||
# check if results are identical
|
||||
# obviously they cannot be identical since structuring element are different (octagon<>disk)
|
||||
# assert (rc==rctmf).all()
|
||||
|
||||
rec = np.asarray(rec)
|
||||
|
||||
plt.figure()
|
||||
plt.title('increasing element size')
|
||||
plt.plot(e_range,rec)
|
||||
plt.legend(['rank.median','ctmf.median_filter','ndimage.percentile'])
|
||||
plt.ylabel('time (ms)')
|
||||
plt.xlabel('element radius')
|
||||
plt.figure()
|
||||
plt.imshow(np.hstack((rc,rctmf,rndi)))
|
||||
plt.xlabel('rank.median vs ctmf.median_filter vs ndimage.percentile')
|
||||
|
||||
r = 9
|
||||
elem = disk(r+1)
|
||||
|
||||
rec = []
|
||||
s_range = [100,200,500,1000,2000]
|
||||
for s in s_range:
|
||||
a = (np.random.random((s,s))*256).astype('uint8')
|
||||
(rc,ms_rc) = cr_med(a,elem)
|
||||
rctmf,ms_rctmf = ctmf_med(a,r)
|
||||
rndi,ms_ndi = ndi_med(a,r)
|
||||
rec.append((ms_rc,ms_rctmf,ms_ndi))
|
||||
# check if results are identical
|
||||
# obviously they cannot be identical since structuring element are different (octagon<>disk)
|
||||
# assert (rc==rctmf).all()
|
||||
|
||||
rec = np.asarray(rec)
|
||||
|
||||
plt.figure()
|
||||
plt.title('increasing image size')
|
||||
plt.plot(s_range,rec)
|
||||
plt.legend(['rank.median','ctmf.median_filter','ndimage.percentile'])
|
||||
plt.ylabel('time (ms)')
|
||||
plt.xlabel('image size')
|
||||
|
||||
|
||||
|
||||
compare_dilate()
|
||||
compare_median()
|
||||
plt.show()
|
||||
@@ -1,63 +0,0 @@
|
||||
"""
|
||||
====================================================
|
||||
Bilateral comparison
|
||||
====================================================
|
||||
|
||||
In this example, we compare both bilateral implementation
|
||||
|
||||
* filter.denoise_bilateral
|
||||
* filter.rank.bilateral_mean
|
||||
|
||||
The first filter implements a spatial-gaussian and spectral-gaussian kernel bilateral filter whereas the latter implements
|
||||
a cylindrical kernel bilateral filter i.e. spatial-flat and spectral-flat kernel.
|
||||
|
||||
The timing comparison is just for information since the kernel are not the same.
|
||||
|
||||
"""
|
||||
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
from skimage import data
|
||||
from skimage.filter._denoise import denoise_bilateral
|
||||
from skimage.filter.rank import bilateral_mean
|
||||
from skimage.morphology import disk
|
||||
from skimage.filter import denoise_bilateral
|
||||
import time
|
||||
|
||||
def exec_and_timeit(func):
|
||||
""" Decorator that returns both function results and execution time
|
||||
(result, ms)
|
||||
"""
|
||||
def wrapper(*arg):
|
||||
t1 = time.time()
|
||||
res = func(*arg)
|
||||
t2 = time.time()
|
||||
ms = (t2-t1)*1000.0
|
||||
return (res,ms)
|
||||
return wrapper
|
||||
|
||||
|
||||
@exec_and_timeit
|
||||
def den_bil(image):
|
||||
return denoise_bilateral(a8,win_size=20,sigma_range=255,sigma_spatial=1)[:,:,0]*255
|
||||
|
||||
@exec_and_timeit
|
||||
def rank_bil(image):
|
||||
return bilateral_mean(a8.astype(np.uint16),disk(20),s0=10,s1=10)
|
||||
|
||||
a8 = data.camera()
|
||||
selem = disk(10)
|
||||
|
||||
f1,t1 = den_bil(a8)
|
||||
f2,t2 = rank_bil(a8)
|
||||
|
||||
# display results
|
||||
fig, axes = plt.subplots(nrows=2, figsize=(15,10))
|
||||
ax0, ax1= axes
|
||||
|
||||
ax0.imshow(np.hstack((f1,a8-f1)))
|
||||
ax0.set_title('denoise bilateral (%f ms)'%t1)
|
||||
ax1.imshow(np.hstack((f2,a8-f1)))
|
||||
ax1.set_title('bilateral mean (%f ms)'%t2)
|
||||
plt.show()
|
||||
@@ -1,50 +0,0 @@
|
||||
"""
|
||||
====================================================
|
||||
Denoising the picture of Lena using bilateral filter
|
||||
====================================================
|
||||
|
||||
In this example, we denoise a noisy version of the picture of Lena
|
||||
using an approximation of a bilateral filter.
|
||||
The pixels used to compute a local mean respect these conditions:
|
||||
- be close to the central pixel, i.e. belong to the given structuring element.
|
||||
- have a similar gray level, similarity is fixed by an interval [-s0,+s1] centered on the central pixel gray level.
|
||||
|
||||
The filter used is an approximation of a classical bilateral filter in the sens that kernel are usually gaussian
|
||||
both in spatial and spectral dimensions.
|
||||
"""
|
||||
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
from skimage import data, color, img_as_ubyte
|
||||
from skimage.filter.rank import bilateral_mean
|
||||
from skimage.morphology import disk
|
||||
|
||||
l = img_as_ubyte(color.rgb2gray(data.lena()))
|
||||
l = l[230:290, 220:320]
|
||||
|
||||
noisy = l + 0.4 * l.std() * np.random.random(l.shape)
|
||||
|
||||
selem = disk(30)
|
||||
approx_bilateral_denoised = bilateral_mean(noisy.astype(np.uint8), selem=selem,s0=10,s1=10)
|
||||
|
||||
plt.figure(figsize=(8, 2))
|
||||
|
||||
plt.subplot(131)
|
||||
plt.imshow(noisy, cmap=plt.cm.gray, vmin=40, vmax=220)
|
||||
plt.axis('off')
|
||||
plt.title('noisy', fontsize=20)
|
||||
plt.subplot(132)
|
||||
plt.imshow(approx_bilateral_denoised, cmap=plt.cm.gray, vmin=40, vmax=220)
|
||||
plt.axis('off')
|
||||
plt.title('bilateral denoising', fontsize=20)
|
||||
|
||||
selem = disk(30)
|
||||
approx_bilateral_denoised = bilateral_mean(noisy.astype(np.uint8), selem=selem,s0=40,s1=40)
|
||||
plt.subplot(133)
|
||||
plt.imshow(approx_bilateral_denoised, cmap=plt.cm.gray, vmin=40, vmax=220)
|
||||
plt.axis('off')
|
||||
plt.title('(more) bilateral denoising', fontsize=20)
|
||||
|
||||
plt.subplots_adjust(wspace=0.02, hspace=0.02, top=0.9, bottom=0, left=0,right=1)
|
||||
plt.show()
|
||||
@@ -1,47 +0,0 @@
|
||||
"""
|
||||
=====================
|
||||
Local Autolevel
|
||||
=====================
|
||||
|
||||
Local autolevel stretch local histogram between 0 and max_graylevel (e.g. 255 for 8 bit image).
|
||||
The following code shows the difference between autolevel and percentile auto_level where [min,max] interval
|
||||
is replaced by [p0,p1] percentiles interval
|
||||
|
||||
"""
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
|
||||
from skimage import data
|
||||
|
||||
from skimage.filter.rank import percentile_autolevel,autolevel
|
||||
from skimage.morphology import disk
|
||||
|
||||
|
||||
image = data.camera()
|
||||
|
||||
selem = disk(20)
|
||||
loc_autolevel = autolevel(image,selem=selem)
|
||||
loc_perc_autolevel0 = percentile_autolevel(image,selem=selem,p0=.00,p1=1.0)
|
||||
loc_perc_autolevel1 = percentile_autolevel(image,selem=selem,p0=.01,p1=.99)
|
||||
loc_perc_autolevel2 = percentile_autolevel(image,selem=selem,p0=.05,p1=.95)
|
||||
loc_perc_autolevel3 = percentile_autolevel(image,selem=selem,p0=.1,p1=.9)
|
||||
|
||||
loc_perc_autolevel = np.hstack((loc_perc_autolevel0,loc_perc_autolevel1,loc_perc_autolevel2,loc_perc_autolevel3))
|
||||
|
||||
fig, axes = plt.subplots(nrows=3, figsize=(7, 8))
|
||||
ax0, ax1, ax2 = axes
|
||||
plt.gray()
|
||||
|
||||
ax0.imshow(image)
|
||||
ax0.set_title('Image')
|
||||
|
||||
ax1.imshow(loc_autolevel)
|
||||
ax1.set_title('Autolevel')
|
||||
|
||||
ax2.imshow(loc_perc_autolevel,vmin=0,vmax=255)
|
||||
ax2.set_title('percentile autolevel 0%,1%,5% and 10%')
|
||||
|
||||
for ax in axes:
|
||||
ax.axis('off')
|
||||
|
||||
plt.show()
|
||||
@@ -1,69 +0,0 @@
|
||||
"""
|
||||
=====================
|
||||
Local Thresholding
|
||||
=====================
|
||||
|
||||
Thresholding is the simplest way to segment objects from a background. If that
|
||||
background is relatively uniform, then you can use a global threshold value to
|
||||
binarize the image by pixel-intensity. If there's large variation in the
|
||||
background intensity, however, adaptive thresholding (a.k.a. local or dynamic
|
||||
thresholding) may produce better results.
|
||||
|
||||
Here, we binarize an image using the `threshold_adaptive` function, which
|
||||
calculates thresholds in regions of size `block_size` surrounding each pixel
|
||||
(i.e. local neighborhoods). Each threshold value is the weighted mean of the
|
||||
local neighborhood minus an offset value.
|
||||
|
||||
An other approach is to binarize locally the image using local histogram distribution.
|
||||
|
||||
rank.threshold function set pixels higher than the local mean to 1, to 0 otherwize
|
||||
rank.morph_contr_enh replaces each pixel by the local minimum (or local maximum) if the
|
||||
pixel gray level is more close to the local minimum (resp. by the local maximum
|
||||
if the pixel gray level is more close to the local maximum).
|
||||
|
||||
"""
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
from skimage import data
|
||||
from skimage.filter import threshold_otsu, threshold_adaptive
|
||||
|
||||
from skimage.filter.rank import threshold,morph_contr_enh
|
||||
from skimage.morphology import disk
|
||||
|
||||
|
||||
image = data.page()
|
||||
|
||||
global_thresh = threshold_otsu(image)
|
||||
binary_global = image > global_thresh
|
||||
|
||||
block_size = 40
|
||||
binary_adaptive = threshold_adaptive(image, block_size, offset=10)
|
||||
|
||||
selem = disk(20)
|
||||
loc_thresh = threshold(image,selem=selem)
|
||||
loc_morph_contr_enh = morph_contr_enh(image,selem=selem)
|
||||
|
||||
fig, axes = plt.subplots(nrows=5, figsize=(7, 8))
|
||||
ax0, ax1, ax2, ax3, ax4 = 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')
|
||||
|
||||
ax3.imshow(loc_thresh)
|
||||
ax3.set_title('Local thresholding')
|
||||
|
||||
ax4.imshow(loc_morph_contr_enh)
|
||||
ax4.set_title('Local morphological contrast enhancement')
|
||||
|
||||
|
||||
for ax in axes:
|
||||
ax.axis('off')
|
||||
|
||||
plt.show()
|
||||
@@ -335,7 +335,7 @@ def percentile_threshold(image, selem, out=None, mask=None, shift_x=False, shift
|
||||
local threshold : uint8 array or uint16 array depending on input image
|
||||
The result of the local threshold.
|
||||
|
||||
|
||||
|
||||
"""
|
||||
|
||||
return _apply(
|
||||
|
||||
Reference in New Issue
Block a user