You cannot do that using mahotas
as the function which computes the co-occurrence map is not available in this library. An alternative approach to the extraction of texture features from the GLCM would consist in utilizing skimage.feature.graycoprops
(check out this thread for details).
But if you wish to stick to mahotas, you should try using skimage.util.view_as_windows
rather than a sliding window as it could speed up the scanning across the image. Please, be sure to read the caveat about memory usage at the end of the documentation. If using view_as_windows
is an affordable approach for you, the following code gets the job done:
import numpy as np
from skimage import io, util
import mahotas.features.texture as mht
def haralick_features(img, win, d):
win_sz = 2*win + 1
window_shape = (win_sz, win_sz)
arr = np.pad(img, win, mode='reflect')
windows = util.view_as_windows(arr, window_shape)
Nd = len(d)
feats = np.zeros(shape=windows.shape[:2] + (Nd, 4, 13), dtype=np.float64)
for m in xrange(windows.shape[0]):
for n in xrange(windows.shape[1]):
for i, di in enumerate(d):
w = windows[m, n, :, :]
feats[m, n, i, :, :] = mht.haralick(w, distance=di)
return feats.reshape(feats.shape[:2] + (-1,))
DEMO
For the sample run below I have set win
to 19
which corresponds to a window of shape (39, 39)
. I have considered two different distances. Notice that mht.haralick
yields 13 GLCM features for four directions. Altogether this results in a 104-dimensional feature vector for each pixel. When applied to a (250, 200)
pixels crop from a Landsat image, feature extraction finishes in around 7 minutes.
In [171]: img = io.imread('landsat_crop.tif')
In [172]: img.shape
Out[172]: (250L, 200L)
In [173]: win = 19
In [174]: d = (1, 2)
In [175]: %time feature_map = haralick_features(img, win, d)
Wall time: 7min 4s
In [176]: feature_map.shape
Out[176]: (250L, 200L, 104L)
In [177]: feature_map[0, 0, :]
Out[177]:
array([ 8.19278030e-03, 1.30863698e+01, 7.64234582e-01, ...,
3.59561817e+00, -1.35383606e-01, 8.32570045e-01])
In [178]: io.imshow(img)
Out[178]: <matplotlib.image.AxesImage at 0xc5a9b38>