2

如何找到一组二维互相关函数的相关峰值和坐标?给定一个包含一组 2D 互相关函数的 3D ndarray。找到最大(峰值)值及其坐标(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)
4

1 回答 1

1

您需要展平您正在搜索的维度,然后使用unravel_indexandtake_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]]
于 2019-12-02T13:22:05.167 回答