【问题标题】:extract glcm matrix using numpy使用 numpy 提取 glcm 矩阵
【发布时间】:2019-05-22 23:35:59
【问题描述】:

我想使用 python (numpy) 找到 GLCM 矩阵 我已经编写了这段代码,它从四个角度给了我一个正确的结果,但是速度很慢,处理 1000 张带有恶魔 128x128 的图片大约需要 35 分钟

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

我需要帮助以使此代码更快 谢谢。

【问题讨论】:

    标签: python numpy textures glcm


    【解决方案1】:

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

    这是一个纯 NumPy 实现,通过矢量化提高性能:

    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。如下所示,这将计算速度提高了两个数量级。

    演示

    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)
    

    【讨论】:

    • 这种方式比我的方式好,但相差40ms左右,差别不大,谢谢。我相信你需要初始化firstsecond 变量。我已经在我的项目中使用了 skimages,但是当我得到特征向量并用它来评估我的机器时,我只有 0.29 精度(正确的 29%)来比较两个向量e 我计算 Distancenp.linalg.norm( featuresVector1- featuresVector2)。我的数据集很好,KNN 的精度为 0.78,Color Histograme 的精度为 0.89,我不知道为什么 GLCM 的精度只有 0.29 , 任何想法
    • 我同意减少 15% 的计算时间对于您的应用程序来说是不够的(但无论如何这都是一个显着的改进)。我认为没有必要初始化firstsecond。不过,我的代码有一个错误,因为我没有使用参数distance(感谢@Noureim 的编辑)。最后,如果您提出新问题,您更有可能在此问题上获得帮助。
    猜你喜欢
    • 2013-10-10
    • 2015-09-04
    • 2017-05-09
    • 1970-01-01
    • 2018-02-11
    • 1970-01-01
    • 2015-04-25
    • 1970-01-01
    • 2014-03-05
    相关资源
    最近更新 更多