灰度共生矩阵(skimage库中的graycomatrix,greycoprops)

import numpy as np
from skimage.feature import greycomatrix, greycoprops

使用灰度级为4,矩阵维度为4x4的矩阵

image = np.array([[0, 0, 1, 1],
               [0, 0, 1, 1],
               [0, 2, 2, 2],
               [2, 2, 3, 3]])

计算image的灰度共生矩阵

# image:输入图像(灰度级为4的4X4的二维矩阵)
# [1]:距离为1
# [0, np.pi/4, np.pi/2, 3*np.pi/4]:四个方向,0度,45度,90度,135度
# level =4:灰度级为4,这里的灰度级要和输入的image的灰度级相对应
result = greycomatrix(image, [1], [0, np.pi/4, np.pi/2, 3*np.pi/4], levels=4)

0度方向,距离为1的灰度共生矩阵

result[:,:,0,0]
array([[2, 2, 1, 0],
       [0, 2, 0, 0],
       [0, 0, 3, 1],
       [0, 0, 0, 1]], dtype=uint32)

45度方向,距离为1的灰度共生矩阵

result[:,:,0,1]
array([[1, 1, 3, 0],
       [0, 1, 1, 0],
       [0, 0, 0, 2],
       [0, 0, 0, 0]], dtype=uint32)

90度方向,距离为1的灰度共生矩阵

result[:,:,0,2]
array([[3, 0, 2, 0],
       [0, 2, 2, 0],
       [0, 0, 1, 2],
       [0, 0, 0, 0]], dtype=uint32)

135度方向,距离为1的灰度共生矩阵

result[:,:,0,3]
array([[2, 0, 0, 0],
       [1, 1, 2, 0],
       [0, 0, 2, 1],
       [0, 0, 0, 0]], dtype=uint32)

特征值计算

在这里插入图片描述

contrast特征值

# 以下输出的结果为4个方向上contrast值
contrast = greycoprops(result,'contrast')
contrast
array([[0.58333333, 1.77777778, 1.        , 0.44444444]])

dissimilarity特征值

# 以下输出的结果为4个方向上dissimilarity值
dissimilarity = greycoprops(result,'dissimilarity')
dissimilarity
array([[0.41666667, 1.11111111, 0.66666667, 0.44444444]])

homogeneity特征值

# 以下输出的结果为4个方向上homogeneity值
homogeneity = greycoprops(result,'homogeneity')
homogeneity
array([[0.80833333, 0.51111111, 0.7       , 0.77777778]])

ASM特征值

# 以下输出的结果为4个方向上ASM值
ASM = greycoprops(result,'ASM')
ASM
array([[0.16666667, 0.20987654, 0.18055556, 0.18518519]])

energy特征值

# 以下输出的结果为4个方向上energy值
energy = greycoprops(result,'energy')
energy
array([[0.40824829, 0.45812285, 0.42491829, 0.43033148]])

correlation特征值

# 以下输出的结果为4个方向上correlation值
correlation = greycoprops(result,'correlation')
correlation
array([[0.79698847, 0.6435959 , 0.70116959, 0.8104432 ]])

graycomatrix的API链接

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐