如何求出一组二维互相关函数的相关峰值和坐标?给出了包含一组二维互相关函数的三维射线。找出最大值(峰值)及其坐标(x和y指数)的有效方法是什么?
下面的代码做的工作,但我认为它是低效的。
import numpy as np
import numpy.matlib
ccorr = np.random.rand(7,5,5)
xind = ccorr.argmax(axis=-1)
mccorr = ccorr[np.matlib.repmat(np.arange(0,7)[:,np.newaxis],1,5),np.matlib.repmat(np.arange(0,5)[np.newaxis,:],7,1), xind]
yind = mccorr.argmax(axis=-1)
xind = xind[np.arange(0,7),yind]
values = mccorr[np.arange(0,7),yind]
print("cross-correlation functions (z,y,x)")
print(ccorr)
print("x and y indices of the maximum values")
print(xind,yind)
print("Maximum values")
print(values)发布于 2019-12-02 13:22:05
您需要将搜索的维度扁平化,然后使用unravel_index和take_along_axis分别获取坐标和值。
ccorr = np.random.rand(7,5,5)
cc_rav = ccorr.reshape(ccorr.shape[0], -1)
idx = np.argmax(cc_rav, axis = -1)
indices_2d = np.unravel_index(idx, ccorr.shape[1:])
vals = np.take_along_axis(ccorr, indices = indices_2d, axis = 0)如果您使用的是numpy版本<1.15:
vals = cc_rav[np.arange(ccorr.shape[0]), idx]或者:
vals = ccorr[np.arange(ccorr.shape[0]),
indices_2d[0], indices_2d[1]]https://stackoverflow.com/questions/59139101
复制相似问题