I am using scikit-image's greycomatrix (GLCM) to extract features from an image. This method is quite fast, but when creating a feature map of an image with GLCM one needs to glide a window over the image and call greycomatrix once for each window. For a 256x256 image that is 65536 calls. This becomes very slow with simple Python loops. I have been searching high and low, but I cannot find an existing implementation of this window gliding method, so I have made it myself so far. It seems strange that this doesn't already exist within scikit-image since this is the primary way to use the GLCM.
In the scikit-image GLCM tutorial (http://scikit-image.org/docs/dev/auto_examples/plot_glcm.html) they only analyse a few individual windows and say nothing about gliding windows. That is not very helpful.
import numpy as np
from skimage.feature import greycomatrix
from skimage.data import coins
def glide(image, w, d, theta, levels=16):
image = np.pad(image, int(w/2), mode='reflect') # Add padding.
M, N = image.shape
feature_map = np.zeros((M, N)) # Placeholder for some feature.
for m in xrange(0, M):
for n in xrange(0, N):
window = image[m:m+w, n:n+w]
glcm = greycomatrix(window, d, theta, levels)
# Do something with glcm: Find variance, entropy, etc.
feature_map[m,n] = 1. # Compute something here.
feature_map = glide(coins(), w=21, d=[5], theta=[0], levels=256)
This is my custom gliding window method. Does a more efficient version of this already exist within scikit-image or similar?