【问题标题】:Which output from Circular Convolution is initially correct? The FFT convolution or the scipy signal wrap convolution循环卷积的哪个输出最初是正确的? FFT 卷积或 scipy 信号环绕卷积
【发布时间】:2018-11-24 16:01:56
【问题描述】:

通过 FFT VS 通过 scipy convolve2d 使用“wrap”边界设置进行循环卷积会得到相同的结果,但在输出矩阵中的不同位置。

在这两种方法中,最初的位置是正确的(即我应该移动哪一种来匹配另一个)?

import numpy as np
import scipy
from scipy import signal
from numpy.fft import ifft2, fft2

imgSize = 4
filtSize = 3
padFilt = imgSize - filtSize
testImg = np.random.uniform(0,1,(imgSize,imgSize))
filter = np.random.normal(0,3.0,(filtSize,filtSize))


circConv = scipy.signal.convolve2d(testImg, filter, boundary="wrap", mode="same")

filterFFT = np.pad(filter,((0,padFilt),(0,padFilt)),'constant')
fftConv = np.real(ifft2(fft2(testImg) * fft2(filterFFT)))

# Roll the FFT output to match the wrap output
fftConv = np.roll(fftConv, (-((filterFFT.shape[0] - 1) // 2), -((filterFFT.shape[1] - 1) // 2)), axis=(0, 1))
print(circConv)
print(fftConv)

【问题讨论】:

    标签: python scipy fft


    【解决方案1】:

    取决于您的坐标原点在哪里。 如果它在左上角,请使用 fft。 如果它在中心,请使用卷积。

    >>> a = (1, 0, 0, 0, 0)
    >>> a = np.outer(a, a)
    >>> a
    array([[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]])
    >>> np.real(ifft2(fft2(a) * fft2(a)))
    array([[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.]])
    >>> 
    >>> a = (0, 0, 1, 0, 0)
    >>> a = np.outer(a, a)
    >>> a
    array([[0, 0, 0, 0, 0],
           [0, 0, 0, 0, 0],
           [0, 0, 1, 0, 0],
           [0, 0, 0, 0, 0],
           [0, 0, 0, 0, 0]])
    >>> scipy.signal.convolve2d(a, a, boundary="wrap", mode="same")
    array([[0, 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),默认情况下是左上角?
    • @Kevinj22 实际上取决于您的过滤器。例如,如果是高斯模糊,峰值在哪里?如果它在中心,那就是原点,如果高斯看起来被切成四等分,顶点在角落,那么左上角就是原点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-06
    • 2012-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-25
    相关资源
    最近更新 更多