[英]Local Maxima with circular window
我正在尝试使用圆形内核在矩阵上计算局部最大值过滤器。 输出应该是局部最大值的像元。 对于输入“数据”中的每个像素,我需要通过圆形窗口查看它是否是局部最大值,因此返回值1,否则返回0。
我有基于以下答案的代码: 如何将圆盘形蒙版应用于numpy数组?
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
谢谢你的帮助!
sc.filters.generic_filter()
的第二个参数应该是一个函数,您要向其传递由local_maxima(data, np.shape(kernel))
调用返回的值,即矩阵。
我对您在这里所做的工作有些困惑,但是我认为您根本不需要generic_filter
调用, maximum_filter
应该可以执行您想要的操作:
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
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.maximum_filter(data, footprint=kernel, mode='constant')
max_matrix = np.where(loc_max == data, 1, 0)
np.savetxt('.....\Local\Test_Local_Max.txt', max_matrix, delimiter='\t')
(我没有在这台计算机上安装python,所以尚未对此进行测试,对不起)
编辑:我已经测试过,它似乎可以给出正确的结果:
[[1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 0, 1, 1, 1, 1],
[1, 1, 1, 0, 0, 0, 1, 1, 1],
[1, 1, 0, 0, 1, 0, 0, 1, 1],
[1, 1, 1, 0, 0, 0, 1, 1, 1],
[1, 1, 1, 1, 0, 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]]
如果访问的单元格是kernel
定义的圆形窗口的局部最大值,则可以使用下面的返回1
的代码(我只是使用%pylab
绘制结果作为说明):
%pylab
import scipy.ndimage as sc
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)])
matshow(data)
colorbar()
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
matshow(kernel)
colorbar()
def filter_func(a):
return a[len(a)/2] == a.max()
out = sc.generic_filter(data, filter_func, footprint=kernel)
matshow(out)
colorbar()
以下是带有随机输入数据数组的结果:
data = np.random.random(size=data.shape)
matshow(data)
out = sc.generic_filter(data, filter_func, footprint=kernel)
matshow(out)
colorbar()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.