mirror of
https://github.com/wassname/scikit-image.git
synced 2026-07-05 05:56:36 +08:00
Add rescale_intensity function with test and example.
This commit is contained in:
@@ -8,10 +8,14 @@ histogram equalization. Histogram equalization enhances contrast by "spreading
|
||||
out the most frequent intensity values" in an image [1]_. The equalized image
|
||||
has a roughly linear cumulative distribution function, as shown in this example.
|
||||
|
||||
For comparison, this example also shows an image after its intensity levels are
|
||||
uniformly stretched.
|
||||
|
||||
.. [1] http://en.wikipedia.org/wiki/Histogram_equalization
|
||||
|
||||
"""
|
||||
import matplotlib.pyplot as plt
|
||||
from matplotlib import ticker
|
||||
|
||||
from skimage import data
|
||||
from skimage.util.dtype import dtype_range
|
||||
@@ -22,35 +26,49 @@ def plot_hist(img, bins=256):
|
||||
"""Plot histogram and cumulative histogram for image"""
|
||||
img_cdf, bins = exposure.cumulative_distribution(img, bins)
|
||||
plt.hist(img.ravel(), bins=bins)
|
||||
plt.ylabel('Number of pixels')
|
||||
plt.xlabel('Pixel intensiy')
|
||||
# Shorten y-tick labels using scientific notation
|
||||
y_formatter = ticker.ScalarFormatter(useOffset=True)
|
||||
y_formatter.set_powerlimits((0, 0)) # force use of scientific notation
|
||||
ax = plt.gca()
|
||||
ax.yaxis.set_major_formatter(y_formatter)
|
||||
|
||||
ax_cdf = plt.twinx()
|
||||
ax_cdf.plot(bins, img_cdf, 'r')
|
||||
xmin, xmax = dtype_range[img.dtype.type]
|
||||
plt.xlim(xmin, xmax)
|
||||
|
||||
ax_cdf.set_ylabel('Fraction of total intensity')
|
||||
|
||||
img_orig = data.camera()
|
||||
# squeeze image intensities to lower image contrast
|
||||
img = img_orig / 5 + 100
|
||||
img_rescale = exposure.rescale_intensity(img)
|
||||
img_eq = exposure.equalize(img)
|
||||
|
||||
plt.subplot(2, 2, 1)
|
||||
plt.subplot(2, 3, 1)
|
||||
plt.imshow(img, cmap=plt.cm.gray, vmin=0, vmax=255)
|
||||
plt.title('Low contrast input image')
|
||||
plt.title('Low contrast image')
|
||||
plt.axis('off')
|
||||
plt.subplot(2, 2, 2)
|
||||
plt.subplot(2, 3, 4)
|
||||
plt.ylabel('Number of pixels')
|
||||
plot_hist(img)
|
||||
|
||||
plt.subplot(2, 2, 3)
|
||||
plt.imshow(img_eq, cmap=plt.cm.gray, vmin=0, vmax=1)
|
||||
plt.title('After\nhistogram equalization')
|
||||
plt.subplot(2, 3, 2)
|
||||
plt.imshow(img_rescale, cmap=plt.cm.gray, vmin=0, vmax=255)
|
||||
plt.title('Rescale intensities')
|
||||
plt.axis('off')
|
||||
plt.subplot(2, 2, 4)
|
||||
plot_hist(img_eq)
|
||||
plt.subplot(2, 3, 5)
|
||||
plot_hist(img_rescale)
|
||||
|
||||
plt.subplots_adjust(left=0.05, hspace=0.25, wspace=0.3, top=0.95, bottom=0.1)
|
||||
plt.subplot(2, 3, 3)
|
||||
plt.imshow(img_eq, cmap=plt.cm.gray, vmin=0, vmax=1)
|
||||
plt.title('Histogram equalization')
|
||||
plt.axis('off')
|
||||
plt.subplot(2, 3, 6)
|
||||
plot_hist(img_eq)
|
||||
plt.ylabel('Fraction of total intensity')
|
||||
|
||||
# prevent overlap of y-axis labels
|
||||
plt.subplots_adjust(wspace=0.4)
|
||||
plt.show()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user