Skip to content

Instantly share code, notes, and snippets.

@andreydung
Last active August 29, 2015 14:11
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 andreydung/98802b965c7774a78b9e to your computer and use it in GitHub Desktop.
Save andreydung/98802b965c7774a78b9e to your computer and use it in GitHub Desktop.
import numpy as np
def integral(mask):
"""
calculate integral image for quick histogram
"""
assert(mask.dtype == bool)
M, N = mask.shape
integral = np.zeros((M + 1, N + 1)).astype(int)
for i in range(1,M + 1):
for j in range(1,N + 1):
integral[i][j] = integral[i-1][j] + integral[i][j-1] - \
integral[i-1][j-1] + mask[i - 1][j - 1]
return integral
def padimage(integral, patch):
"""
pad for integral image only
"""
M, N = integral.shape
out = np.zeros((M + 2*patch, N + 2*patch))
out[patch: M + patch, patch: N + patch] = integral
out[(M+patch):,:] = np.tile(out[M+patch-1,:], (patch, 1))
out[:, (N+patch):] = np.tile(out[:, N+patch-1], (patch, 1)).transpose()
return out
def histogram(textonmap, Nclusters):
"""
calculate the histogram of textons in the image inside a sliding window
"""
patch = 1
M, N = textonmap.shape
histogram = np.zeros((M, N, Nclusters))
for lev in range(Nclusters):
mask = textonmap == lev
integral = integral(mask)
integral = padimage(integral, patch)
for i in range(M):
for j in range(N):
histogram[i][j][lev] = integral[i+2*patch+1][j+2*patch+1] + integral[i][j] - \
integral[i][j+2*patch+1] - integral[i+2*patch+1][j]
# histogram normalization
sums = histogram.sum(axis = 2)
histogram = histogram/sums[:,:,np.newaxis]
return histogram
textonmap = np.array([[1, 0, 0],[1, 0, 0],[1, 0, 0]])
print histogram(textonmap, 10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment