Created
January 9, 2020 04:05
-
-
Save ntdat017/55bedee495b48107204c9a7dcf0163b4 to your computer and use it in GitHub Desktop.
transform image with same histogram
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
import numpy as np | |
def hist_matching(src_img, dst_img): | |
shape = src_img.shape | |
src = src_img.ravel() | |
dst = dst_img.ravel() | |
s_values, bin_idx, s_counts = np.unique(src, return_inverse=True, return_counts=True) | |
d_values, d_counts = np.unique(dst, return_counts=True) | |
s_quantiles = np.cumsum(s_counts).astype(np.float64) | |
s_quantiles /= s_quantiles[-1] | |
d_quantiles = np.cumsum(d_counts).astype(np.float64) | |
d_quantiles /= d_quantiles[-1] | |
interp_d_values = np.interp(s_quantiles, d_quantiles, d_values).astype(np.int) | |
return interp_d_values[bin_idx].reshape(shape) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment