Skip to content

Instantly share code, notes, and snippets.

@JunsikChoi
Created October 20, 2017 10:14
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 JunsikChoi/fe6804d2d07d8c3c32fa91d7e0a6e1f5 to your computer and use it in GitHub Desktop.
Save JunsikChoi/fe6804d2d07d8c3c32fa91d7e0a6e1f5 to your computer and use it in GitHub Desktop.
histogram_matching in slice-wise manner.
def hist_match(im_src, im_tar, n_bins):
# normalize both images
src_max = im_src.max()
im_src_n = min_max_norm(im_src)
im_tar_n = min_max_norm(im_tar)
# Get histogram
hist_src, bins = np.histogram(im_src_n.flatten(), n_bins)
hist_tar, bins = np.histogram(im_tar_n.flatten(), n_bins)
# Normalize cumulative density function
cdf_src = hist_src.cumsum()
cdf_tar = hist_tar.cumsum()
# cdf_src = cdf_src / cdf_src[-1]
cdf_tar = cdf_tar / cdf_tar[-1]
# Interpolation & reshape
im_match = np.interp(im_src_n.flatten(), cdf_tar, bins[:-1]).astype(np.float32)
im_match = im_match.reshape((im_src.shape[0], im_src.shape[1]))
# TODO : Should I multiply src_max?
im_match_n = min_max_norm(im_match)
im_match_n = im_match_n * src_max
return im_match_n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment