我已经实施了解决方案here。这是一段非常好的代码,但它只能获得本地的单个像素最大值。 我需要的是获得最大值及其邻域,假设一个以局部最大值为中心的3x3矩阵。
我有以下代码只能获取本地最大值和最小值,而不是它们的周围环境:
def detect_peaks(self, image=None, radius=1):
"""
Takes an image and detect the peaks usingthe local maximum filter.
Returns a boolean mask of the peaks (i.e. 1 when
the pixel's value is the neighborhood maximum, 0 otherwise)
"""
if (image == None):
image = self.getParticlesMatrix()
# define an 8-connected neighborhood
r = 2
neighborhood = generate_binary_structure(r,r)
#apply the local maximum filter; all pixel of maximal value
#in their neighborhood are set to 1
local_max = maximum_filter(image, footprint=neighborhood) == image
#local_max is a mask that contains the peaks we are
#looking for, but also the background.
#In order to isolate the peaks we must remove the background from the mask.
local_min = minimum_filter(image, footprint=neighborhood) == image
#we create the mask of the background
background = (image == 0)
#a little technicality: we must erode the background in order to
#successfully subtract it form local_max, otherwise a line will
#appear along the background border (artifact of the local maximum 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
detected_peaks = (local_max - eroded_background) + (local_min - eroded_background)
return np.multiply( detected_peaks, image )
注意参数r = 2,我试过r = 3但它不起作用。
问题是:如何过滤以获取图片局部最大值和最小值以及周围环境?
感谢。