反卷积似乎不起作用(fft2; scipy)
我在空间域中将信号 f
与内核 h
进行卷积,然后在频域中进行反卷积。由于我知道内核 h
,理论上我应该能够毫无问题地恢复 f
,但是,它不起作用。这就是我所拥有的:
from scipy.fft import fft, ifft, fftfreq, fft2, ifft2, fftshift, ifftshift
from scipy.ndimage import convolve
d = 11 # diameter of image
f = np.zeros((d, d))
r = 1 # radius of line
f[d//2-1: d//2+r+1] = 1 # image with a line
f[d//2, d//2] = 0 # and 0 at the center
f[0, 0] = 2 # and a 2 at top left corner
print("\nf =")
print(f)
h = np.zeros_like(f)
h[d//2-1, d//2-1] = h[d//2, d//2] = h[d//2+1, d//2+1] = 1 # identity filter
print("\nh =")
print(h)
g = convolve(f, h) # conv spatial domain
G = fft2(g)
H = fft2(h) # fft of filter
f_approx = fftshift(ifft2(G/H)) # deconvolution
print("\nf^approx =")
print(np.round(f_approx.real, 2))
这是输出:
f =
[[2. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 0. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
h =
[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
f^approx =
[[ 0. -0.67 -0. -0. -0. -0. -0. -0. -0. -0. 1.33]
[-0.67 2. 1.33 0. -0. -0. -0. 0. -0. 0. -0. ]
[-0. 1.33 0. -0.67 -0. -0. 0. 0. -0. 0. -0. ]
[-0. 0. -0.67 -2. -0.67 0. -0. -0. -0. -0. -0. ]
[ 1. 1. 1. 0.33 3. 2.33 1. 1. 1. 1. 1. ]
[ 1. 1. 1. 1. 2.33 0. 0.33 1. 1. 1. 1. ]
[ 1. 1. 1. 1. 1. 0.33 -1. 0.33 1. 1. 1. ]
[-0. 0. 0. 0. -0. 0. -0.67 2. 1.33 -0. 0. ]
[-0. -0. 0. -0. -0. -0. -0. 1.33 -0. -0.67 -0. ]
[ 0. 0. 0. -0. 0. -0. 0. 0. -0.67 -2. -0.67]
[ 1.33 -0. -0. -0. 0. 0. 0. -0. -0. -0.67 2. ]]
为什么我没有得到原始的 f
?是我使用的功能不正确,还是方法有问题?谢谢
PS:顺便说一句,如果 h
是恒等式,那么反卷积工作正常,问题是当过滤器是其他东西时。
I'm convolving a signal f
, with a kernel h
, in spatial domain, then, I'm deconvolving in frequency domain. Since I know the kernel h
, in theory I should be able to get f
back without any problem, however, it's not working. This is what I have:
from scipy.fft import fft, ifft, fftfreq, fft2, ifft2, fftshift, ifftshift
from scipy.ndimage import convolve
d = 11 # diameter of image
f = np.zeros((d, d))
r = 1 # radius of line
f[d//2-1: d//2+r+1] = 1 # image with a line
f[d//2, d//2] = 0 # and 0 at the center
f[0, 0] = 2 # and a 2 at top left corner
print("\nf =")
print(f)
h = np.zeros_like(f)
h[d//2-1, d//2-1] = h[d//2, d//2] = h[d//2+1, d//2+1] = 1 # identity filter
print("\nh =")
print(h)
g = convolve(f, h) # conv spatial domain
G = fft2(g)
H = fft2(h) # fft of filter
f_approx = fftshift(ifft2(G/H)) # deconvolution
print("\nf^approx =")
print(np.round(f_approx.real, 2))
This is the output:
f =
[[2. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 0. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
h =
[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
f^approx =
[[ 0. -0.67 -0. -0. -0. -0. -0. -0. -0. -0. 1.33]
[-0.67 2. 1.33 0. -0. -0. -0. 0. -0. 0. -0. ]
[-0. 1.33 0. -0.67 -0. -0. 0. 0. -0. 0. -0. ]
[-0. 0. -0.67 -2. -0.67 0. -0. -0. -0. -0. -0. ]
[ 1. 1. 1. 0.33 3. 2.33 1. 1. 1. 1. 1. ]
[ 1. 1. 1. 1. 2.33 0. 0.33 1. 1. 1. 1. ]
[ 1. 1. 1. 1. 1. 0.33 -1. 0.33 1. 1. 1. ]
[-0. 0. 0. 0. -0. 0. -0.67 2. 1.33 -0. 0. ]
[-0. -0. 0. -0. -0. -0. -0. 1.33 -0. -0.67 -0. ]
[ 0. 0. 0. -0. 0. -0. 0. 0. -0.67 -2. -0.67]
[ 1.33 -0. -0. -0. 0. 0. 0. -0. -0. -0.67 2. ]]
Why am I not getting the original f
back? Am I using the functions incorrectly, or is there some problem with the approach? Thanks
PS: by the way, if the h
is the identity, then the deconvolution works fine, the problem is when the filter is something else.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
谢谢 @chris-luengo。另外,感谢 ahmed 。
我想这就是这样:
我希望它对某人有用;)
Thanks @chris-luengo. Also, thanks to Ahmed.
I guess this would be it, then:
I hope it's useful to someone ;)