我正在尝试从给定数组中检测局部最大值。该数组是计算霍夫线变换后得到的正弦曲线。这是图像
这是 numpy 文件accumulator.npy。显然,有两个局部最大值。我想知道检测这两个最大值的最佳方法是什么。另外,找到最大值后如何绘制回线?谢谢。
我正在尝试从给定数组中检测局部最大值。该数组是计算霍夫线变换后得到的正弦曲线。这是图像
这是 numpy 文件accumulator.npy。显然,有两个局部最大值。我想知道检测这两个最大值的最佳方法是什么。另外,找到最大值后如何绘制回线?谢谢。
您可以使用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]]