Problem plotting an image's Fourier transforms. "ValueError: x and y can be no greater than 2-D, but have shapes (2592,) and (2592, 1, 3)"

Problem plotting an image's Fourier transforms. "ValueError: x and y can be no greater than 2-D, but have shapes (2592,) and (2592, 1, 3)"

我正在尝试获取图像的 fft,然后使用 matplotlib 绘制该 fft 的 fraq。然而,这个错误信息:

"ValueError: x and y can be no greater than 2-D, but have shapes (2592,) and (2592, 1, 3)".

我试着像这样重塑我的 np.array:

import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import tkinter
from scipy.fftpack import fft, fft2, fftshift

resim = Image.open(r'yeni.jpg')

resim_data = np.asarray(resim)

fourier = fft2(resim_data)

#psd2D = np.abs(fourier)**2


plt.figure()
plt.semilogy(abs(fourier).astype(np.uint8))
plt.title('fourier transform fraq')
plt.show()

错误消息爆炸:

Traceback (most recent call last):

File "myfrouier.py", line 21, in

plt.semilogy(abs(fourier).astype(np.uint8)) File

"/home/aybarsyildiz/.local/lib/python3.6/site-packages/matplotlib/pyplot.py",

line 2878, in semilogy return gca().semilogy(*args, **kwargs)
File "/home/aybarsyildiz/.local/lib/python3.6/site-packages/matplotlib/axes/_axes.py", line 1844, in semilogy l = self.plot(*args, **kwargs) File "/home/aybarsyildiz/.local/lib/python3.6/site-packages/matplotlib/init.py", line 1810, in inner return func(ax, *args, **kwargs)
File "/home/aybarsyildiz/.local/lib/python3.6/site-packages/matplotlib/axes/_axes.py", line 1611, in plot for line in self._get_lines(*args, **kwargs):
File "/home/aybarsyildiz/.local/lib/python3.6/site-packages/matplotlib/axes/_base.py", line 393, in _grab_next_args yield from self._plot_args(this, kwargs) File "/home/aybarsyildiz/.local/lib/python3.6/site-packages/matplotlib/axes/_base.py", line 370, in _plot_args x, y = self._xy_from_xy(x, y) File "/home/aybarsyildiz/.local/lib/python3.6/site-packages/matplotlib/axes/_base.py", line 234, in _xy_from_xy "shapes {} and {}".format(x.shape, y.shape)) ValueError: x and y can be no greater than 2-D, but have shapes (2592,) and (2592, 1, 3)

您似乎没有必要的二维数组,而是一个具有额外三维的数组。您必须选择要对该维度执行的操作:

  • 如果只需要一个通道的信息,可以选择只保留第n个​​维度的值:

    n = 1
    resim_data = resim_data[:, :, n]
    
  • 计算第三维所有值的平均值

    resim_data = resim_data.mean(axis=-1)
    
  • 选择所有第三维值的最大值

    resim_data = resim_data.max(axis=-1)
    
  • ...


示例:

我将你的代码与 244x244 像素的示例图像一起使用,但得到了与你的类似的错误:

ValueError: x and y can be no greater than 2-D, but have shapes (244,) and (244, 244, 4)

我只对第一个通道感兴趣,所以我从三维中删除了所有其他不必要的值:

resim_data = np.asarray(resim)
print(resim_data.shape)
n = 0
resim_data = resim_data[:, :, n]
print(resim_data.shape)

打印:

(244, 244, 4)
(244, 244)

如您所见,resim_data 不再有三维空间。之后没有错误。