【问题标题】: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)"绘制图像的傅立叶变换时出现问题。 “ValueError:x 和 y 不能大于 2-D,但具有 (2592,) 和 (2592, 1, 3) 形状”
【发布时间】:2019-05-25 10:23:33
【问题描述】:

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

“ValueError:x 和 y 不能大于 2-D,但具有 (2592,) 和 (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(最近一次调用最后一次):

文件“myfrouier.py”,第 21 行,在

plt.semilogy(abs(fourier).astype(np.uint8)) 文件

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

第 2878 行,符号学 return gca().semilogy(*args, **kwargs)
文件“/home/aybarsyildiz/.local/lib/python3.6/site-packages/matplotlib/axes/_axes.py”, 第 1844 行,符号学 l = self.plot(*args, **kwargs) 文件“/home/aybarsyildiz/.local/lib/python3.6/site-packages/matplotlib/init.py”, 第 1810 行,在内部 返回函数(ax,*args,**kwargs)
文件“/home/aybarsyildiz/.local/lib/python3.6/site-packages/matplotlib/axes/_axes.py”, 第 1611 行,在情节中 对于 self._get_lines(*args, **kwargs) 中的行:
文件“/home/aybarsyildiz/.local/lib/python3.6/site-packages/matplotlib/axes/_base.py”, 第 393 行,在 _grab_next_args 来自 self._plot_args(this, kwargs) 文件“/home/aybarsyildiz/.local/lib/python3.6/site-packages/matplotlib/axes/_base.py”, 第 370 行,在 _plot_args x, y = self._xy_from_xy(x, y) 文件“/home/aybarsyildiz/.local/lib/python3.6/site-packages/matplotlib/axes/_base.py”, 第 234 行,在 _xy_from_xy "形状 {} 和 {}".format(x.shape, y.shape)) ValueError: x 和 y 不能大于 2-D,但具有 (2592,) 和 (2592, 1, 3) 形状

【问题讨论】:

  • 错误似乎很明显。 y的形状是(2592, 1, 3),你需要一个二维数组

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


【解决方案1】:

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

  • 如果只需要一个通道的信息,可以选择只保留第三维的第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 和 y 不能大于 2-D,但具有 (244,) 和 (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 不再有第三维。之后没有错误。

【讨论】:

    猜你喜欢
    • 2020-10-27
    • 1970-01-01
    • 1970-01-01
    • 2020-04-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 2015-04-04
    • 1970-01-01
    相关资源
    最近更新 更多