GLCM feature texture extraction

  levels = 8,
  kernel_size = 5,
  distance = 1,
  angle = 0,
  dir_save = NULL,
  threads = 1,
  verbose = FALSE



a numeric matrix


a vector of character strings. One or all of the following: 'mean', 'std', 'contrast', 'dissimilarity', 'homogeneity', 'ASM', 'energy', 'max', 'entropy'


an integer specifying the window size. This parameter will create a mask of size levels x levels internally


an integer specifying the kernel size. A kernel of 1's will be created and the cv2.filter2D filter will be utilized for the convolution


a numeric value specifying the pixel pair distance offsets (a 'pixel' value such as 1.0, 2.0 etc.)


a numeric value specifying the pixel pair angles (a 'degree' value such as 0.0, 30.0, 45.0, 90.0 etc.)


either NULL or a character string specifying a valid path to a directory where the output GLCM matrices (for the specified 'methods') will be saved. By setting this parameter to a valid directory the memory usage will be decreased.


an integer value specifying the number of cores to run in parallel


a boolean. If TRUE then information will be printed out in the console


a list consisting of one or more GLCM features


The following are two factors which (highly probable) will increase memory usage during computations:

  • 1st. the image size (the user might have to resize the image first)

  • 2nd. the 'levels' parameter. The bigger this parameter the more matrices will be initialized and more memory will be used. For instance if the 'levels' parameter equals to 8 then 8 * 8 = 64 matrices of equal size to the input image will be initialized. That means if the image has dimensions (2745 x 2745) and the image-object size is approx. 60 MB then by initializing 64 matrices the memory will increase to 3.86 GB.

This function is an Rcpp implementation of the python fastGLCM module. When using each function separately by utilizing all threads it's slightly faster compared to the python vectorized functions, however it's a lot faster when computing all features at once.

The dir_save parameter allows the user to save the GLCM's as .csv files to a directory. That way the output GLCM's matrices won't be returned in the R session (reduced memory usage). However, by saving the GLCM's to .csv files the computation time increases.



#> Loading required package: OpenImageR

temp_dir = tempdir(check = FALSE)
# temp_dir

zip_file = system.file('images', '', package = "fastGLCM")
utils::unzip(zip_file, exdir = temp_dir)
path_extracted = file.path(temp_dir, 'JAXA_Joso-City2_PAN.tif')

im = readImage(path = path_extracted)
#> [1] 1555 1414

# resize the image and adjust pixel values range

im = resizeImage(im, 500, 500, 'nearest')
im = OpenImageR::norm_matrix_range(im, 0, 255)

# computation of all GLCM features

methods = c('mean',

res_glcm = fastGLCM_Rcpp(data = im,
                         methods = methods,
                         levels = 8,
                         kernel_size = 5,
                         distance = 1.0,
                         angle = 0.0,
                         threads = 1)
# str(res_glcm)

# plot_multi_images(list_images = res_glcm,
#                   par_ROWS = 2,
#                   par_COLS = 5,
#                   titles = methods)

if (file.exists(path_extracted)) file.remove(path_extracted)
#> [1] TRUE