python - greycomatrix scikit-image python 中的级别参数

标签 python numpy image-processing scikit-image glcm

我正在使用 scikit-image 工具将我的 Matlab 图像处理算法转移到 Python,并且我正在使用 GLCM 计算灰度共生矩阵 ( greycomatrix ) .如果参数 levels 小于强度图像的最大值 (image.max()),我会遇到问题。例如:

import numpy as np
from skimage.feature import greycomatrix
image = np.array([[0, 0, 1, 1],[0, 0, 1, 1],[0, 2, 2, 2],[2, 2, 3, 3]], dtype=np.uint8)
result = greycomatrix(image, distances = [1], angles = [0], levels = 4, symmetric=True)

输出是:

glcm = result[:,:,0,0]

array([[4, 2, 1, 0],
   [2, 4, 0, 0],
   [1, 0, 6, 1],
   [0, 0, 1, 2]], dtype=uint32)

这是正确的,一个 4x4 矩阵。但是如果levels=3,我无法计算GLCM,错误是:

result = greycomatrix(image, distances = [1], angles = [0], levels = 3, symmetric=True)

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.4/site-packages/skimage/feature/texture.py", line 97, in greycomatrix
assert image.max() < levels
AssertionError

当然......我得到了错误,但我应该能够计算出水平小于 image.max() 的 GLCM(3x3 矩阵)。例如,对于:

result = greycomatrix(image, distances = [1], angles = [0], levels = 3, symmetric=True)

我应该得到以下 GLCM(我可以在 Matlab 中完成):

4     3     0
3    10     1
0     1     2

当我处理巨大的图像时,我会降低 GLCM 的级别以减少计算时间。 greycomatrix 有问题还是我想错了? 提前致谢。

最佳答案

也许我迟到了,但希望我的回答将来对其他人有用...

根据 Matlab documentation 'NumLevels' 可以小于 max(image(:)) 因为灰度值被剪裁了:

Grayscale values less than or equal to low are scaled to 1. Grayscale values greater than or equal to high are scaled to 'NumLevels'.

您可以通过 NumPy 的 clip 在 Python 中使用相同的解决方法:

In [11]: import numpy as np

In [12]: from skimage.feature import greycomatrix

In [13]: image = np.array([[0, 0, 1, 1], 
    ...:                   [0, 0, 1, 1], 
    ...:                   [0, 2, 2, 2], 
    ...:                   [2, 2, 3, 3]], dtype=np.uint8)
    ...: 

In [14]: clipped = np.clip(image, a_min=0, a_max=2)

In [15]: clipped
Out[15]: 
array([[0, 0, 1, 1],
       [0, 0, 1, 1],
       [0, 2, 2, 2],
       [2, 2, 2, 2]], dtype=uint8)

In [16]: result = greycomatrix(clipped, 
    ...:                       distances=[1], 
    ...:                       angles=[0], 
    ...:                       levels=3, 
    ...:                       symmetric=True)
    ...: 

In [17]: result[:, :, 0, 0]
Out[17]: 
array([[ 4,  2,  1],
       [ 2,  4,  0],
       [ 1,  0, 10]], dtype=uint32)

如果您希望减小灰度共生矩阵的大小,我建议您重新量化图像而不是裁剪灰度值。这种方法可以通过 NumPy 的 digitize 轻松实现。 :

In [59]: nlevels = 256

In [60]: nbins = 64

In [61]: arr = np.arange(nlevels).reshape((16, 16)).astype(np.uint8).T

In [62]: np.set_printoptions(threshold=300, linewidth=100)

In [63]: arr
Out[63]: 
array([[  0,  16,  32,  48,  64,  80,  96, 112, 128, 144, 160, 176, 192, 208, 224, 240],
       [  1,  17,  33,  49,  65,  81,  97, 113, 129, 145, 161, 177, 193, 209, 225, 241],
       [  2,  18,  34,  50,  66,  82,  98, 114, 130, 146, 162, 178, 194, 210, 226, 242],
       [  3,  19,  35,  51,  67,  83,  99, 115, 131, 147, 163, 179, 195, 211, 227, 243],
       [  4,  20,  36,  52,  68,  84, 100, 116, 132, 148, 164, 180, 196, 212, 228, 244],
       [  5,  21,  37,  53,  69,  85, 101, 117, 133, 149, 165, 181, 197, 213, 229, 245],
       [  6,  22,  38,  54,  70,  86, 102, 118, 134, 150, 166, 182, 198, 214, 230, 246],
       [  7,  23,  39,  55,  71,  87, 103, 119, 135, 151, 167, 183, 199, 215, 231, 247],
       [  8,  24,  40,  56,  72,  88, 104, 120, 136, 152, 168, 184, 200, 216, 232, 248],
       [  9,  25,  41,  57,  73,  89, 105, 121, 137, 153, 169, 185, 201, 217, 233, 249],
       [ 10,  26,  42,  58,  74,  90, 106, 122, 138, 154, 170, 186, 202, 218, 234, 250],
       [ 11,  27,  43,  59,  75,  91, 107, 123, 139, 155, 171, 187, 203, 219, 235, 251],
       [ 12,  28,  44,  60,  76,  92, 108, 124, 140, 156, 172, 188, 204, 220, 236, 252],
       [ 13,  29,  45,  61,  77,  93, 109, 125, 141, 157, 173, 189, 205, 221, 237, 253],
       [ 14,  30,  46,  62,  78,  94, 110, 126, 142, 158, 174, 190, 206, 222, 238, 254],
       [ 15,  31,  47,  63,  79,  95, 111, 127, 143, 159, 175, 191, 207, 223, 239, 255]], dtype=uint8)

In [64]: binned = np.uint8(np.digitize(arr, np.arange(0, nlevels, nbins))) - 1

In [65]: binned
Out[65]: 
array([[0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3],
       [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3],
       [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3],
       [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3],
       [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3],
       [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3],
       [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3],
       [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3],
       [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3],
       [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3],
       [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3],
       [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3],
       [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3],
       [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3],
       [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3],
       [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3]], dtype=uint8)

In [66]: glcm = greycomatrix(binned, 
    ...:                     distances=[1], 
    ...:                     angles=[0], 
    ...:                     levels=binned.max()+1, 
    ...:                     symmetric=True)
    ...: 

In [67]: glcm[:, :, 0, 0]    
Out[67]: 
array([[96, 16,  0,  0],
       [16, 96, 16,  0],
       [ 0, 16, 96, 16],
       [ 0,  0, 16, 96]], dtype=uint32)

关于python - greycomatrix scikit-image python 中的级别参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24893824/

相关文章:

image-processing - 霍夫变换以确定线条及其宽度

Python 无法检查某个值是否在列表中

python - 属性错误 : 'module' object has no attribute 'SMBus'

python - OpenCV 读取具有多个流/轨道的视频文件

python - 将 Numpy 数组拆分为等长的子数组

matlab - 如何在 MATLAB 中将图像分成 block ?

python - 如何使用 python 绘制具有两个斜率的线

python - 将 python 稀疏矩阵导入 MATLAB

python - 获取每月数据库的每日平均值

image-processing - 从图像计算到平面和正方形角的距离