gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 20:59:16 52 4
gpt4 key购买 nike

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

ValueError: buffer source array is read-only

(完整的跟踪可以在下面找到)

这就是我所做的:

将 (PIL) 图像转换为具有 8 个量化级别的灰度:

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, distances=[1], angles=[0, np.pi/4, np.pi/2], levels=256, symmetric=True, normed=True)

_glcm_loop(image, distances, angles, levels, P)

File "skimage/feature/_texture.pyx", line 18, in skimage.feature._texture._glcm_loop

File "stringsource", line 654, in View.MemoryView.memoryview_cwrapper

File "stringsource", line 349, in View.MemoryView.memoryview._cinit__ ValueError: buffer source array is read-only

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

更新

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

import numpy as np

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

最佳答案

函数greycomatrix需要一个NumPy ndarray而不是一个PIL Image对象。您需要像这样转换 greyImg:

import numpy as np

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

关于python - 如何在 python 中使用 scikit-image greycomatrix() 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55809188/

52 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com