mirror of
https://github.com/wassname/scikit-image.git
synced 2026-06-30 17:41:49 +08:00
add examples - to be cont.
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
"""
|
||||
====================================================
|
||||
Denoising the picture of Lena using total variation
|
||||
====================================================
|
||||
|
||||
In this example, we denoise a noisy version of the picture of Lena
|
||||
using the total variation denoising filter. The result of this filter
|
||||
is an image that has a minimal total variation norm, while being as
|
||||
close to the initial image as possible. The total variation is the L1
|
||||
norm of the gradient of the image, and minimizing the total variation
|
||||
typically produces "posterized" images with flat domains separated by
|
||||
sharp edges.
|
||||
|
||||
It is possible to change the degree of posterization by controlling
|
||||
the tradeoff between denoising and faithfulness to the original image.
|
||||
|
||||
"""
|
||||
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
from skimage import data, color, img_as_ubyte
|
||||
from skimage.filter import tv_denoise
|
||||
from skimage.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)
|
||||
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(bilateral_denoised, cmap=plt.cm.gray, vmin=40, vmax=220)
|
||||
plt.axis('off')
|
||||
plt.title('bilateral denoising', fontsize=20)
|
||||
|
||||
selem = disk(30)
|
||||
bilateral_denoised = bilateral_mean(noisy.astype(np.uint8), selem=selem,s0=30,s1=30)
|
||||
plt.subplot(133)
|
||||
plt.imshow(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()
|
||||
@@ -0,0 +1,86 @@
|
||||
"""
|
||||
===============================
|
||||
Local Histogram Equalization
|
||||
===============================
|
||||
|
||||
This examples enhances an image with low contrast, using a method called
|
||||
*local histogram equalization*, which "spreads out the most frequent intensity
|
||||
values" in an image . The equalized image has a roughly linear cumulative
|
||||
distribution function for each pixel neigborhood.
|
||||
|
||||
to be adjusted...
|
||||
|
||||
.. [1] http://en.wikipedia.org/wiki/Histogram_equalization
|
||||
.. [2] http://homepages.inf.ed.ac.uk/rbf/HIPR2/stretch.htm
|
||||
|
||||
"""
|
||||
|
||||
from skimage import data
|
||||
from skimage.util.dtype import dtype_range
|
||||
from skimage import exposure
|
||||
from skimage.rank import egalise
|
||||
from skimage.morphology import disk
|
||||
|
||||
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
import numpy as np
|
||||
|
||||
def plot_img_and_hist(img, axes, bins=256):
|
||||
"""Plot an image along with its histogram and cumulative histogram.
|
||||
|
||||
"""
|
||||
ax_img, ax_hist = axes
|
||||
ax_cdf = ax_hist.twinx()
|
||||
|
||||
# Display image
|
||||
ax_img.imshow(img, cmap=plt.cm.gray)
|
||||
ax_img.set_axis_off()
|
||||
|
||||
# Display histogram
|
||||
ax_hist.hist(img.ravel(), bins=bins)
|
||||
ax_hist.ticklabel_format(axis='y', style='scientific', scilimits=(0, 0))
|
||||
ax_hist.set_xlabel('Pixel intensity')
|
||||
|
||||
xmin, xmax = dtype_range[img.dtype.type]
|
||||
ax_hist.set_xlim(xmin, xmax)
|
||||
|
||||
# Display cumulative distribution
|
||||
img_cdf, bins = exposure.cumulative_distribution(img, bins)
|
||||
ax_cdf.plot(bins, img_cdf, 'r')
|
||||
|
||||
return ax_img, ax_hist, ax_cdf
|
||||
|
||||
|
||||
# Load an example image
|
||||
img = data.moon()
|
||||
|
||||
# Contrast stretching
|
||||
p2 = np.percentile(img, 2)
|
||||
p98 = np.percentile(img, 98)
|
||||
img_rescale = exposure.rescale_intensity(img, in_range=(p2, p98))
|
||||
|
||||
# Equalization
|
||||
selem = disk(30)
|
||||
img_eq = egalise(img,selem=selem)
|
||||
|
||||
|
||||
# Display results
|
||||
f, axes = plt.subplots(2, 3, figsize=(8, 4))
|
||||
|
||||
ax_img, ax_hist, ax_cdf = plot_img_and_hist(img, axes[:, 0])
|
||||
ax_img.set_title('Low contrast image')
|
||||
ax_hist.set_ylabel('Number of pixels')
|
||||
|
||||
ax_img, ax_hist, ax_cdf = plot_img_and_hist(img_rescale, axes[:, 1])
|
||||
ax_img.set_title('Contrast stretching')
|
||||
|
||||
ax_img, ax_hist, ax_cdf = plot_img_and_hist(img_eq, axes[:, 2])
|
||||
ax_img.set_title('Local Histogram equalization')
|
||||
ax_cdf.set_ylabel('Fraction of total intensity')
|
||||
|
||||
|
||||
# prevent overlap of y-axis labels
|
||||
plt.subplots_adjust(wspace=0.4)
|
||||
plt.show()
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
"""
|
||||
=====================
|
||||
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.
|
||||
|
||||
Added local threshold using rank filter
|
||||
|
||||
to be adjusted ...
|
||||
|
||||
"""
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
from skimage import data
|
||||
from skimage.filter import threshold_otsu, threshold_adaptive
|
||||
|
||||
from skimage.rank import threshold
|
||||
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(10)
|
||||
loc_thresh = threshold(image,selem=selem)
|
||||
|
||||
fig, axes = plt.subplots(nrows=4, figsize=(7, 8))
|
||||
ax0, ax1, ax2, ax3 = 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')
|
||||
|
||||
|
||||
for ax in axes:
|
||||
ax.axis('off')
|
||||
|
||||
plt.show()
|
||||
Reference in New Issue
Block a user