与skimage的图象纹理

时间:2018-06-13 09:49:05

标签: python image-processing feature-extraction scikit-image glcm

我试图从greycomatrix使用skimage.feature创建的GLCM中获取texture properties。我的输入数据是具有多个波段的图像,我想要每个像素的纹理属性(产生具有维度cols x rows x (properties *bands)的图像),因为它可以使用ENVI实现。但是,对于greycomatrixgreycoprops来说,我对此并不熟悉。这就是我试过的:

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)

不幸的是,这给了我一个1 x 4矩阵每prop,我猜每个角度对于整个图像是一个值,但这不是我想要的。我需要每个像素,例如每个像素的contrast,根据其各自的环境计算。我错过了什么?

1 个答案:

答案 0 :(得分: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个波段(单击here进行下载)。在每个图像像素处,使用大小为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>

Multispectral image

修改

您可以通过NumPy的uint8或scikit-images的img_as_ubyte将您的数据投射到greycomatrix所需的类型。