python - 绘制图像的傅里叶变换时出现问题。 "ValueError: x and y can be no greater than 2-D, but have shapes (2592,) and (2592, 1, 3)"

标签 python python-3.x matplotlib image-processing fft

我正在尝试获取图像的 fft,然后使用 matplotlib 绘制该 fft 的 fraq。但是,此错误消息:

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

我尝试像这样 reshape 我的 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)

最佳答案

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

  • 如果只需要一个 channel 的信息,可以选择只保留第三维的第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)

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

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 不再具有第三维。之后就没有错误了。

关于python - 绘制图像的傅里叶变换时出现问题。 "ValueError: x and y can be no greater than 2-D, but have shapes (2592,) and (2592, 1, 3)",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53944197/

相关文章:

python - 从数据库中获取数据并将其放入 GUI 中的表中

python - __init__.py 和 __main__.py 有什么区别?

Python - 模拟链式函数调用

python - Matplotlib 的绘图非常缓慢

python - matplotlib 绘图速度非常慢

python - 在 map 上绘制点,但错误代码为 "ValueError: ' box_aspect' 且 'fig_aspect' 必须为正值"

python - 运行时错误 : Invalid DISPLAY variable

python - 使 sklearn 中的网格搜索功能忽略空模型

python - 是否有代码可以在程序中运行以使用已知密码连接到安全 WiFi

python - Pandas 条件创建同时排除