mirror of
https://github.com/wassname/scikit-image.git
synced 2026-07-15 11:25:53 +08:00
107 lines
2.7 KiB
Python
107 lines
2.7 KiB
Python
import numpy as np
|
|
|
|
import skimage
|
|
|
|
|
|
__all__ = ['histogram', 'cumulative_distribution', 'equalize']
|
|
|
|
|
|
def histogram(image, nbins=256):
|
|
"""Return histogram of image.
|
|
|
|
Unlike `numpy.histogram`, this function returns the centers of bins and
|
|
does not rebin integer arrays. For integer arrays, each integer value has
|
|
its own bin, which improves speed and intensity-resolution.
|
|
|
|
Parameters
|
|
----------
|
|
image : array
|
|
Input image.
|
|
nbins : int
|
|
Number of bins used to calculate histogram. This value is ignored for
|
|
integer arrays.
|
|
|
|
Returns
|
|
-------
|
|
hist : array
|
|
The values of the histogram.
|
|
bin_centers : array
|
|
The values at the center of the bins.
|
|
"""
|
|
|
|
# For integer types, histogramming with bincount is more efficient.
|
|
if np.issubdtype(image.dtype, np.integer):
|
|
offset = 0
|
|
if np.min(image) < 0:
|
|
offset = np.min(image)
|
|
hist = np.bincount(image.ravel() - offset)
|
|
bin_centers = np.arange(len(hist)) + offset
|
|
|
|
# clip histogram to start with a non-zero bin
|
|
idx = np.nonzero(hist)[0][0]
|
|
return hist[idx:], bin_centers[idx:]
|
|
else:
|
|
hist, bin_edges = np.histogram(image.flat, nbins)
|
|
bin_centers = (bin_edges[:-1] + bin_edges[1:]) / 2.
|
|
return hist, bin_centers
|
|
|
|
|
|
def cumulative_distribution(image, nbins=256):
|
|
"""Return cumulative distribution function (cdf) for the given image.
|
|
|
|
Parameters
|
|
----------
|
|
image : array
|
|
Image array.
|
|
nbins : int
|
|
Number of bins for image histogram.
|
|
|
|
Returns
|
|
-------
|
|
img_cdf : array
|
|
Values of cumulative distribution function.
|
|
bin_centers : array
|
|
Centers of bins.
|
|
|
|
References
|
|
----------
|
|
.. [1] http://en.wikipedia.org/wiki/Cumulative_distribution_function
|
|
|
|
"""
|
|
hist, bin_centers = histogram(image, nbins)
|
|
img_cdf = hist.cumsum()
|
|
img_cdf = img_cdf / float(img_cdf[-1])
|
|
return img_cdf, bin_centers
|
|
|
|
|
|
def equalize(image, nbins=256):
|
|
"""Return image after histogram equalization.
|
|
|
|
Parameters
|
|
----------
|
|
image : array
|
|
Image array.
|
|
nbins : int
|
|
Number of bins for image histogram.
|
|
|
|
Returns
|
|
-------
|
|
out : float array
|
|
Image array after histogram equalization.
|
|
|
|
Notes
|
|
-----
|
|
This function is adapted from [1]_ with the author's permission.
|
|
|
|
References
|
|
----------
|
|
.. [1] http://www.janeriksolem.net/2009/06/histogram-equalization-with-python-and.html
|
|
.. [2] http://en.wikipedia.org/wiki/Histogram_equalization
|
|
|
|
"""
|
|
image = skimage.img_as_float(image)
|
|
cdf, bin_centers = cumulative_distribution(image, nbins)
|
|
out = np.interp(image.flat, bin_centers, cdf)
|
|
return out.reshape(image.shape)
|
|
|