title: R: Calculate Grey-Level-Co-Occurence-Matrix (GLCM) for an image
body: <p>To investigate on the distribution of pixelvalues in an image, I want to compute a Grey-Level-Co-Occurence-Matrix (GLCM) for entire Images (NO sliding/moving Windows). The idea is to receive a single value (for "mean", "variance", "homogeneity", "contrast", "dissimilarity", "entropy", "second_moment", "correlation") for every image, to compare the images among each other regarding their distribution of pixelvalues.</p>
  
  <p>e.g.:</p>
  
  <pre><code>image 1:
  
  0 0 0 0 
  0 0 0 1
  0 0 1 1
  0 1 1 1
  
  image 2:
  
  1 0 0 1 
  0 1 0 0
  0 0 1 0
  1 0 0 1
  
  image 3:
  
  1 1 1 0
  1 1 0 0
  1 0 0 0
  0 0 0 0
  
  </code></pre>
  
  <p>All of These 3 images have got the same statistics (mean, max, min, …), nevertheless the distribution of the pixelvalues is completely different. To find kind of a measure to describe that difference, I want to compute the GLCM´s for each of these images.</p>
  
  <p>I am using the package "glcm" so far, a fantastic package for texture-analysis by Alex Zvoleff. Unfortunately it´s just possible to use it with a sliding/moving window… But since I want to receive one single value for every image per statistical measure it seems to be useless for me... Is there anyone who can help an R-Rookie like me out with that? :)</p>
  
  <pre><code>install.packages("glcm")
  library(glcm)
  # install and load package "glcm"
  # see URL:http://azvoleff.com/articles/calculating-image-textures-with-glcm/
  
  values &lt;- seq(1, c(12*12), 1)
  values_mtx &lt;- matrix(data = values, nrow = 12, ncol = 12, byrow = TRUE)
  # create an "image"
  
  values_mtx_small &lt;- values_mtx[-12, -12]
  # since you have to use a sliding/moving window in glcm::glcm() give the image # ...an odd number of rows and cols by deleting the last row and last column
  
  values_raster_small &lt;- raster(values_mtx_small)
  # create rasterlayer-object
  
  values_textures &lt;- glcm::glcm(values_raster_small, window = c((nrow(values_raster_small)-2), (ncol(values_raster_small)-2)), shift=list(c(0,1), c(1,1), c(1,0), c(1,-1)), statistics = c("mean", "variance", "homogeneity", "contrast", "dissimilarity", "entropy", "second_moment", "correlation"), min_x = NULL, max_x = NULL, na_opt = "ignore", na_val = NA, asinteger = FALSE)
  # compute a GLCM for the image with a maximum size for the moving window to
  # ...receive a "measure" for the image
  
  values_textures_mean &lt;- as.matrix(values_textures$glcm_mean)
  # extract the calculated GLCM_mean data
  
  values_textures_mean
  # get an Output
  
     [,1] [,2] [,3] [,4] [,5]      [,6] [,7] [,8] [,9] [,10] [,11]
   [1,]   NA   NA   NA   NA   NA        NA   NA   NA   NA    NA    NA
   [2,]   NA   NA   NA   NA   NA        NA   NA   NA   NA    NA    NA
   [3,]   NA   NA   NA   NA   NA        NA   NA   NA   NA    NA    NA
   [4,]   NA   NA   NA   NA   NA        NA   NA   NA   NA    NA    NA
   [5,]   NA   NA   NA   NA   NA 0.4589603   NA   NA   NA    NA    NA
   [6,]   NA   NA   NA   NA   NA 0.5516493   NA   NA   NA    NA    NA
   [7,]   NA   NA   NA   NA   NA        NA   NA   NA   NA    NA    NA
   [8,]   NA   NA   NA   NA   NA        NA   NA   NA   NA    NA    NA
   [9,]   NA   NA   NA   NA   NA        NA   NA   NA   NA    NA    NA
  [10,]   NA   NA   NA   NA   NA        NA   NA   NA   NA    NA    NA
  [11,]   NA   NA   NA   NA   NA        NA   NA   NA   NA    NA    NA
  # unfortunately two numbers as "measure" are left…
  </code></pre>