我正在尝试使用圆形内核计算矩阵上的局部最大值滤波器。
输出应该是局部最大值的单元格。对于输入“数据”中的每个像素,我需要通过圆形窗口查看它是否是局部最大值,从而返回值 1,否则返回 0。
我有这段代码,基于这里的答案:如何将圆盘形掩码应用于 numpy 数组? https://stackoverflow.com/questions/8647024/how-to-apply-a-disc-shaped-mask-to-a-numpy-array
import numpy as np
import scipy.ndimage as sc
radius = 2
kernel = np.zeros((2*radius+1, 2*radius+1))
y,x = np.ogrid[-radius:radius+1, -radius:radius+1]
mask2 = x**2 + y**2 <= radius**2
kernel[mask2] = 1
def local_maxima(matrix, window_size):
loc_max = sc.maximum_filter(matrix, window_size, mode='constant')
return loc_max
data = np.array([(1, 1, 1, 1, 1, 1, 1, 1, 1), (1, 1, 1, 1, 1, 1, 1, 1, 1), (1, 1, 1, 1, 1, 1, 1, 1, 1),
(1, 1, 1, 1, 1, 1, 1, 1, 1), (1, 1, 1, 1, 4, 1, 1, 1, 1), (1, 1, 1, 1, 1, 1, 1, 1, 1),
(1, 1, 1, 1, 1, 1, 1, 1, 1), (1, 1, 1, 1, 1, 1, 1, 1, 1), (1, 1, 1, 1, 1, 1, 1, 1, 1),
(1, 1, 1, 1, 1, 1, 1, 1, 1)])
loc_max = sc.filters.generic_filter(data, local_maxima(data, np.shape(kernel)), footprint=kernel)
max_matrix = np.where(loc_max == data, 1, 0)
np.savetxt('.....\Local\Test_Local_Max.txt', max_matrix, delimiter='\t')
内核具有以下形状:
[[ 0. 0. 1. 0. 0.]
[ 0. 1. 1. 1. 0.]
[ 1. 1. 1. 1. 1.]
[ 0. 1. 1. 1. 0.]
[ 0. 0. 1. 0. 0.]]
因此,搜索单元将仅是值为 1 的单元。值为 0 的单元应从局部最大值搜索中排除。
但该脚本在第 21 行给出了以下错误:
RuntimeError: function parameter is not callable
谢谢你的帮助!