Last active
August 29, 2015 14:11
-
-
Save andreydung/98802b965c7774a78b9e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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