我正在处理 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)