python函数从数组中找到局部最大值/峰值

问题描述 投票:0回答:1

我正在尝试从给定数组中检测局部最大值。该数组是计算霍夫线变换后获得的正弦曲线。这是图像enter image description here

这里是Numpy文件accumulator.npy。显然,有两个局部最大值。我想知道什么是检测这两个最大值的最佳方法。另外,找到最大值后如何绘制背线?谢谢。

python-3.x numpy scipy hough-transform straight-line-detection
1个回答
0
投票

您可以使用skimage功能peak_local_max()查找峰。还有很多您似乎没有想到的东西,所以我添加了一些高斯平滑法供您尝试。

最后还有一些绘图,使您可以更好地以3D形式显示数据-但您可以忽略它。

#!/usr/bin/env python3

from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt
from scipy import ndimage as ndi
from skimage.feature import peak_local_max
from skimage import img_as_float

# Load data and find maxima
data = np.load('accum.npy')
im = img_as_float(data)
image_max = ndi.maximum_filter(im, size=20, mode='constant')

# Experiment with various smoothing parameters
for sigma in range(4):
    coordinates = peak_local_max(ndi.gaussian_filter(im,sigma=sigma), min_distance=20)
    print(f"Sigma for smoothing: {sigma}, coordinates of peaks:")
    print(coordinates)

# Plotting stuff
sigmaForPlot=0
fig = plt.figure()
ax = plt.axes(projection='3d')
x = np.outer(np.ones(800),np.arange(300))
y = np.outer(np.arange(800), np.ones(300))
ax.plot_surface(x, y,ndi.gaussian_filter(im,sigma=sigmaForPlot),cmap='viridis', edgecolor='none')
ax.set_title('Surface plot')
plt.show()

样本输出

Sigma for smoothing: 1, coordinates of peaks:
[[595 113]
 [589  36]
 [448  80]
 [400 144]
 [351 260]
 [251 166]
 [210 216]]
Sigma for smoothing: 2, coordinates of peaks:
[[589  36]
 [399 144]
 [239 170]
 [210 216]]
Sigma for smoothing: 3, coordinates of peaks:
[[589  36]
 [398 145]
 [210 216]]

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.