Skip to content

Instantly share code, notes, and snippets.

@ntdat017
Created January 9, 2020 04:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ntdat017/55bedee495b48107204c9a7dcf0163b4 to your computer and use it in GitHub Desktop.
Save ntdat017/55bedee495b48107204c9a7dcf0163b4 to your computer and use it in GitHub Desktop.
transform image with same histogram
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