paws.txt數據:
# Array shape: (4, 11, 14)
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.40 0.40 0.00 0.00 0.00 0.00
0.00 0.00 0.00 0.00 0.00 0.00 0.70 1.40 2.20 1.80 0.70 0.00 0.00 0.00
0.00 0.00 0.00 0.00 0.00 1.10 4.00 6.50 4.30 1.80 0.70 0.00 0.00 0.00
0.00 0.00 0.00 0.00 0.00 1.40 6.10 7.20 3.20 0.70 0.40 1.10 1.40 0.40
0.00 0.00 0.70 1.40 1.10 0.70 2.20 2.50 1.10 0.40 1.40 6.50 7.20 1.40
0.00 0.40 2.90 7.90 5.40 1.40 0.70 0.40 1.10 1.80 2.90 7.90 8.30 1.80
0.00 0.00 1.80 5.40 3.20 1.80 4.30 3.60 2.90 6.10 7.60 2.50 1.80 0.40
0.00 0.00 0.40 0.70 0.70 2.50 9.00 7.90 3.60 7.90 9.00 2.20 0.00 0.00
0.00 0.00 0.00 0.00 0.00 1.10 4.70 4.00 1.40 2.90 3.60 0.70 0.00 0.00
0.00 0.00 0.00 0.00 0.00 0.40 0.70 0.70 0.40 0.40 0.40 0.00 0.00 0.00
# New slice
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
0.00 0.00 0.00 0.00 0.40 1.10 1.10 0.70 0.40 0.00 0.00 0.00 0.00 0.00
0.00 0.00 0.00 0.00 0.40 2.20 3.60 3.20 2.20 1.10 0.40 0.00 0.00 0.00
0.00 0.40 1.10 0.70 0.40 1.10 2.90 5.40 5.00 2.20 0.40 0.00 0.00 0.00
0.00 1.10 5.80 4.30 1.10 0.40 1.10 2.50 2.20 1.10 1.10 1.80 0.70 0.00
0.00 1.10 5.00 4.30 1.80 1.10 0.40 0.40 0.70 1.80 6.10 8.60 2.90 0.40
0.00 0.00 1.10 1.80 5.40 5.80 2.50 3.20 5.00 3.20 4.70 5.40 1.40 0.00
0.00 0.00 0.00 1.40 7.20 7.60 4.00 8.60 10.10 3.20 1.10 0.70 0.40 0.00
0.00 0.00 0.00 0.40 2.20 2.20 1.80 4.70 6.50 1.80 0.00 0.00 0.00 0.00
0.00 0.00 0.00 0.00 0.00 0.40 0.40 0.70 1.10 0.40 0.00 0.00 0.00 0.00
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
# New slice
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
0.00 0.00 0.00 0.00 0.40 0.40 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
0.00 0.00 0.00 1.10 1.80 1.80 0.70 0.00 0.00 0.00 0.00 0.00 0.00 0.00
0.00 0.00 0.40 2.90 4.70 2.90 0.70 0.00 0.40 0.40 0.00 0.00 0.00 0.00
0.00 0.00 0.00 1.10 2.90 2.90 0.70 0.70 2.20 2.50 0.70 0.00 0.00 0.00
0.00 0.00 0.00 0.00 0.40 0.40 0.40 1.40 6.10 8.30 2.20 0.00 0.00 0.00
0.00 0.40 0.70 0.40 0.40 0.40 0.40 1.40 3.20 2.50 0.70 0.00 0.00 0.00
0.00 0.40 1.10 0.70 1.40 2.50 2.50 4.70 7.90 3.20 0.40 0.00 0.00 0.00
0.00 0.00 0.40 0.40 2.20 5.80 4.00 2.90 8.60 3.60 0.40 0.00 0.00 0.00
0.00 0.00 0.00 0.00 0.70 1.80 1.40 0.70 1.80 0.70 0.00 0.00 0.00 0.00
0.00 0.00 0.00 0.00 0.00 0.40 0.40 0.00 0.00 0.00 0.00 0.00 0.00 0.00
# New slice
0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
0.00 0.00 0.00 0.00 0.00 0.40 0.40 0.40 0.00 0.00 0.00 0.00 0.00 0.00
0.00 0.00 0.00 0.00 0.40 1.40 1.40 1.80 0.70 0.00 0.00 0.00 0.00 0.00
0.00 0.00 0.00 0.00 0.40 1.40 4.00 5.40 2.20 0.40 0.00 0.00 0.00 0.00
0.00 0.00 0.70 1.10 0.40 1.10 3.20 3.60 1.10 0.00 0.00 0.00 0.00 0.00
0.00 0.40 2.90 3.60 1.10 0.40 0.70 0.70 0.40 0.40 0.00 0.00 0.00 0.00
0.00 0.40 2.50 3.20 1.80 0.70 0.40 0.40 0.40 1.40 0.70 0.00 0.00 0.00
0.00 0.00 0.70 3.60 5.80 2.90 1.40 2.20 1.40 1.80 1.10 0.00 0.00 0.00
0.00 0.00 1.10 5.00 6.80 3.20 4.00 6.10 1.80 0.40 0.40 0.00 0.00 0.00
0.00 0.00 0.40 1.10 1.80 1.80 4.30 3.20 0.70 0.00 0.00 0.00 0.00 0.00
0.00 0.00 0.00 0.00 0.00 0.40 0.70 0.40 0.00 0.00 0.00 0.00 0.00 0.00
# New slice
二維peak峯值點檢測:
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()
一維peak峯值點檢測:
from pylab import *
def peakdet(v, thresh):
maxthresh = []
minthresh = []
peaks = []
valleys = []
for x, y in v:
if y > thresh:
maxthresh.append((x, y))
elif y < -thresh:
minthresh.append((x, y))
for x, y in maxthresh:
try:
if (v[x - 1][1] < y) & (v[x + 1][1] < y):
peaks.append((x, y))
except Exception:
pass
for x, y in minthresh:
try:
if (v[x - 1][1] > y) & (v[x + 1][1] > y):
valleys.append((x, y))
except Exception:
pass
return peaks, valleys
# input signal
t = array(range(100))
series = 8.3 * sin(t) + 2.7 * cos(2 * t) - 4.5 * sin(1.2 * t)
arr = [*zip(t, series)]
thresh = 0.95
peaks, valleys = peakdet(arr, thresh)
scatter([x for x, y in peaks], [y for x, y in peaks], color = 'red')
scatter([x for x, y in valleys], [y for x, y in valleys], color = 'blue')
plot(t, 100 * [thresh], color='green', linestyle='--', dashes=(5, 3))
plot(t, 100 * [-thresh], color='green', linestyle='--', dashes=(5, 3))
plot(t, series, 'k')
show()