mirror of
https://github.com/wassname/scikit-image.git
synced 2026-07-13 17:45:20 +08:00
Use feature.peak_local_max instead of custom peak detection.
This commit is contained in:
@@ -13,7 +13,7 @@ a simple peak extraction algorithm.
|
||||
"""
|
||||
|
||||
import numpy as np
|
||||
from skimage.feature import match_template
|
||||
from skimage.feature import match_template, peak_local_max
|
||||
from numpy.random import randn
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
@@ -30,21 +30,14 @@ image += randn(400, 400)*2
|
||||
# Match the template.
|
||||
result = match_template(image, target, method='norm-corr')
|
||||
|
||||
# peak extraction algorithm.
|
||||
delta = 5
|
||||
found_positions = []
|
||||
for i in range(50):
|
||||
index = np.argmax(result)
|
||||
y, x = np.unravel_index(index, result.shape)
|
||||
if not found_positions:
|
||||
found_positions.append((x, y))
|
||||
for position in found_positions:
|
||||
distance = np.sqrt((x - position[0]) ** 2 + (y - position[1]) ** 2)
|
||||
if distance > delta:
|
||||
found_positions.append((x, y))
|
||||
result[y, x] = 0
|
||||
if len(found_positions) == len(target_positions):
|
||||
break
|
||||
found_positions = peak_local_max(result)
|
||||
|
||||
if len(found_positions) > 2:
|
||||
# Keep the two maximum peaks.
|
||||
intensities = result[tuple(found_positions.T)]
|
||||
i_maxsort = np.argsort(intensities)[::-1]
|
||||
found_positions = found_positions[i_maxsort][:2]
|
||||
|
||||
x_found, y_found = np.transpose(found_positions)
|
||||
|
||||
plt.gray()
|
||||
@@ -67,3 +60,4 @@ plt.autoscale(tight=True)
|
||||
plt.axis('off')
|
||||
|
||||
plt.show()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user