下面的代码计算与 tif 图像的 NIR 波段“向上 1 像素偏移”相对应的 GLCM:
import numpy as np
from skimage import io
from skimage.feature import greycomatrix, greycoprops
x = io.imread('m_2909112_se_15_1_20150826.tif')
nir = x[:, :, 3]
glcm = greycomatrix(nir, [1], [np.pi/2], levels=256, normed=True, symmetric=True)
就是这样nir
looks:
设置参数的效果normed
to True
是计算出的 GLCM 除以其总和,结果是glcm
有相当小的值。这是一个示例:
In [48]: np.set_printoptions(precision=3)
In [49]: glcm[:5, :5, 0, 0]
Out[49]:
array([[ 0.000e+00, 0.000e+00, 0.000e+00, 0.000e+00, 0.000e+00],
[ 0.000e+00, 2.725e-03, 6.940e-05, 3.725e-05, 2.426e-05],
[ 0.000e+00, 6.940e-05, 1.709e-04, 4.103e-05, 2.216e-05],
[ 0.000e+00, 3.725e-05, 4.103e-05, 4.311e-04, 4.222e-05],
[ 0.000e+00, 2.426e-05, 2.216e-05, 4.222e-05, 5.972e-05]])
显示glcm
作为图像,您需要重新缩放它,例如如下所示:
from skimage.exposure import rescale_intensity
scaled = rescale_intensity(glcm[:,:,0,0])
io.imshow(scaled)