您可以使用cumsum
加快trap
。计算累积和(@Benjamin提出的一维积分图像)
>>> import numpy as np
>>> csdata = np.cumsum(data, axis=1)
与离散长度积分
>>> npoints = 6
>>> result = np.zeros_like(data)
>>> result[:-npoints, :] = csdata[npoints:, :] - csdata[:-npoints, :]
The result
是一个向量化cumdata[i+npoints, j] - cumdata[i, j]
对于每一个i, j
在图像中。最后将用零填充npoints
行。你可以reflect
边界与np.pad
如果你想阻止这种情况。
对于非离散间隔,您可以使用插值:
>>> from scipy.interpolate import interp2d
>>> C = 0.5 # to interpolate every npoints+C pixels
>>> y, x = np.mgrid[:data.shape[0], :data.shape[1]]
>>> ynew, xnew = np.mgrid[C:data.shape[0]+C, :data.shape[1]]
>>> f = interp2d(x, y, csdata)
>>> csnew = f(xnew, ynew)
以上移动了常规网格C
中的像素y
方向,并对累积数据进行插值csdata
在这些点上(实际上,它对每个像素进行向量插值)。
那么积分为npoints+C
长度可以得到为
>>> npoints = 6
>>> result = np.zeros_like(data)
>>> result[:-npoints, :] = csnew[npoints:, :] - csdata[:-npoints, :]
请注意,现在的上限是csnew
(移位 6 实际上得到 6.5 元素),使其在实践中整合每 6.5 点。
然后您可以找到最大值点:
>>> idx = np.argmax(result.ravel()) # ravel to get the 1D maximum point
>>> maxy, maxx = np.unravel_index(idx, data.shape) # get 2D coordinates of idx