Created
April 9, 2019 08:55
-
-
Save steermomo/da4663a91ef19bd345b7bd844a3e56de to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def hist_match(source, template): | |
""" | |
Adjust the pixel values of a grayscale image such that its histogram | |
matches that of a target image | |
Arguments: | |
----------- | |
source: np.ndarray | |
Image to transform; the histogram is computed over the flattened | |
array | |
template: np.ndarray | |
Template image; can have different dimensions to source | |
Returns: | |
----------- | |
matched: np.ndarray | |
The transformed output image | |
""" | |
oldshape = source.shape | |
source = source.ravel() | |
template = template.ravel() | |
# get the set of unique pixel values and their corresponding indices and | |
# counts | |
s_values, bin_idx, s_counts = np.unique(source, return_inverse=True, | |
return_counts=True) | |
t_values, t_counts = np.unique(template, return_counts=True) | |
# take the cumsum of the counts and normalize by the number of pixels to | |
# get the empirical cumulative distribution functions for the source and | |
# template images (maps pixel value --> quantile) | |
s_quantiles = np.cumsum(s_counts).astype(np.float64) | |
s_quantiles /= s_quantiles[-1] | |
t_quantiles = np.cumsum(t_counts).astype(np.float64) | |
t_quantiles /= t_quantiles[-1] | |
# interpolate linearly | |
interp_t_values = np.interp(s_quantiles, t_quantiles, t_values) | |
return interp_t_values[bin_idx].reshape(oldshape) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment