Create graylevel cooccurrence matrix from image
glcm = graycomatrix(I)
glcms = graycomatrix(I, param1, val1,
param2, val2,...)
[glcm, SI] = graycomatrix(...)
creates
a graylevel cooccurrence matrix (GLCM) from image I. glcm = graycomatrix(I)
graycomatrix
creates
the GLCM by calculating how often a pixel with graylevel (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 graylevels.
If I
is an intensity image, graycomatrix
scales
the image to eight graylevels. You can specify the number of graylevels 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 4by5 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 graylevel cooccurrence
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.
Parameter  Description  Default 

 A two element vector,  Minimum and maximum specified by class, e.g. 
 Integer specifying the number of graylevels to use when
scaling the grayscale values in  8 (numeric) 
 pby2 array of integers specifying the distance between
the pixel of interest and its neighbor. Each row in the array is a
twoelement vector, Angle Offset 0 45 90 135 The
figure illustrates the array:


'Symmetric'  Boolean that creates a GLCM where the ordering of values
in the pixel pairs is not considered. For example, when  false 
[glcm, SI] = graycomatrix(...)
returns
the scaled image, SI
, used to calculate the graylevel
cooccurrence matrix. The values in SI
are between
1 and NumLevels
.
I
can be numeric or logical but must be twodimensional,
real, and nonsparse. SI is a double matrix having the same size as
I. glcms
is a 'NumLevels'
by'NumLevels'
byP
double
array where P
is the number of offsets in 'Offset'
.
Another name for a graylevel cooccurrence matrix is a graylevel spatial dependence matrix. Also, the word cooccurrence 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 graylevel cooccurrence matrix for a grayscale image.
I = imread('circuit.tif'); glcm = graycomatrix(I,'Offset',[2 0]);
Calculate the graylevel cooccurrence 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. SMC3, 1973, pp. 610621.
Haralick, R.M., and L.G. Shapiro. Computer and Robot Vision: Vol. 1, AddisonWesley, 1992, p. 459.