python时间序列峰值检测_Python-二维阵列中的峰值检测

小编典典

我使用局部最大滤波器检测到峰值。这是第一个4个爪子的数据集的结果:

我还在9个爪子的第二个数据集上运行了它,效果也很好。

这是你的操作方式:

import numpy as np

from scipy.ndimage.filters import maximum_filter

from scipy.ndimage.morphology import generate_binary_structure, binary_erosion

import matplotlib.pyplot as pp

#for some reason I had to reshape. Numpy ignored the shape header.

paws_data = np.loadtxt("paws.txt").reshape(4,11,14)

#getting a list of images

paws = [p.squeeze() for p in np.vsplit(paws_data,4)]

def detect_peaks(image):

"""

Takes an image and detect the peaks usingthe local maximum filter.

Returns a boolean mask of the peaks (i.e. 1 when

the pixel's value is the neighborhood maximum, 0 otherwise)

"""

# define an 8-connected neighborhood

neighborhood = generate_binary_structure(2,2)

#apply the local maximum filter; all pixel of maximal value

#in their neighborhood are set to 1

local_max = maximum_filter(image, footprint=neighborhood)==image

#local_max is a mask that contains the peaks we are

#looking for, but also the background.

#In order to isolate the peaks we must remove the background from the mask.

#we create the mask of the background

background = (image==0)

#a little technicality: we must erode the background in order to

#successfully subtract it form local_max, otherwise a line will

#appear along the background border (artifact of the local maximum filter)

eroded_background = binary_erosion(background, structure=neighborhood, border_value=1)

#we obtain the final mask, containing only peaks,

#by removing the background from the local_max mask (xor operation)

detected_peaks = local_max ^ eroded_background

return detected_peaks

#applying the detection and plotting results

for i, paw in enumerate(paws):

detected_peaks = detect_peaks(paw)

pp.subplot(4,2,(2*i+1))

pp.imshow(paw)

pp.subplot(4,2,(2*i+2) )

pp.imshow(detected_peaks)

pp.show()

你需要做的就是scipy.ndimage.measurements.label在蒙版上使用以标记所有不同的对象。这样你就可以分别与他们一起玩了。

请注意,该方法效果很好,因为背景不嘈杂。如果是这样,你将在背景中检测到许多其他不需要的峰。另一个重要因素是邻里的大小。如果峰大小发生变化,则需要对其进行调整(应保持大致成比例)。

2020-02-21

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值