绘制图像的傅里叶变换时出现问题。"ValueError: x和y不能大于2-D,但形状分别为(2592,)和(2592, 1, 3)"。

3

我正在尝试获取图像的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 (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 和 y 的维度不能超过2,但其形状分别为(2592,)和(2592,1,3)


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

2

您似乎没有必要的二维数组,而是有一个额外的第三维数组。您需要选择如何处理该维度:

  • If you only need the information of one channel, you can choose to keep only the n-th values of the third dimension:

    n = 1
    resim_data = resim_data[:, :, n]
    
  • Calculate the mean for all values of the third dimension

    resim_data = resim_data.mean(axis=-1)
    
  • Choose the maximum value for all values of the third dimension

    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 不再具有第三个维度。此后没有出现任何错误。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接