numpy的有效求和面积表计算 [英] Efficient summed Area Table Calculation with Numpy

查看:169
本文介绍了numpy的有效求和面积表计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用python和python计算特征计数矩阵的总面积表.麻木.目前,我正在使用以下代码:

I'm trying to calculate a summed area table of a feature count matrix using python and numpy. Currently I'm using the following code:

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

上面的代码大约需要35秒才能在3200 x 1400阵列上执行计算.有什么方法可以使用Numpy技巧来加快计算速度吗?我意识到基本的速度问题在于嵌套的python循环,但我不知道如何避免它们.

The above code takes about 35 seconds to perform the calculation on a 3200 x 1400 array. Is there any way to use Numpy trick to speed up the computation? I realize the fundamental speed problem lies in the nested python loops, but I don't know how to avoid them.

推荐答案

有一个用于累积总和的NumPy函数cumsum.两次应用将产生所需的表:

There's a NumPy function cumsum for cumulative sums. Applying it twice yields the desired table:

import numpy as np

A = np.random.randint(0, 10, (3, 4))

print A
print A.cumsum(axis=0).cumsum(axis=1)

输出:

[[7 4 7 2]
 [6 9 9 5]
 [6 6 7 6]]
[[ 7 11 18 20]
 [13 26 42 49]
 [19 38 61 74]]

性能分析:( https://stackoverflow.com/a/25351344/3419103)

import numpy as np
import time

A = np.random.randint(0, 10, (3200, 1400))

t = time.time()
S = A.cumsum(axis=0).cumsum(axis=1)
print np.round_(time.time() - t, 3), 'sec elapsed'

输出:

0.15 sec elapsed

这篇关于numpy的有效求和面积表计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆