如何在python中使用scikit-image greycomatrix()函数?

问题描述 投票:1回答:1

我正在尝试从图像中计算灰度共生矩阵以进行特征提取。我正在使用greycomatrix执行任务,但由于我收到以下错误,似乎有一些我不了解的过程:

ValueError:缓冲区源数组是只读的

(完整的痕迹可以在下面找到)

所以这就是我所做的:

使用8个量化级别将(PIL)图像转换为灰度:

greyImg = img.convert('L', colors=8)

然后计算glcm矩阵:

glcm = greycomatrix(greyImg, distances=[1], angles=[0, np.pi/4, np.pi/2], 
                    symmetric=True, normed=True)

这导致了一个相当神秘的错误:

glcm = greycomatrix(img,distance = [1],angles = [0,np.pi / 4,np.pi / 2],levels = 256,symmetric = True,normed = True)

_glcm_loop(图像,距离,角度,等级,P)

在skimage.feature._texture._glcm_loop中输入第18行的文件“skimage / feature / _texture.pyx”

在View.MemoryView.memoryview_cwrapper中输入“stringsource”,第654行

View.MemoryView.memoryview._cinit__中的文件“stringsource”,第349行.ValueError:缓冲区源数组是只读的

我一直在尝试与参数混合,但我似乎无法弄清楚,为什么会发生这种情况。计算glcm矩阵的正确方法是什么?

Update

问题在于灰度转换。需要进行以下更改:

import numpy as np

greyImg = np.array(img.convert('L', colors=8))
python numpy python-imaging-library scikit-image glcm
1个回答
0
投票

函数greycomatrix期望NumPy ndarray而不是PIL Image对象。你需要像这样转换greyImg

import numpy as np

greyImg = np.asarray(img.convert('L', colors=8))
© www.soinside.com 2019 - 2024. All rights reserved.