Skip to main content
 首页 » 编程设计

python之GLCM 图像中的黑色空间

2024年10月01日7exmyth

我正在尝试使用 Haralick 描述的 GLCM(能量、均匀性等)为我拥有的一系列 4 波段(R、G、B、NIR)航拍照片计算一些纹理测量值。我已经在一个子集上尝试过这个,但最终得到的图像大部分是空白的。我目前的理解是它与灰度和 levels 参数有关,但我无法弄清楚。

我的日期非常大(几 GB),所以我试图通过使用模块 RIOS 来提高效率(读取图像作为 400×400×nbands numpy 数组,处理数据并写出到输出图像).

我的输入场景可以查到here (200 MB)。

我的输出图像看起来像(这可能很难看清,因为黑色像素非常小):

我的代码是:

#Set up input and output filenames 
infiles = applier.FilenameAssociations() 
infiles.image1 = "infile.tif" 
 
outfiles = applier.FilenameAssociations() 
outfiles.outimage = "outfile.tif" 
 
controls = applier.ApplierControls() 
controls.progress = cuiprogress.CUIProgressBar() 
# I ultimately want to use a window here 
# which RIOS easily allows you to set up. 
# For a 3x3 the overlap is 1, 5x5 overlap is 2 etc 
#controls.setOverlap(4) 
 
def doFilter(info, infiles, outfiles, controls=controls): 
    grayImg = img_as_ubyte(color.rgb2gray(infiles.image1[3])) 
    g = greycomatrix(grayImg, distances=[1], angles=[0, np.pi/4, np.pi/2, 3*np.pi/4], symmetric=True, normed=True) 
    filtered = greycoprops(g, 'energy') 
    # create 3d image from 2d array 
    outfiles.outimage = numpy.expand_dims(filtered, axis=0) 
 
 
applier.apply(doFilter, infiles, outfiles, controls=controls) 

显然这里有问题,因为我的输出与我预期的不一样。我猜这与“级别”参数有关。我在这里得到了解释:Black line in GLCM result这很好地解释了参数,但我无法改进我的结果。

谁能向我解释为什么我的结果如图所示,我该如何补救?

请您参考如下方法:

下面的代码计算与 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 的样子:

将参数 normed 设置为 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)