Skip to content

Instantly share code, notes, and snippets.

@Swarchal
Last active November 9, 2015 21:13
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 Swarchal/16442d68218801eb1b89 to your computer and use it in GitHub Desktop.
Save Swarchal/16442d68218801eb1b89 to your computer and use it in GitHub Desktop.
import matplotlib.pyplot as plt
from skimage.io import imread
from skimage.morphology import disk
from skimage.filters import rank
import numpy as np
im_all = imread("/home/scott/Dropbox/wally/ww2.jpg")
all_red = im_all[:, :, 0]
im_wally = imread("/home/scott/Dropbox/wally/wally2.png")
wally_red = im_wally[:, :, 0]
def windowed_histogram_similarity(image, selem, reference_hist, n_bins):
# Compute normalized windowed histogram feature vector for each pixel
px_histograms = rank.windowed_histogram(image, selem, n_bins=n_bins)
X = px_histograms
Y = reference_hist
num = (X - Y) ** 2
denom = X + Y
denom[denom == 0] = np.infty
frac = num / denom
chi_sqr = 0.5 * np.sum(frac, axis=2)
similarity = 1 / (chi_sqr + 1.0e-10)
return similarity
# need to reduce grayscales values to 16 levels
def quant_16(image):
image_out = image // 16
return image_out
wally16 = quant_16(wally_red)
all16 = quant_16(all_red)
# compute wally histogram and normalize
wally_hist, _ = np.histogram(im_wally.flatten(), bins = 16, range = (0, 16))
wally_hist = wally_hist.astype(float) / np.sum(wally_hist)
selem = disk(25)
similarity = windowed_histogram_similarity(all16, selem, wally_hist,
wally_hist.shape[0])
fig = plt.figure(figsize = (12,12))
ax1 = plt.subplot(2,1,1)
ax1.imshow(im_all)
ax1.set_title("Original Image")
ax2 = plt.subplot(2,1,2)
ax2.imshow(all16, cmap = plt.cm.gray)
ax2.imshow(similarity, cmap = plt.cm.Spectral_r, alpha = 0.5)
ax2.set_title("Similarity to Wally")
plt.savefig("/home/scott/Dropbox/wally/histogram_window.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment