mirror of
https://github.com/wassname/scikit-image.git
synced 2026-07-02 18:31:49 +08:00
Use ndimage.maximum_filter to simplify peak detection.
This change should give identitical results to the previous implementation.
This commit is contained in:
+10
-30
@@ -26,44 +26,24 @@ def peak_min_dist(image, min_distance=10, threshold=0.1):
|
||||
(row, column) coordinates of peaks.
|
||||
"""
|
||||
image = image.copy()
|
||||
# Non maximum filter of size 3
|
||||
image_max = ndimage.maximum_filter(image, 3, mode='constant')
|
||||
# Non maximum filter
|
||||
size = 2 * min_distance + 1
|
||||
image_max = ndimage.maximum_filter(image, size=size, mode='constant')
|
||||
mask = (image == image_max)
|
||||
image *= mask
|
||||
|
||||
# Remove the image borders
|
||||
image[:3] = 0
|
||||
image[-3:] = 0
|
||||
image[:, :3] = 0
|
||||
image[:, -3:] = 0
|
||||
image[:min_distance] = 0
|
||||
image[-min_distance:] = 0
|
||||
image[:, :min_distance] = 0
|
||||
image[:, -min_distance:] = 0
|
||||
|
||||
# find top corner candidates above a threshold
|
||||
corner_threshold = np.max(image.ravel()) * threshold
|
||||
image_t = (image >= corner_threshold) * 1
|
||||
|
||||
# get coordinates of candidates
|
||||
candidates = image_t.nonzero()
|
||||
coords = np.transpose(candidates)
|
||||
# get coordinates of peaks
|
||||
coordinates = np.transpose(image_t.nonzero())
|
||||
|
||||
# ...and their values
|
||||
candidate_values = image[candidates]
|
||||
|
||||
# sort candidates
|
||||
index = np.argsort(candidate_values)[::-1]
|
||||
|
||||
# store allowed point locations in array
|
||||
allowed_locations = np.zeros(image.shape)
|
||||
allowed_locations[min_distance:-min_distance,
|
||||
min_distance:-min_distance] = 1
|
||||
|
||||
# select the best points taking min_distance into account
|
||||
filtered_coords = []
|
||||
for i in index:
|
||||
if allowed_locations[tuple(coords[i])] == 1:
|
||||
filtered_coords.append(coords[i])
|
||||
allowed_locations[
|
||||
(coords[i][0] - min_distance):(coords[i][0] + min_distance),
|
||||
(coords[i][1] - min_distance):(coords[i][1] + min_distance)] = 0
|
||||
|
||||
return np.array(filtered_coords)
|
||||
return coordinates
|
||||
|
||||
|
||||
Reference in New Issue
Block a user