def find_extrema(cls, image):
"""
Finds extrema, both mininma and maxima, based on local maximum filter.
Returns extrema in form of two rows, where the first and second are
positions of x and y, respectively.
Parameters
----------
image : numpy 2D array
Monochromatic image or any 2D array.
Returns
-------
min_peaks : numpy array
Minima positions.
max_peaks : numpy array
Maxima positions.
"""
# define an 3x3 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_min = maximum_filter(-image, footprint=neighborhood)==-image
local_max = maximum_filter(image, footprint=neighborhood)==image
# can't distinguish between background zero and filter zero
background = (image==0)
#appear along the bg border (artifact of the local max 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)
min_peaks = local_min ^ eroded_background
max_peaks = local_max ^ eroded_background
min_peaks[[0,-1],:] = False
min_peaks[:,[0,-1]] = False
max_peaks[[0,-1],:] = False
max_peaks[:,[0,-1]] = False
min_peaks = np.nonzero(min_peaks)
max_peaks = np.nonzero(max_peaks)
return min_peaks, max_peaks
评论列表
文章目录