我将尝试使用Python和Numpy计算特征计数矩阵的总面积表。目前,我正在使用以下代码:
上述代码在一个3200 x 1400的数组上执行计算大约需要35秒钟。有没有使用Numpy技巧来加速计算的方法?我意识到基本的速度问题在于嵌套的Python循环,但我不知道如何避免它们。
def summed_area_table(img):
table = np.zeros_like(img).astype(int)
for row in range(img.shape[0]):
for col in range(img.shape[1]):
if (row > 0) and (col > 0):
table[row, col] = (img[row, col] +
table[row, col - 1] +
table[row - 1, col] -
table[row - 1, col - 1])
elif row > 0:
table[row, col] = img[row, col] + table[row - 1, col]
elif col > 0:
table[row, col] = img[row, col] + table[row, col - 1]
else:
table[row, col] = img[row, col]
return table
上述代码在一个3200 x 1400的数组上执行计算大约需要35秒钟。有没有使用Numpy技巧来加速计算的方法?我意识到基本的速度问题在于嵌套的Python循环,但我不知道如何避免它们。