Open In App

Grey Level Co-occurrence Matrix in MATLAB

Last Updated : 28 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The use of texture to identify regions of interest in an image is a crucial characteristic. One of Haralick et al.’s earliest approaches to texture feature extraction was Grey Level Co-occurrence Matrices (GLCM)  in the year 1973. Since then, it has been used extensively in a number of texture analysis applications and continues to be a significant technique for feature extraction in texture analysis. Haralick used the GLCMs to extract fourteen features to describe texture. In CBIR applications, Dacheng et al utilized 3D co-occurrence matrices. For the purpose of matching and recognizing objects, Kovalev and Petrov made use of special multidimensional co-occurrence matrices. Clustering methods make use of multi-dimensional texture analysis, which was first demonstrated. Co-occurrence matrices and their extraction of additional features from n-dimensional Euclidean spaces are the aims of this work, which extends the concept to these spaces. In CBIR applications, the newly defined features are found to be useful.

Grey Level Co-occurrence Matrix:

The spatial relationship between neighboring or adjacent pixels is provided by GLCM. We will gauge some texture features from this GLCM matrix. The second-order statistical texture analysis method is called GLCM. It measures the frequency with which pixels in a given direction and distance d are present in an image and examines the spatial relationship between them. A gray-level spatial dependence matrix is another name for a gray-level co-occurrence matrix. By figuring out how frequently two pixels with the same grayscale intensity value—i and j—occur horizontally next to one another, the gray matrix generates the GLCM.

Syntax:

glcms = graycomatrix(I)

glcms = graycomatrix(I,Name,Value)

[glcms,SI] = graycomatrix(___)

  • glcms- From image I, gray matrix(I) generates a gray-level co-occurrence matrix (GLCM), which is also known as a gray-level spatial dependence matrix.
  • glcms- Depending on the values of the optional name-value pair arguments, the gray matrix(I, Name, Value) returns one or more gray-level co-occurrence matrices.
  • [glcms,SI]- The scaled image, SI, used to calculate the gray-level co-occurrence matrix is returned by gray matrix(___).

Calculate GLCM Matrix:

Use the graycomatrix function to build a GLCM. The function determines how frequently a pixel with the intensity (gray-level) value I occur in a particular spatial relationship to a pixel with the value j to produce a gray-level co-occurrence matrix (GLCM). The pixel of interest and the pixel immediately to its right (horizontally adjacent) are the default definitions of the spatial relationship, but you can specify other spatial relationships between the two pixels. The number of times the input image’s pixel with the value I occurred in the specified spatial relationship to a pixel with value j is simply added for each element (i,j) in the final glcm.

The quantity of dim levels in the picture decides the size of the GLCM. Graycomatrix uses scaling to reduce the number of intensity values in an image to eight by default. However, you can control this scaling of gray levels by utilizing the NumLevels and GrayLimits parameters.

The spatial distribution of the grey levels in the texture image can be learned from the gray-level co-occurrence matrix. For instance, the texture is coarse with respect to the specified offset if the majority of the entries in the GLCM are concentrated along the diagonal. The GLCM can also be used to calculate a number of statistical measures.

Example 1:

Matlab




clear all
% GLCM MATRIX
  
X=[6 7 8 7 8; 5 6 7 8 9; 7 7 4 4 7; 3 4 3 4 3; 2 9 2 9 9];
% Amount of steps between two pixels
Displacement = 1; 
  
% Amount of Quantization Level
NumQuantLevels = 9; 
glcm = zeros([NumQuantLevels,NumQuantLevels]); 
  
% PRREALOCATING THE GLCM MATRIX
  
for i = 1:size(X,1)
    for j = 1 :size(X,2)-1
        glcm(X(i,j),X(i,j+1))=glcm(X(i,j),X(i,j+1))+1; 
        % INCREASE BY 1 % 
    end
  
end


Output:

OUTPUT

Explanation:

It is calculated how many times a pixel and its neighboring pixel appear. The offset is 1, but it can be increased according to the need. The movement of the pixels here is going in the right direction. The quantization level is set to 9 because each pixel in the matrix X has a maximum value of 9. In the preceding example, for instance, pixel 6 and its neighbor 7 occur six times in matrix X, and the GLCM matrix updates them at position (6,7) as 2.

Example 2:

Matlab




clear clc
% GLCM MATRIX
I = imread('apple1.png');
imshow(I)
[glcm,SI] = graycomatrix(I,'Offset',[2 0],'Symmetric',true);
glcm; 
imshow(rescale(SI))


Output:

Input image

 


 

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads