Documentation Center

  • Trial Software
  • Product Updates


Create gray-level co-occurrence matrix from image


glcm = graycomatrix(I)
glcms = graycomatrix(I, param1, val1, param2, val2,...)
[glcm, SI] = graycomatrix(...)


glcm = graycomatrix(I) creates a gray-level co-occurrence matrix (GLCM) from image I. graycomatrix creates the GLCM by calculating how often a pixel with gray-level (grayscale intensity) value i occurs horizontally adjacent to a pixel with the value j. (You can specify other pixel spatial relationships using the 'Offsets' parameter -- see Parameters.) Each element (i,j) in glcm specifies the number of times that the pixel with value i occurred horizontally adjacent to a pixel with value j.

graycomatrix calculates the GLCM from a scaled version of the image. By default, if I is a binary image, graycomatrix scales the image to two gray-levels. If I is an intensity image, graycomatrix scales the image to eight gray-levels. You can specify the number of gray-levels graycomatrix uses to scale the image by using the 'NumLevels' parameter, and the way that graycomatrix scales the values using the 'GrayLimits' parameter — see Parameters.

The following figure shows how graycomatrix calculates several values in the GLCM of the 4-by-5 image I. Element (1,1) in the GLCM contains the value 1 because there is only one instance in the image where two, horizontally adjacent pixels have the values 1 and 1. Element (1,2) in the GLCM contains the value 2 because there are two instances in the image where two, horizontally adjacent pixels have the values 1 and 2. graycomatrix continues this processing to fill in all the values in the GLCM.

glcms = graycomatrix(I, param1, val1, param2, val2,...) returns one or more gray-level co-occurrence matrices, depending on the values of the optional parameter/value pairs. Parameter names can be abbreviated, and case does not matter.


The following table lists these parameters in alphabetical order.





A two element vector, [low high], that specifies how the values in I are scaled into gray levels. If N is the number of gray levels (see parameter 'NumLevels') to use for scaling, the range [low high] is divided into N equal width bins and values in a bin get mapped to a single gray level. Grayscale values less than or equal to low are scaled to 1. Grayscale values greater than or equal to high are scaled to 'NumLevels'. If'GrayLimits' is set to [], graycomatrix uses the minimum and maximum grayscale values in I as limits, [min(I(:)) max(I(:))].

Minimum and maximum specified by class, e.g.
double [0 1]
[-32768 32767]


Integer specifying the number of gray-levels to use when scaling the grayscale values in I. For example, if NumLevels is 8, graycomatrix scales the values in I so they are integers between 1 and 8. The number of gray-levels determines the size of the gray-level co-occurrence matrix (glcm).

8 (numeric)
2 (binary)


p-by-2 array of integers specifying the distance between the pixel of interest and its neighbor. Each row in the array is a two-element vector, [row_offset, col_offset], that specifies the relationship, or offset, of a pair of pixels. row_offset is the number of rows between the pixel-of-interest and its neighbor. col_offset is the number of columns between the pixel-of-interest and its neighbor. Because the offset is often expressed as an angle, the following table lists the offset values that specify common angles, given the pixel distance D.

Angle        Offset

  0              [  0 D ]

 45             [ -D D ]

 90             [ -D 0 ]

135           [ -D -D ]

The figure illustrates the array: offset = [0 1; -1 1; -1 0; -1 -1]

[0 1]


Boolean that creates a GLCM where the ordering of values in the pixel pairs is not considered. For example, when 'Symmetric' is set to true, graycomatrix counts both 1,2 and 2,1 pairings when calculating the number of times the value 1 is adjacent to the value 2. When 'Symmetric' is set to false, graycomatrix only counts 1,2 or 2,1, depending on the value of 'offset'. See Notes.


[glcm, SI] = graycomatrix(...) returns the scaled image, SI, used to calculate the gray-level co-occurrence matrix. The values in SI are between 1 and NumLevels.

Class Support

I can be numeric or logical but must be two-dimensional, real, and nonsparse. SI is a double matrix having the same size as I. glcms is a 'NumLevels'-by-'NumLevels'-by-P double array where P is the number of offsets in 'Offset'.


Another name for a gray-level co-occurrence matrix is a gray-level spatial dependence matrix. Also, the word co-occurrence is frequently used in the literature without a hyphen, cooccurrence.

graycomatrix ignores pixel pairs if either of the pixels contains a NaN.

graycomatrix replaces positive Infs with the value NumLevels and replaces negative Infs with the value 1.

graycomatrix ignores border pixels, if the corresponding neighbor pixel falls outside the image boundaries.

The GLCM created when 'Symmetric' is set to true is symmetric across its diagonal, and is equivalent to the GLCM described by Haralick (1973). The GLCM produced by the following syntax, with 'Symmetric' set to true

 graycomatrix(I, 'offset', [0 1], 'Symmetric', true)

is equivalent to the sum of the two GLCMs produced by the following statements where'Symmetric' is set to false.

graycomatrix(I, 'offset', [0 1], 'Symmetric', false) 
graycomatrix(I, 'offset', [0 -1], 'Symmetric', false)


Calculate the gray-level co-occurrence matrix for a grayscale image.

I = imread('circuit.tif');
glcm = graycomatrix(I,'Offset',[2 0]);

Calculate the gray-level co-occurrence matrix and return the scaled version of the image, SI, used by graycomatrix to generate the GLCM.

I = [ 1 1 5 6 8 8; 2 3 5 7 0 2; 0 2 3 5 6 7];
[glcm,SI] = graycomatrix(I,'NumLevels',9,'G',[])


Haralick, R.M., K. Shanmugan, and I. Dinstein, "Textural Features for Image Classification", IEEE Transactions on Systems, Man, and Cybernetics, Vol. SMC-3, 1973, pp. 610-621.

Haralick, R.M., and L.G. Shapiro. Computer and Robot Vision: Vol. 1, Addison-Wesley, 1992, p. 459.

See Also

Was this topic helpful?