您可以使用cumsum
用來加快trap
。計算累積性總和(1維積分圖像通過@Benjamin提議)
>>> import numpy as np
>>> csdata = np.cumsum(data, axis=1)
集成具有離散長度
>>> npoints = 6
>>> result = np.zeros_like(data)
>>> result[:-npoints, :] = csdata[npoints:, :] - csdata[:-npoints, :]
的result
是cumdata[i+npoints, j] - cumdata[i, j]
針對圖像中的每一個i, j
矢量。它將填滿0行最後的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
(在實踐中,它vectorices對每個像素進行插值)。
然後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
可能:https://en.wikipedia.org/wiki/Summed_area_table – Benjamin
你如何定義的積分爲ponint'x'域?它是'[0,npoints]'*開始*在'x'還是'[-s,s]'(用s = npoints/2')*居中*在'x'? –