Skip to content

Instantly share code, notes, and snippets.

@brikeats
Created May 25, 2016 22:00
Show Gist options
  • Save brikeats/9efbe2349042fbf5fb556f40105838a1 to your computer and use it in GitHub Desktop.
Save brikeats/9efbe2349042fbf5fb556f40105838a1 to your computer and use it in GitHub Desktop.
Histogam matching. This should be part of skimage
def hist_match(im, ref_im):
if np.squeeze(im).ndim == 2:
im = np.expand_dims(im, axis=2)
ref_im = np.expand_dims(ref_im, axis=2)
out_im = np.empty_like(im)
for chan_num in range(im.shape[2]):
chan, ref_chan = im[...,chan_num], ref_im[...,chan_num]
cdf, bin_centers = cumulative_distribution(chan)
ref_cdf, ref_bin_centers = cumulative_distribution(ref_chan)
chan_percentiles = np.interp(chan.flat, bin_centers, cdf)
flat_out = np.interp(chan_percentiles, ref_cdf, ref_bin_centers)
out = flat_out.reshape(chan.shape)
out_im[..., chan_num] = out
return np.squeeze(out_im)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment