Docs for 0.14.3
All versions

# Finding local maxima¶

The peak_local_max function returns the coordinates of local peaks (maxima) in an image. A maximum filter is used for finding local maxima. This operation dilates the original image and merges neighboring local maxima closer than the size of the dilation. Locations where the original image is equal to the dilated image are returned as local maxima. from scipy import ndimage as ndi
import matplotlib.pyplot as plt
from skimage.feature import peak_local_max
from skimage import data, img_as_float

im = img_as_float(data.coins())

# image_max is the dilation of im with a 20*20 structuring element
# It is used within peak_local_max function
image_max = ndi.maximum_filter(im, size=20, mode='constant')

# Comparison between image_max and im to find the coordinates of local maxima
coordinates = peak_local_max(im, min_distance=20)

# display results
fig, axes = plt.subplots(1, 3, figsize=(8, 3), sharex=True, sharey=True)
ax = axes.ravel()
ax.imshow(im, cmap=plt.cm.gray)
ax.axis('off')
ax.set_title('Original')

ax.imshow(image_max, cmap=plt.cm.gray)
ax.axis('off')
ax.set_title('Maximum filter')

ax.imshow(im, cmap=plt.cm.gray)
ax.autoscale(False)
ax.plot(coordinates[:, 1], coordinates[:, 0], 'r.')
ax.axis('off')
ax.set_title('Peak local max')

fig.tight_layout()

plt.show()


Total running time of the script: ( 0 minutes 0.128 seconds)

Gallery generated by Sphinx-Gallery