灰度共生矩阵(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的灰度共生矩阵
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)
特征值计算
![在这里插入图片描述](https://img-blog.csdnimg.cn/b1c7b0f549a547f192bb68177a1f483d.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBASVQtRGFuWkY=,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center)
contrast特征值
contrast = greycoprops(result,'contrast')
contrast
array([[0.58333333, 1.77777778, 1. , 0.44444444]])
dissimilarity特征值
dissimilarity = greycoprops(result,'dissimilarity')
dissimilarity
array([[0.41666667, 1.11111111, 0.66666667, 0.44444444]])
homogeneity特征值
homogeneity = greycoprops(result,'homogeneity')
homogeneity
array([[0.80833333, 0.51111111, 0.7 , 0.77777778]])
ASM特征值
ASM = greycoprops(result,'ASM')
ASM
array([[0.16666667, 0.20987654, 0.18055556, 0.18518519]])
energy特征值
energy = greycoprops(result,'energy')
energy
array([[0.40824829, 0.45812285, 0.42491829, 0.43033148]])
correlation特征值
correlation = greycoprops(result,'correlation')
correlation
array([[0.79698847, 0.6435959 , 0.70116959, 0.8104432 ]])
graycomatrix的API链接