mirror of
https://github.com/wassname/scikit-image.git
synced 2026-08-14 12:50:39 +08:00
FEAT - combined API from is_local_maximum() into peak_local_max()
is_local_maximum() is a wrapper function for peak_local_max() is_local_maximum() runs much faster (~20% of prior runtime, nearly = to peak_local_max()) All tests in .feature and .morphology subpackages pass as written with these changes. Todo: * Fully document API * remove commented-out old algorithm in is_local_maximum() * add new tests for full coverage of new, more complex peak_local_max()
This commit is contained in:
@@ -28,6 +28,7 @@ from _heapq import heappush, heappop
|
||||
import numpy as np
|
||||
import scipy.ndimage
|
||||
from ..filter import rank_order
|
||||
from ..feature import peak_local_max
|
||||
|
||||
from . import _watershed
|
||||
|
||||
@@ -281,62 +282,65 @@ def is_local_maximum(image, labels=None, footprint=None):
|
||||
[False, False, False, False],
|
||||
[False, True, False, True]], dtype=bool)
|
||||
"""
|
||||
if labels is None:
|
||||
labels = np.ones(image.shape, dtype=np.uint8)
|
||||
if footprint is None:
|
||||
footprint = np.ones([3] * image.ndim, dtype=np.uint8)
|
||||
assert((np.all(footprint.shape) & 1) == 1)
|
||||
footprint = (footprint != 0)
|
||||
footprint_extent = (np.array(footprint.shape) - 1) // 2
|
||||
if np.all(footprint_extent == 0):
|
||||
return labels > 0
|
||||
result = (labels > 0).copy()
|
||||
#
|
||||
# Create a labels matrix with zeros at the borders that might be
|
||||
# hit by the footprint.
|
||||
#
|
||||
big_labels = np.zeros(np.array(labels.shape) + footprint_extent * 2,
|
||||
labels.dtype)
|
||||
big_labels[[slice(fe, -fe) for fe in footprint_extent]] = labels
|
||||
#
|
||||
# Find the relative indexes of each footprint element
|
||||
#
|
||||
image_strides = np.array(image.strides) // image.dtype.itemsize
|
||||
big_strides = np.array(big_labels.strides) // big_labels.dtype.itemsize
|
||||
result_strides = np.array(result.strides) // result.dtype.itemsize
|
||||
footprint_offsets = np.mgrid[[slice(-fe, fe + 1) for fe in footprint_extent]]
|
||||
# if labels is None:
|
||||
# labels = np.ones(image.shape, dtype=np.uint8)
|
||||
# if footprint is None:
|
||||
# footprint = np.ones([3] * image.ndim, dtype=np.uint8)
|
||||
# assert((np.all(footprint.shape) & 1) == 1)
|
||||
# footprint = (footprint != 0)
|
||||
# footprint_extent = (np.array(footprint.shape) - 1) // 2
|
||||
# if np.all(footprint_extent == 0):
|
||||
# return labels > 0
|
||||
# result = (labels > 0).copy()
|
||||
# #
|
||||
# # Create a labels matrix with zeros at the borders that might be
|
||||
# # hit by the footprint.
|
||||
# #
|
||||
# big_labels = np.zeros(np.array(labels.shape) + footprint_extent * 2,
|
||||
# labels.dtype)
|
||||
# big_labels[[slice(fe, -fe) for fe in footprint_extent]] = labels
|
||||
# #
|
||||
# # Find the relative indexes of each footprint element
|
||||
# #
|
||||
# image_strides = np.array(image.strides) // image.dtype.itemsize
|
||||
# big_strides = np.array(big_labels.strides) // big_labels.dtype.itemsize
|
||||
# result_strides = np.array(result.strides) // result.dtype.itemsize
|
||||
# footprint_offsets = np.mgrid[[slice(-fe, fe + 1) for fe in footprint_extent]]
|
||||
|
||||
fp_image_offsets = np.sum(image_strides[:, np.newaxis] *
|
||||
footprint_offsets[:, footprint], 0)
|
||||
fp_big_offsets = np.sum(big_strides[:, np.newaxis] *
|
||||
footprint_offsets[:, footprint], 0)
|
||||
#
|
||||
# Get the index of each labeled pixel in the image and big_labels arrays
|
||||
#
|
||||
indexes = np.mgrid[[slice(0, x) for x in labels.shape]][:, labels > 0]
|
||||
image_indexes = np.sum(image_strides[:, np.newaxis] * indexes, 0)
|
||||
big_indexes = np.sum(big_strides[:, np.newaxis] *
|
||||
(indexes + footprint_extent[:, np.newaxis]), 0)
|
||||
result_indexes = np.sum(result_strides[:, np.newaxis] * indexes, 0)
|
||||
#
|
||||
# Now operate on the raveled images
|
||||
#
|
||||
big_labels_raveled = big_labels.ravel()
|
||||
image_raveled = image.ravel()
|
||||
result_raveled = result.ravel()
|
||||
#
|
||||
# A hit is a hit if the label at the offset matches the label at the pixel
|
||||
# and if the intensity at the pixel is greater or equal to the intensity
|
||||
# at the offset.
|
||||
#
|
||||
for fp_image_offset, fp_big_offset in zip(fp_image_offsets, fp_big_offsets):
|
||||
same_label = (big_labels_raveled[big_indexes + fp_big_offset] ==
|
||||
big_labels_raveled[big_indexes])
|
||||
less_than = (image_raveled[image_indexes[same_label]] <
|
||||
image_raveled[image_indexes[same_label] + fp_image_offset])
|
||||
result_raveled[result_indexes[same_label][less_than]] = False
|
||||
# fp_image_offsets = np.sum(image_strides[:, np.newaxis] *
|
||||
# footprint_offsets[:, footprint], 0)
|
||||
# fp_big_offsets = np.sum(big_strides[:, np.newaxis] *
|
||||
# footprint_offsets[:, footprint], 0)
|
||||
# #
|
||||
# # Get the index of each labeled pixel in the image and big_labels arrays
|
||||
# #
|
||||
# indexes = np.mgrid[[slice(0, x) for x in labels.shape]][:, labels > 0]
|
||||
# image_indexes = np.sum(image_strides[:, np.newaxis] * indexes, 0)
|
||||
# big_indexes = np.sum(big_strides[:, np.newaxis] *
|
||||
# (indexes + footprint_extent[:, np.newaxis]), 0)
|
||||
# result_indexes = np.sum(result_strides[:, np.newaxis] * indexes, 0)
|
||||
# #
|
||||
# # Now operate on the raveled images
|
||||
# #
|
||||
# big_labels_raveled = big_labels.ravel()
|
||||
# image_raveled = image.ravel()
|
||||
# result_raveled = result.ravel()
|
||||
# #
|
||||
# # A hit is a hit if the label at the offset matches the label at the pixel
|
||||
# # and if the intensity at the pixel is greater or equal to the intensity
|
||||
# # at the offset.
|
||||
# #
|
||||
# for fp_image_offset, fp_big_offset in zip(fp_image_offsets, fp_big_offsets):
|
||||
# same_label = (big_labels_raveled[big_indexes + fp_big_offset] ==
|
||||
# big_labels_raveled[big_indexes])
|
||||
# less_than = (image_raveled[image_indexes[same_label]] <
|
||||
# image_raveled[image_indexes[same_label] + fp_image_offset])
|
||||
# result_raveled[result_indexes[same_label][less_than]] = False
|
||||
|
||||
return result
|
||||
# return result
|
||||
return peak_local_max(image, labels=labels, min_distance=1,
|
||||
footprint=footprint, indices=False,
|
||||
exclude_border=False)
|
||||
|
||||
|
||||
# ---------------------- deprecated ------------------------------
|
||||
|
||||
Reference in New Issue
Block a user