我正在使用移动GLCM(灰度级共生矩阵)窗口来生成6641x2720图像以生成其特征图像(像对比度,二次矩等Haralick功能)。但它需要永远运行。 该代码工作正常,因为我已经在较小的图像上进行了测试。但是,我需要让它运行得更快。将尺寸缩小至25%(1661x680)需要30分钟才能运行。我怎样才能让它跑得更快?下面的代码:使用GLCM减少纹理分析中的运行时间[Python]
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灰度值。查看http://scikit-image.org/docs/dev/api/skimage.feature.html#skimage.feature.greycomatrix中的选项级别。 –
减少垃圾箱数量不会改变计算时间。 – FiReTiTi
'greycomatrix'在它的实现中有4个'for'循环,这意味着这个代码可能有大约O(n ** 6)的执行时间。我不确定是否可以在不改写'greycomatrix'和'greycoprops'功能的情况下减少这个特殊功能。 – callyalater