我正在尝试在python中开发一种快速算法,以查找图像中的峰值,然后找到这些峰值的质心。我使用scipy.ndimage.label和ndimage.find_objects编写了以下代码来查找对象。这似乎是代码中的瓶颈,在500x500图像中定位20个对象大约需要7毫秒。我想将此图像放大到更大(2000x2000)的图像,但是时间增加到将近100毫秒。因此,我想知道是否有更快的选择。
这是我到目前为止的代码,可以运行,但是很慢。首先,我使用一些高斯峰来模拟我的数据。这部分很慢,但是在实践中我将使用真实数据,因此我不太在意加快该部分的速度。我希望能够很快找到山峰。
import time
import numpy as np
import matplotlib.pyplot as plt
import scipy.ndimage
import matplotlib.patches
plt.figure(figsize=(10,10))
ax1 = plt.subplot(221)
ax2 = plt.subplot(222)
ax3 = plt.subplot(223)
ax4 = plt.subplot(224)
size = 500 #width and height of image in pixels
peak_height = 100 # define the height of the peaks
num_peaks = 20
noise_level = 50
threshold = 60
np.random.seed(3)
#set up a sim