python中如何计算图片中不同区域的灰度共生矩阵并显示,获得纹理特征统计量并分析它们的大小和代表的意义
时间: 2024-05-11 11:19:41 浏览: 14
在Python中,可以使用scikit-image库中的greycomatrix函数计算灰度共生矩阵,并使用greycoprops函数获取不同纹理特征的统计量。
下面是一个示例代码,展示了如何计算灰度共生矩阵并显示、获取不同纹理特征的统计量:
```python
import numpy as np
import matplotlib.pyplot as plt
from skimage import io, color
from skimage.feature import greycomatrix, greycoprops
# 读取图片并转为灰度图像
img = io.imread('image.jpg')
gray = color.rgb2gray(img)
# 计算灰度共生矩阵
distances = [1, 2, 3]
angles = [0, np.pi/4, np.pi/2, 3*np.pi/4]
glcm = greycomatrix(gray, distances=distances, angles=angles, levels=256, symmetric=True, normed=True)
# 显示灰度共生矩阵
fig, axs = plt.subplots(nrows=2, ncols=2, figsize=(8, 8))
axs = axs.ravel()
titles = ['0', 'pi/4', 'pi/2', '3*pi/4']
for i, angle in enumerate(angles):
axs[i].imshow(glcm[:,:,i,0], cmap=plt.cm.gray, vmin=0, vmax=1)
axs[i].set_title(titles[i])
axs[i].axis('off')
plt.show()
# 获取不同纹理特征的统计量
contrast = greycoprops(glcm, 'contrast')
dissimilarity = greycoprops(glcm, 'dissimilarity')
homogeneity = greycoprops(glcm, 'homogeneity')
energy = greycoprops(glcm, 'energy')
correlation = greycoprops(glcm, 'correlation')
ASM = greycoprops(glcm, 'ASM')
# 分析纹理特征统计量的大小和代表的意义
print("Contrast: ", contrast)
print("Dissimilarity: ", dissimilarity)
print("Homogeneity: ", homogeneity)
print("Energy: ", energy)
print("Correlation: ", correlation)
print("ASM: ", ASM)
```
在上述代码中,我们假设图片名为image.jpg,首先使用skimage库中的io.imread函数读取图片并转为灰度图像;接着,使用greycomatrix函数计算灰度共生矩阵,并使用matplotlib库中的imshow函数展示灰度共生矩阵;最后,使用greycoprops函数获取不同纹理特征的统计量,并输出分析结果。
需要注意的是,计算灰度共生矩阵时,需要指定距离和角度参数;获取不同纹理特征的统计量时,需要指定要获取的纹理特征名称。根据计算结果,我们可以分析每个纹理特征的大小和代表的意义,以便进一步应用到图像分析中。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)