Compute the gray-level co-occurrence matrix (GLCM) for an image.
graycomatrix
calculates the GLCM for the input image img. The GLCM
is a statistical method of examining the textures that considers the spatial
relationship of pixels.
Inputs
A grayscale image (2D matrix) for which the GLCM is to be computed. Image type can be int8, uint8, int16, uint16, int32, uint32, single, double or logical.
Name-Value Pair Arguments
An M-by-2 array specifying the pixel pair offsets for which the GLCM is calculated. Each row in the array is a two-element integer vector [row_offset, col_offset]. The default is [0 1], which calculates the GLCM for horizontally adjacent pixels.
A scalar specifying the number of gray levels to use when scaling the input image. The default is 2 for logical image and 8 for any other image. The image is scaled to have integer values between1 and the number of levels.
A two-element vector [low high] that specifies the intensity values in img that are to be considered when scaling the image to the number of levels. The default is the range of the class of img. if [] is used as input, the graylimits are [min(img(:)) max(img(:))].
A logical value indicating whether the GLCM should be symmetric. If true, the GLCM is averaged with its transpose to ensure symmetry. The default is false.
A logical value indicating whether the GLCM computation will use oct fiie. If true the GLCM is computed using cpp code and oct interface. If false, the computation is done using Octave function compute_glcm. The default is true. The oct computation is a little bit faster, and the compute_glcm is left here for educational reasons (Octave only parameter).
Outputs
The gray-level co-occurrence matrix or matrices. If multiple offsets are specified, glcm will be a 3D array, where each slice along the third dimension corresponds to the GLCM for a particular offset.
Scaled image used to calculate the GLCM (type double).
Example
img = phantom(); glcm = graycomatrix (img, 'NumLevels', 16, 'Offset', [0 1; -1 1], 'Symmetric', true);
See also: imhist.
The following code
% Demonstration of graycomatrix with phantom image disp("Demonstrating graycomatrix with the Shepp-Logan phantom:"); P = phantom(50); figure(1); imagesc(P); colormap(gray); title("Phantom Image"); colorbar; axis image; % Calculate GLCMs with different parameters [glcm1, SI] = graycomatrix(P, "NumLevels", 8, "Offset", [0 1]); [glcm2, ~] = graycomatrix(P, "NumLevels", 8, "Offset", [1 0]); [glcm3, ~] = graycomatrix(P, "NumLevels", 8, "Offset", [1 1]); [glcm4, ~] = graycomatrix(P, "NumLevels", 8, "Offset", [0 1], "Symmetric", true); % Display the scaled image and GLCMs figure(2); imagesc(SI); colormap(gray); title("Scaled Image (8 levels)"); colorbar; axis image; figure(3); subplot(2,2,1); imagesc(log2(glcm1+1)); title("log2 GLCM [0 1]"); colorbar; subplot(2,2,2); imagesc(log2(glcm2+1)); title("log2 GLCM [1 0]"); colorbar; subplot(2,2,3); imagesc(log2(glcm3+1)); title("log2 GLCM [1 1]"); colorbar; subplot(2,2,4); imagesc(log2(glcm4+1)); title("log2 GLCM [0 1] Symmetric"); colorbar; % Calculate and display GLCMs with different number of levels figure(4); levels = [4, 8, 16, 32]; for i = 1:length(levels) [glcm, ~] = graycomatrix(P, "NumLevels", levels(i)); subplot(2,2,i); imagesc(glcm); title(sprintf("GLCM with %d levels", levels(i))); colorbar; end disp("The figures show how different parameters affect the GLCM computation."); disp("Figure 1: Original phantom image"); disp("Figure 2: Scaled image with 8 gray levels"); disp("Figure 3: GLCMs with different offsets and symmetry"); disp("Figure 4: GLCMs with different numbers of gray levels");
Produces the following output
Demonstrating graycomatrix with the Shepp-Logan phantom: graycomatrix has been re-written. If you want the previous version, please run graycomatrix_old The figures show how different parameters affect the GLCM computation. Figure 1: Original phantom image Figure 2: Scaled image with 8 gray levels Figure 3: GLCMs with different offsets and symmetry Figure 4: GLCMs with different numbers of gray levels
and the following figures
Figure 1 | Figure 2 | Figure 3 | Figure 4 |
---|---|---|---|
![]() |
![]() |
![]() |
![]() |
Package: image