GLCM for texture analysis

START

 

 

  • GLCM (gray level co-occurrence matrix) is mainly useful to perform the texture analysis and find the features from image.
  • As name suggested its work on gray image and try to create sort of 2 d histogram from image.
  • Main application of GLCM are texture analysis, feature extraction and segmentation.

Steps to Calculate GLCM matrix :

  • Lets assume image I which is gray image.
  • Initialize GLCM matrix size 256 x 256  (256 is level of GLCM).
  • Suppose we use zero angle of GLCM means direction of GLCM is horizontal.
  • Suppose distance of GLCM is 1, means we just look horizontally next pixel to current pixel.
  • In image  I(i,j) get the gray value (suppose value of pixel is a =  127 at I(i,j)), and get gray  value I(i,j+1) in case of distance 1 and zero degree of  GLCM (suppose value of pixel is b = 58 at I(i,j+1)).
  •  Go to GLCM matrix co-ordinate (a = 127, b=58) and increment the value by 1.
  • Iterate the full image that will give us GLCM matrix of zero degree for distance 1.
  • According to texture type, GLCM distance can be decided.
road_bricks
Figure. 1 texture image of road and brick
  • For road  because texture changes so rapidly we consider the small distance to calculate GLCM but for bricks, distance of the GLCM matrix should be large.
  • Angle of GLCM should be selected according to  direction of image texture changes, so in brick image we want to consider 2 direction 0 degree and 90 degree.
  • Calculate features from GLCM matrix, there are many features are available to perform the texture analysis like contrast, correlation, energy , and homogeneity etc.
  • We will get the contrast feature from GLCM matrix which are sufficient to say weather texture is rough or smooth.

Lets write basic GLCM code which calculate the zero degree GLCM for 256 level and get contrast feature.

%%%%%%%%%%%%%%%%%%%%%%%%%Code Start here %%%%%%%%%%%%%%%

%%%%%  GLCM function start here %%%%%%%%%%%%%

function GLCM_0 = getGLCM0(img_gray, distance)
%% function calculate the GLCM matrix at zero degree angle and given distance
%% GLCM_0 is GLCM matrix for 0 degree angle.
%% img_gray is input gray image
%% distance is distance of GLCM calculated

%% initialize GLCM matrix

GLCM_0 = zeros(256,256);

[row,col] = size(img_gray);

for i=1:row
for j=1:col-distance

pixel1 = img_gray(i,j)+1;
pixel2 = img_gray(i,j+distance)+1;
GLCM_0(pixel1,pixel2) = GLCM_0(pixel1,pixel2)+1;

end
end

end

%%% GLCM function end here %%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%% Contrast function start here %%%%%%%%%%%%%%%%%

function Contrast = GLCM_contrast(GLCM)

%% function calculate the GLCM contrast for GLCM matrix
%% GLCM is Gray level co-occurance matrix as input.
%% Contasrt is feature of GLCM matrix

%% normalize the GLCM

[row,col] = size(GLCM);

N_factor = sum(sum(GLCM));

GLCM = GLCM./N_factor;

Contrast = 0;

for i = 1:row
for j = 1:col

temp = ((i-j)^2)* GLCM(i,j);
Contrast = Contrast + temp;
end
end

end

%%%% Contrast function end here  %%%%

 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%main GLCM code %%%%%%%%%

%% this main code calls  GLCM and contrast function for given image

clc % clear the screen

clear all % clear all variables

close all % close all MATLAB windows

%% read Image

% set path to read image from system

ImagePath = ‘D:\DSUsers\uidp6927\image_processingCode\frontend-large Brick.jpg’;

img_RGB = imread(ImagePath); % Read image from path

img_gray = rgb2gray(img_RGB); % Convert image to gray

img_gray = imresize(img_gray,[900,1200]);   % re sizing (normalizing the size)

GLCM_0 = getGLCM0(img_gray, 5);     %% get the GLCM matrix for distance 5 

Contrast = GLCM_contrast(GLCM_0);   %% calculate the contrast feature

disp(‘Contrast of function is =’)
Contrast
%% use matlab inbuilt function to verify result

glcm = graycomatrix(img_gray,’NumLevels’,256,’offset’,[0 5]);

stats = graycoprops(GLCM_0,’Contrast’);

disp(‘Contrast of MATLAB is = ‘)
stats.Contrast

%%%%%%%%%%%%%%%%%%%%%%%%Code End here   %%%%%%%%%%%%%%%%% 

Matlab provide the inbuilt function from which we can calculate the GLCM and Contrast and Verify our results.

glcm = graycomatrix(img_gray,’NumLevels’,256,’offset’,[0 5]);

stats = graycoprops(GLCM_0,’Contrast’);

Lest take two input image first is road texture and second is brick image apply the GLCM and get the contrast feature.

free asphalt texture or bitumen road background photo
Figure. 2 Road Texture Image

 

frontend-large Brick
Figure. 3 Brick texture image

 

Result: 

Contrast feature of figure. 2 road texture is  3.9489e+03

Contrast feature of figure.3 Brick texture is 91.0206

As from result we can say that road texture have so much texture because of that its contrast value is so much high if less texture will be present in image contrast value will be low.

So for smooth area in image contrast will be low,  for rough area contrast will be high.

* Note: GLCM value depend on the size of image we are performing the GLCM so size of image should be fix or normalize.

For more  information about GLCM  feature analysis please go through the paper harlick texture features .

 

the_end1happy_learning

Happy Learning

Cheers

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s