【发布时间】:2016-12-28 16:14:30
【问题描述】:
python's scipy 中有一些信号生成辅助函数,但这些仅适用于一维信号。
我想生成一个二维理想带通滤波器,它是一个全零矩阵,带有一圈一以从我的图像中去除一些周期性噪声。
我现在正在处理:
def unit_circle(r):
def distance(x1, y1, x2, y2):
return math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
d = 2*r + 1
mat = np.zeros((d, d))
rx , ry = d/2, d/2
for row in range(d):
for col in range(d):
dist = distance(rx, ry, row, col)
if abs(dist - r) < 0.5:
mat[row, col] = 1
return mat
结果:
In [18]: unit_circle(6)
Out[18]:
array([[ 0., 0., 0., 0., 1., 1., 1., 1., 1., 0., 0., 0., 0.],
[ 0., 0., 1., 1., 0., 0., 0., 0., 0., 1., 1., 0., 0.],
[ 0., 1., 1., 0., 0., 0., 0., 0., 0., 0., 1., 1., 0.],
[ 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
[ 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
[ 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
[ 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
[ 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
[ 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
[ 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
[ 0., 1., 1., 0., 0., 0., 0., 0., 0., 0., 1., 1., 0.],
[ 0., 0., 1., 1., 0., 0., 0., 0., 0., 1., 1., 0., 0.],
[ 0., 0., 0., 0., 1., 1., 1., 1., 1., 0., 0., 0., 0.]])
有没有更直接的方法来生成matrix of circle of ones, all else zeros?
编辑: Python 2.7.12
【问题讨论】:
-
当前输出是你想要的输出吗?
-
@OhadEytan 是的,但感觉有点复杂,希望看到其他解决方案。事实上,我很想知道是否有像 scipy 提供的 1-D 信号那样生成二维信号的框架。
-
您应该始终指定您的 python 版本,因为除法运算符在 Py2 和 Py3 中的工作方式不同,除非您在 Py2 中从
__future__导入新的运算符。
标签: python numpy scipy signal-processing python-2.x