1

I'm trying to get texture properties from a GLCM I created using greycomatrix from skimage.feature. My input data is an image with multiple bands and I want the texture properties for each pixel (resulting in an image with the dimensions cols x rows x (properties *bands)), as it can be achieved using ENVI. But I'm too new to this to come to grips with greycomatrix and greycoprops. This is what I tried:

import numpy as np
from skimage import io
from skimage.feature import greycomatrix, greycoprops

array = io.imread('MYFILE.tif')
array = array.astype(np.int64)
props = ['contrast', 'dissimilarity', 'homogeneity', 'energy', 'correlation', 'ASM']
textures = np.zeros((array.shape[0], array.shape[1], array.shape[2] * len(props)), np.float32)
angles = [0, np.pi / 4, np.pi / 2, 3 * np.pi / 4]
bands = array.shape[2]
for b in range(bands):
    glcm = greycomatrix(array[:, :, b], [1], angles, np.nanmax(array) + 1,
                        symmetric=True, normed=True)
    for p, prop in enumerate(props):
        textures[:, :, b] = greycoprops(glcm, prop)

Unfortunately, this gives me a 1 x 4 matrix per prop, which I guess is one value per angle FOR THE WHOLE IMAGE, but this is not what I want. I need it per pixel, like contrast for each single pixel, computed from its respective surroundings. What am I missing?

4

1 回答 1

1

这个片段应该可以完成工作:

import numpy as np
from skimage import io, util
from skimage.feature.texture import greycomatrix, greycoprops

img = io.imread('fourbandimg.tif')

rows, cols, bands = img.shape

radius = 5
side = 2*radius + 1

distances = [1]
angles = [0, np.pi/2]
props = ['contrast', 'dissimilarity', 'homogeneity']
dim = len(distances)*len(angles)*len(props)*bands

padded = np.pad(img, radius, mode='reflect')
windows = [util.view_as_windows(padded[:, :, band].copy(), (side, side))
           for band in range(bands)]
feats = np.zeros(shape=(rows, cols, dim))

for row in range(rows):
    for col in range(cols):
        pixel_feats = []
        for band in range(bands):
            glcm = greycomatrix(windows[band][row, col, :, :],
                                distances=distances,
                                angles=angles)
            pixel_feats.extend([greycoprops(glcm, prop).ravel()
                                for prop in props])
        feats[row, col, :] = np.concatenate(pixel_feats)

示例图像有 128 行、128 列和 4 个波段(单击此处下载)。在每个图像像素处,使用大小为 11 的正方形局部邻域来计算与每个波段的右侧像素和上方像素相对应的灰度矩阵。然后,为这些矩阵计算对比度、相异性同质性。因此,我们有 4 个波段、1 个距离、2 个角度和 3 个属性。因此,对于每个像素,特征向量具有 4 × 1 × 2 × 3 = 24 个分量。

请注意,为了保留行数和列数,图像已使用沿阵列边缘镜像的图像本身进行填充。如果这种方法不符合您的需求,您可以简单地忽略图像的外框。

作为最后的警告,代码可能需要一段时间才能运行。

演示

In [193]: img.shape
Out[193]: (128, 128, 4)

In [194]: feats.shape
Out[194]: (128, 128, 24)

In [195]: feats[64, 64, :]
Out[195]: 
array([  1.51690000e+04,   9.50100000e+03,   1.02300000e+03,
         8.53000000e+02,   1.25203577e+01,   9.38930575e+00,
         2.54300000e+03,   1.47800000e+03,   3.89000000e+02,
         3.10000000e+02,   2.95064854e+01,   3.38267222e+01,
         2.18970000e+04,   1.71690000e+04,   1.21900000e+03,
         1.06700000e+03,   1.09729371e+01,   1.11741654e+01,
         2.54300000e+03,   1.47800000e+03,   3.89000000e+02,
         3.10000000e+02,   2.95064854e+01,   3.38267222e+01])

In [196]: io.imshow(img)
Out[196]: <matplotlib.image.AxesImage at 0x2a74bc728d0>

多光谱图像

编辑

您可以greycomatrix通过 NumPyuint8或 scikit-images将数据转换为所需的类型img_as_ubyte

于 2018-06-15T10:18:03.180 回答