如何使用 Python 将 FFT(快速傅立叶变换)转换为极坐标变换?

How to transform a FFT (Fast Fourier Transform) into a Polar Transformation with Python?

我能够从我的图像创建 FFT 变换,但我不知道如何继续...

我正在使用它来解决我的问题:

到目前为止,这段代码对我有用:

import cv2
import numpy as np
from matplotlib import pyplot as plt

%matplotlib inline

img = cv2.imread(r'test.jpg', cv2.IMREAD_GRAYSCALE)

f = np.fft.fft2(img)
fshift = np.fft.fftshift(f)
magnitude_spectrum = 20 * np.log(np.abs(fshift))

plt.subplot(121), plt.imshow(img, cmap='gray')
plt.title('Input Image'), plt.xticks([]), plt.yticks([])

plt.subplot(122), plt.imshow(magnitude_spectrum, cmap='gray')
plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([])

plt.show()

我需要极坐标变换生成的平均值,但我不知道如何将 FFT 变换为 极坐标变换Python.

这就是你问题的大致解决方案;它在一张样本图像上进行了测试,结果看起来很可信。

# your code goes here... 

def transform_data(m):
    dpix, dpiy = m.shape
    x_c, y_c = np.unravel_index(np.argmax(m), m.shape)
    angles = np.linspace(0, np.pi*2, min(dpix, dpiy))
    mrc = min(abs(x_c - dpix), abs(y_c - dpiy), x_c, y_c)
    radiuses = np.linspace(0, mrc, max(dpix, dpiy))
    A, R = np.meshgrid(angles, radiuses)
    X = R * np.cos(A)
    Y = R * np.sin(A)
    return A, R, m[X.astype(int) + mrc - 1, Y.astype(int) + mrc - 1]

    angles, radiuses, m = transform_data(magnitude_spectrum)

    plt.contourf(angles, radiuses, m)

最后,我们就可以得到我们想要旋转原图的角度了:

sample_angles = np.linspace(0,  2 * np.pi, len(c.sum(axis=0))) / np.pi*180
turn_angle_in_degrees = 90 - sample_angles[np.argmax(c.sum(axis=0))]

对于我的示例图片,我得到了:

turn_angle_in_degrees = 3.2015810276679844 degrees.

此外,我们可以绘制投影频谱幅度:

plt.plot(sample_angles, c.sum(axis=0))

希望对您有所帮助...