【发布时间】:2016-12-21 17:04:15
【问题描述】:
我正在处理 6641x2720 图像以使用移动的 GLCM(灰度共现矩阵)窗口生成其特征图像(Haralick 特征,如对比度、二阶矩等)。但它需要永远运行。 代码运行良好,因为我已经在较小的图像上对其进行了测试。但是,我需要让它运行得更快。将尺寸减小到 25% (1661x680),运行需要 30 分钟。我怎样才能让它运行得更快?代码如下:
from skimage.feature import greycomatrix, greycoprops
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
import time
start_time = time.time()
img = Image.open('/home/student/python/test50.jpg').convert('L')
y=np.asarray(img, dtype=np.uint8)
#plt.imshow(y, cmap = plt.get_cmap('gray'), vmin = 0, vmax = 255)
contrast = np.zeros((y.shape[0], y.shape[1]), dtype = float)
for i in range(0,y.shape[0]):
for j in range(0,y.shape[1]):
if i < 2 or i > (y.shape[0]-3) or j < 2 or j > (y.shape[1]-3):
continue
else:
s = y[(i-2):(i+3), (j-2):(j+3)]
glcm = greycomatrix(s, [1], [0], symmetric = True, normed = True )
contrast[i,j] = greycoprops(glcm, 'contrast')
print("--- %s seconds ---" % (time.time() - start_time))
plt.imshow(contrast, cmap = plt.get_cmap('gray'), vmin = 0, vmax = 255)
【问题讨论】:
-
也许您应该尝试将您的数据分箱为 4 位 [0-16],而不是使用所有 256 个灰度值。查看scikit-image.org/docs/dev/api/…中的选项级别。
-
减少 bin 的数量不会改变计算时间。
-
greycomatrix在其实现中有 4 个嵌套的for循环,这意味着这段代码可能有大约 O(n**6) 的执行时间。我不确定是否可以在不重写greycomatrix和greycoprops函数的情况下减少此特定函数。
标签: python image-processing scikit-image glcm