首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用numpy提取glcm矩阵

使用numpy提取glcm矩阵
EN

Stack Overflow用户
提问于 2019-05-23 07:35:59
回答 1查看 1.1K关注 0票数 2

我想用python (numpy)找到GLCM矩阵,我写了这段代码,它从四个角度给了我一个正确的结果,但它非常慢,用demonsion 128x128处理1000张图片需要大约35分钟

代码语言:javascript
复制
def getGLCM(image, distance, direction):

    npPixel = np.array(image) // image as numpy array

    glcm = np.zeros((255, 255), dtype=int)
    if direction == 1:  # direction 90° up ↑
        for i in range(distance, npPixel.shape[0]):
            for j in range(0, npPixel.shape[1]):
                glcm[npPixel[i, j], npPixel[i-distance, j]] += 1
    elif direction == 2:  # direction 45° up-right ↗
        for i in range(distance, npPixel.shape[0]):
            for j in range(0, npPixel.shape[1] - distance):
                glcm[npPixel[i, j], npPixel[i - distance, j + distance]] += 1
    elif direction == 3:  # direction 0° right →
        for i in range(0, npPixel.shape[0]):
            for j in range(0, npPixel.shape[1] - distance):
                glcm[npPixel[i, j], npPixel[i, j + distance]] += 1
    elif direction == 4:  # direction -45° down-right ↘
        for i in range(0, npPixel.shape[0] - distance):
            for j in range(0, npPixel.shape[1] - distance):
                glcm[npPixel[i, j], npPixel[i + distance, j + distance]] += 1

    return glcm

我需要帮助,让这个代码更快,谢谢。

EN

回答 1

Stack Overflow用户

发布于 2019-05-23 22:53:50

您的代码中存在错误。您需要将灰度共生矩阵的初始化更改为glcm = np.zeros((256, 256), dtype=int),否则,如果要处理的图像包含一些亮度级别为255的像素,函数getGLCM将抛出错误。

下面是一个纯NumPy实现,它通过矢量化提高了性能:

代码语言:javascript
复制
def vectorized_glcm(image, distance, direction):

    img = np.array(image)

    glcm = np.zeros((256, 256), dtype=int)
    
    if direction == 1:
        first = img[distance:, :]
        second = img[:-distance, :]
    elif direction == 2:
        first = img[distance:, :-distance]
        second = img[:-distance, distance:]
    elif direction == 3:
        first = img[:, :-distance]
        second = img[:, distance:]
    elif direction == 4:
        first = img[:-distance, :-distance]
        second = img[distance:, distance:]
    
    for i, j in zip(first.ravel(), second.ravel()):
        glcm[i, j] += 1
        
    return glcm

如果您愿意使用其他包,我强烈建议您使用scikit image的greycomatrix。如下所示,这使计算速度提高了两个数量级。

演示

代码语言:javascript
复制
In [93]: from skimage import data

In [94]: from skimage.feature import greycomatrix

In [95]: img = data.camera()

In [96]: a = getGLCM(img, 1, 1)

In [97]: b = vectorized_glcm(img, 1, 1)

In [98]: c = greycomatrix(img, distances=[1], angles=[-np.pi/2], levels=256)

In [99]: np.array_equal(a, b)
Out[99]: True

In [100]: np.array_equal(a, c[:, :, 0, 0])
Out[100]: True

In [101]: %timeit getGLCM(img, 1, 1)
240 ms ± 1.16 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [102]: %timeit vectorized_glcm(img, 1, 1)
203 ms ± 3.11 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [103]: %timeit greycomatrix(img, distances=[1], angles=[-np.pi/2], levels=256)
1.46 ms ± 15.5 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56266276

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档