反卷积似乎不起作用(fft2; scipy)

发布于 2025-01-17 19:37:51 字数 2647 浏览 3 评论 0原文

我在空间域中将信号 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遇见了你 2025-01-24 19:37:51

谢谢 @chris-luengo。另外,感谢 ahmed

我想这就是这样:

    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
    print("\nh =")
    print(h)

    g = convolve(f, h, mode="wrap")  # conv spatial domain
    G = fft2(g)
    
    H = fft2(ifftshift(h))  # fft of filter

    f_approx = ifft2(G/H)  # deconvolution
    
    print("\nf^approx =")
    print(np.round(f_approx.real, 2))

我希望它对某人有用;)

Thanks @chris-luengo. Also, thanks to Ahmed.

I guess this would be it, then:

    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
    print("\nh =")
    print(h)

    g = convolve(f, h, mode="wrap")  # conv spatial domain
    G = fft2(g)
    
    H = fft2(ifftshift(h))  # fft of filter

    f_approx = ifft2(G/H)  # deconvolution
    
    print("\nf^approx =")
    print(np.round(f_approx.real, 2))

I hope it's useful to someone ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文