source: https://www.petfinder.com/cats/cat-grooming/
I am trying to receive in Python the exact same results of the functions graycomatrix and graycoprops as in MATLAB. But the results differ and I am not able to write the code which will repeat the results from MATLAB.
I need GLCM features like contrast, correlation, energy and homogeneity.
Any advice is very appreciated.
Example code in MATLAB:
% GLCM feature extraction
offset_GLCM = [0 1; -1 1; -1 0; -1 -1];
offset = [1*offset_GLCM ; 2*offset_GLCM; 3*offset_GLCM];
img = rgb2gray(imread('cat.jpg'));
Grauwertmatrix = graycomatrix(img,'NumLevels', 12, 'GrayLimits', [], 'Offset',offset);
GrauwertStats = graycoprops(Grauwertmatrix);
GLCMFeatureVector = [mean(GrauwertStats.Contrast) mean(GrauwertStats.Correlation) mean(GrauwertStats.Energy) mean(GrauwertStats.Homogeneity)];
disp(GLCMFeatureVector);
and the code above returns:
1.6212 0.8862 0.0607 0.7546
Now I want to receive exactly the same results in Python. I use Python code:
# GLCM feature extraction
import numpy as np
from skimage import feature, io
from sklearn import preprocessing
img = io.imread("cat.jpg", as_grey=True)
S = preprocessing.MinMaxScaler((0,11)).fit_transform(img).astype(int)
Grauwertmatrix = feature.greycomatrix(S, [1,2,3], [0, np.pi/4, np.pi/2, 3*np.pi/4], levels=12, symmetric=False, normed=True)
ContrastStats = feature.greycoprops(Grauwertmatrix, 'contrast')
CorrelationtStats = feature.greycoprops(Grauwertmatrix, 'correlation')
HomogeneityStats = feature.greycoprops(Grauwertmatrix, 'homogeneity')
ASMStats = feature.greycoprops(Grauwertmatrix, 'ASM')
print([np.mean(ContrastStats), np.mean(CorrelationtStats),\
np.mean(ASMStats), np.mean(HomogeneityStats)])
But I get the result:
[1.7607, 0.8844, 0.0429, 0.7085]
Another example. Different results on original image. Reason is that MATLAB by default processes image and Python does not. How to get in Python same result as in MATLAB?:
MATLAB:
>> img = rgb2gray(imread('cat.png'));
>> [Grauwertmatrix, S] = graycomatrix(img,'NumLevels',12,'GrayLimits',[0,12],'Offset',[0,1]);
>> Grauwertmatrix(1:5,1:5)
ans =
4 7 4 8 0
9 33 22 13 10
5 18 16 10 10
2 16 11 22 13
4 12 11 14 14
Python:
>>> from skimage import io, feature
>>> img = io.imread("cat.png", as_grey=True)
>>> Grauwertmatrix = feature.greycomatrix(img, distances=[1], angles=[0], levels=12, symmetric=False, normed=False)
>>> Grauwertmatrix[0:5, 0:5, 0, 0]
array([[299720, 2, 0, 0, 0],
[ 2, 1, 0, 0, 0],
[ 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0]], dtype=uint32)