MATLAB GrayComMatrix和graycoprops的Python实现

2024-09-27 00:23:15 发布

您现在位置:Python中文网/ 问答频道 /正文

image of cat used

来源:https://www.petfinder.com/cats/cat-grooming/

我尝试在Python中接收与MATLAB中相同的函数graycomatrix和graycrops的结果。但是结果不同,我无法编写代码来重复MATLAB的结果。在

我需要GLCM功能,如对比度、相关性、能量和同质性。在

任何建议都非常感谢。在

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);

上面的代码返回:

^{pr2}$

现在我希望在Python中得到完全相同的结果。我使用Python代码:

# 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)])

但我得到的结果是:

[1.7607, 0.8844, 0.0429, 0.7085]

另一个例子。原始图像的不同结果。原因是MATLAB默认处理图像,而Python不处理。如何在Python中得到与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)

Tags: 代码ioimportimgnpmeanfeaturecat
1条回答
网友
1楼 · 发布于 2024-09-27 00:23:15

使用Matlab和Python计算的GLCM特征是不同的,因为用Matlab代码对原始图像进行预处理(即转换为灰度、缩放和重新量化)的结果与Python代码(即arrayS)的结果不同。下面的片段说明了这一点:

Matlab

>> offset_GLCM = [0 1; -1 1; -1 0; -1 -1];
>> offset = [1*offset_GLCM ; 2*offset_GLCM; 3*offset_GLCM];
>> img = rgb2gray(imread('cat.png'));
>> [Grauwertmatrix, S] = graycomatrix(img,'NumLevels', 12, 'GrayLimits', [], 'Offset',offset);

>> S(100:105, 100:105)

ans =

     4     5     7     8     8     6
     5     5     6     7     8     7
     4     4     4     6     7     7
     4     4     5     6     8     8
     5     6     6     7     8     8
     4     5     6     6     7     8

Python

^{pr2}$

最后,我建议您使用无损图像格式(如BMP或PNG),以避免由于JPG解压缩而产生的潜在差异。在

解决方法

为了通过Matlab和Python获得相同的结果,您应该在两种情况下使用相同的预处理图像。例如,可以这样生成这样的图像:

from skimage import io
from sklearn import preprocessing
img = io.imread("cat.png", as_grey=True)
S = preprocessing.MinMaxScaler((0,11)).fit_transform(img).astype(int)
io.imsave('cat_preprocessed.png', S)

请注意,resulting image的对比度非常低,因为强度值在0到11之间。在

为了让Matlab的graycomatrix正确缩放此图像,您需要传递'NumLevels',12和{}(而不是[0,11]):

>> img = imread('cat_preprocessed.png');
>> [Grauwertmatrix, S] = graycomatrix(img,'NumLevels',12,'GrayLimits',[0,12],'Offset',[0,1]);
>> Grauwertmatrix(1:5,1:5)

ans =

       21258        3250         452         186          91
        3208       20119        5268         827         267
         532        5242       40541        8508        1203
         208         848        8616       26436        6324
         102         285        1192        6216       14101

>> graycoprops(Grauwertmatrix,{'contrast'})

ans = 

    Contrast: 0.9681

Python返回相同的结果:

In [86]: from skimage import io, feature

In [87]: img = io.imread("cat_preprocessed.png")

In [88]: Grauwertmatrix = feature.greycomatrix(img, distances=[1], angles=[0], levels=12, symmetric=False, normed=False)

In [89]: Grauwertmatrix[0:5, 0:5, 0, 0]
Out[89]:
array([[21258,  3250,   452,   186,    91],
       [ 3208, 20119,  5268,   827,   267],
       [  532,  5242, 40541,  8508,  1203],
       [  208,   848,  8616, 26436,  6324],
       [  102,   285,  1192,  6216, 14101]], dtype=uint32)

In [90]: feature.greycoprops(Grauwertmatrix, 'contrast')
Out[90]: array([[ 0.96812745]])

编辑

针对您的评论,我认为不可能通过Matlab和Python从原始RGB图像中获得相同的结果,因为灰度转换的执行方式不同。文件中明确说明了这一点:

Matlab的^{}

rgb2gray converts RGB values to grayscale values by forming a weighted sum of the R, G, and B components:

    0.2989 * R + 0.5870 * G + 0.1140 * B

scikit图像的^{}

The weights used in this conversion are calibrated for contemporary CRT phosphors:

Y = 0.2125 R + 0.7154 G + 0.0721 B

相关问题 更多 >

    热门问题