代码如下:
"""
提取glcm纹理特征
"""
import numpy as np
import cv2
import os
import pandas as pd
import matplotlib.pyplot as plt
from skimage.feature import greycomatrix, greycoprops
def get_inputs(s): # s为图像路径
input = cv2.imread(s, cv2.IMREAD_GRAYSCALE) # 读取图像,灰度模式
# 得到共生矩阵,参数:图像矩阵,距离,方向,灰度级别,是否对称,是否标准化
glcm = greycomatrix(
input, [
2, 8, 16], [
0, np.pi / 4, np.pi / 2, np.pi * 3 / 4], 256, symmetric=True, normed=True)
print(glcm)
#得到共生矩阵统计值,http://tonysyu.github.io/scikit-image/api/skimage.feature.html#skimage.feature.greycoprops
for prop in {'contrast', 'dissimilarity',
'homogeneity', 'energy', 'correlation', 'ASM'}:
temp = greycoprops(glcm, prop)
# temp=np.array(temp).reshape(-1)
print(prop, temp)
# plt.imshow(input,cmap="gray")
# plt.show()
if __name__ == '__main__':
get_inputs(r"E:\Images\54542.jpg")
共生矩阵(代码中glcm)为四维,前两维表示行列,后两维分别表示距离和角度。输出的每种特征(代码中temp)行表示距离,列表示角度。