title: Implementing GLCM texture feature with scikit-image and Python
body: <p>I am trying to implement a texture image as <a href="http://www.fp.ucalgary.ca/mhallbey/texture_calculations.htm" rel="nofollow noreferrer">described in this tutorial</a> using Python and skimage. </p>
  
  <p>The issue is to move a 7x7 window over a large raster and replace the center of each pixel with the calculated texture from the 7x7 window. I manage to do this with the code below, but I see no other way than looping through each individual pixel, which is very slow.</p>
  
  <p>One software package does that in a few seconds, so there must be some other way ... is there?</p>
  
  <p>Here the code that works but is very slow ... </p>
  
  <pre><code>import matplotlib.pyplot as plt
  import gdal, gdalconst
  import numpy as np
  from skimage.feature import greycomatrix, greycoprops
  
  filename = "//mnt//glaciology//RS2_20140101.jpg"
  outfilename = "//home//max//Documents//GLCM_contrast.tif"
  sarfile = gdal.Open(filename, gdalconst.GA_ReadOnly)
  
  sarraster = sarfile.ReadAsArray()
  #sarraster is satellite image, testraster will receive texture
  testraster = np.copy(sarraster)
  testraster[:] = 0
  
  for i in range(testraster.shape[0] ):
      print i,
      for j in range(testraster.shape[1] ):
  
          #windows needs to fit completely in image
          if i &lt;3 or j &lt;3:
              continue
          if i &gt; (testraster.shape[0] - 4) or j &gt; (testraster.shape[0] - 4):
              continue
  
          #Calculate GLCM on a 7x7 window
          glcm_window = sarraster[i-3: i+4, j-3 : j+4]
          glcm = greycomatrix(glcm_window, [1], [0],  symmetric = True, normed = True )
  
          #Calculate contrast and replace center pixel
          contrast = greycoprops(glcm, 'contrast')
          testraster[i,j]= contrast
  
  sarplot = plt.imshow(testraster, cmap = 'gray')
  </code></pre>
  
  <p>Results:</p>
  
  <p><a href="https://i.stack.imgur.com/IGqju.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IGqju.png" alt="Contrast GLCM"></a></p>