我正在使用移動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