python - 解释 numpy.fft.fft2 输出

标签 python numpy fft frequency-distribution

我的目标是获得具有图像空间频率的图 - 有点像对其进行傅立叶变换。我不关心频率为 f 的特征在图像上的位置(例如);我只想要一个图形,告诉我每个频率有多少(频带的振幅可以用与该频率的对比度之和来表示)。

我正在尝试通过 numpy.fft.fft2 来做到这一点功能。

这里是一个链接 minimal example描述我的用例。

事实证明,我只得到明显更大的 frequencies[:30,:30] 值,其中绝对最高值是 frequencies[0,0]。我该如何解释呢?

  • 每个值的幅度究竟代表什么?
  • 我的最高值在 frequency[0,0] 中是什么意思什么是 0 Hz 频率?
  • 我能否以某种方式对这些值进行分类,以便我的频谱与方向无关?

最佳答案

freq 有一些非常大的值,还有很多小值。你可以通过绘图看到这一点

plt.hist(freq.ravel(), bins=100)

(见下文。)因此,当您使用

ax1.imshow(freq, interpolation="none")

Matplotlib 使用 freq.min() 作为颜色范围内的最低值(默认为蓝色),freq.max() 作为最高值颜色范围内的值(默认情况下为红色)。由于 freq 中的几乎所有值都靠近蓝色端,因此整个图看起来是蓝色的。

您可以通过重新调整 freq 中的值来获得更多信息,以便低值在颜色范围内分布更广泛。

例如,您可以通过获取freqlog 来获得更好的值分布。 (您可能不想丢弃最高值,因为它们对应于具有最高功率的频率。)

import matplotlib as ml
import matplotlib.pyplot as plt
import numpy as np
import Image
file_path = "data"
image = np.asarray(Image.open(file_path).convert('L'))
freq = np.fft.fft2(image)
freq = np.abs(freq)

fig, ax = plt.subplots(nrows=2, ncols=2, figsize=(14, 6))
ax[0,0].hist(freq.ravel(), bins=100)
ax[0,0].set_title('hist(freq)')
ax[0,1].hist(np.log(freq).ravel(), bins=100)
ax[0,1].set_title('hist(log(freq))')
ax[1,0].imshow(np.log(freq), interpolation="none")
ax[1,0].set_title('log(freq)')
ax[1,1].imshow(image, interpolation="none")
plt.show()

enter image description here


来自 the docs :

The output, analogously to fft, contains the term for zero frequency in the low-order corner of the transformed axes,

因此,freq[0,0] 是“零频率”项。换句话说,它是 discrete Fourier Transform 中的常数项。 .

关于python - 解释 numpy.fft.fft2 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21362843/

相关文章:

python - 在 Python 中如何获取字典的部分 View ?

python - 值错误 : Input arrays should have the same number of samples as target arrays. Found 1600 input samples and 6400 target samples

python - 在 virtualenv 中安装 pip 时出现 ImportError

c# - 傅里叶变换舍入误差

python - 如何对多张图像进行傅里叶变换并将输出保存到单个对象中

python - 在特征名称后面加上@有什么作用?

python - 如何在不使用循环的情况下生成循环数字序列?

python - conda搜索最旧版本的numpy,受限于Python版本

python - 如何静态加载 python 模块(如 scipy)?

numpy - 转置时numpy和 Octave 的fft不同