Texture Feature Extraction –GLCM
%% ===============================
% GLCM Texture Feature Extraction
% ===============================
clc; clear; close all;
%% Step 1: Read and preprocess image
I = imread('[Link]'); % Replace with your image
figure; imshow(I); title('Original Image');
% Convert to grayscale if RGB
if size(I,3) == 3
Igray = rgb2gray(I);
else
Igray = I;
end
%% Step 2: Quantize image (optional)
% Reduce gray levels for smaller GLCM
Igray = uint8(floor(double(Igray)/16)); % 16 gray levels
%% Step 3: Compute GLCMs in multiple directions
offsets = [0 1; -1 1; -1 0; -1 -1]; % 0°, 45°, 90°, 135°
glcms = graycomatrix(Igray, 'Offset', offsets);
%% Step 4: Extract texture features for each GLCM
numGLCMs = size(glcms,3);
contrast = zeros(1,numGLCMs);
energy = zeros(1,numGLCMs);
homogeneity = zeros(1,numGLCMs);
correlation = zeros(1,numGLCMs);
for k = 1:numGLCMs
stats = graycoprops(glcms(:,:,k),
{'Contrast','Energy','Homogeneity','Correlation'});
contrast(k) = [Link];
energy(k) = [Link];
homogeneity(k) = [Link];
correlation(k) = [Link];
end
%% Step 5: Display features
angles = [0 45 90 135];
fprintf('GLCM Texture Features:\n');
fprintf('Angle\tContrast\tEnergy\tHomogeneity\tCorrelation\n');
for k = 1:numGLCMs
fprintf('%d\t%.4f\t\t%.4f\t%.4f\t\t%.4f\n', angles(k), contrast(k),
energy(k), homogeneity(k), correlation(k));
end
%% Step 6: Optional - Average features across angles
avgContrast = mean(contrast);
avgEnergy = mean(energy);
avgHomogeneity = mean(homogeneity);
avgCorrelation = mean(correlation);
fprintf('\nAverage Texture Features:\n');
fprintf('Contrast: %.4f\nEnergy: %.4f\nHomogeneity: %.4f\nCorrelation: %.4f\
n', ...
avgContrast, avgEnergy, avgHomogeneity, avgCorrelation);