python实现GLCM图像特征提取
时间: 2023-07-11 20:04:52 浏览: 73
GLCM(Gray-Level Co-occurrence Matrix)是一种用于图像特征提取的方法,可以用于描述图像中像素灰度值之间的关系。下面是python实现GLCM图像特征提取的步骤:
1. 将图像转换为灰度图像。
2. 根据需要选择距离和角度。常用的距离为1和2,常用的角度为0度、45度、90度和135度。
3. 根据选择的距离和角度计算GLCM矩阵。
4. 根据GLCM矩阵计算出各种统计量,如能量、对比度、均值等。
下面是python代码示例:
```python
import numpy as np
from skimage.feature import greycomatrix, greycoprops
from skimage import io, color
# 读取图像并转换为灰度图像
img = io.imread('image.jpg')
img_gray = color.rgb2gray(img)
# 计算GLCM矩阵
d = 1 # 距离
angles = [0, np.pi/4, np.pi/2, 3*np.pi/4] # 角度
glcm = greycomatrix(img_gray, distances=[d], angles=angles, levels=256, symmetric=True, normed=True)
# 计算GLCM统计量
contrast = greycoprops(glcm, 'contrast')
dissimilarity = greycoprops(glcm, 'dissimilarity')
homogeneity = greycoprops(glcm, 'homogeneity')
energy = greycoprops(glcm, 'energy')
correlation = greycoprops(glcm, 'correlation')
ASM = greycoprops(glcm, 'ASM')
```
其中,`img`为原始图像,`img_gray`为灰度图像,`d`为距离,`angles`为角度,`levels`为灰度级数,`symmetric`为是否对称,`normed`为是否进行归一化。`glcm`为计算得到的GLCM矩阵,`contrast`、`dissimilarity`、`homogeneity`、`energy`、`correlation`和`ASM`为计算得到的GLCM统计量。