本文介绍了绘制图像的傅立叶变换时出现问题."ValueError:x和y不能大于2-D,但形状为(2592,)和(2592,1,3)".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取图像的英尺,然后使用matplotlib绘制该英尺的帧.但是,此错误消息:

I'm trying to get the fft of an image and then plot the fraq of that fft with using matplotlib. However, this error message:

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

I tried to reshape my np.array like so:

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()

错误消息提示:

文件

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

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

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

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

第2878行,符号学返回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返回func(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",_grab_next_args中的第393行来自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",_xy_from_xy中的第234行形状{}和{}".format(x.shape,y.shape))ValueError:x和y不能大于2-D,但形状为(2592,)和(2592,1,3)

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)

推荐答案

您似乎没有必需的2d数组,但具有附加的三维尺寸的数组.您必须选择要对该维度进行的操作:

You don't seem to have the necessary 2d-array, but an array with an additional third dimension. You have to choose what you want to do with that dimension:

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

  • 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像素的示例图像一起使用,并得到了与您类似的错误:

    I used your code with an example image with 244x244 pixels and got a similar error to yours:

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

    I was only interested in the first channel, so I dropped all other unnecessary values from the third dimension:

    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 不再具有第三维.此后没有错误.

    As you can see, resim_data has no third dimension any more. No errors after that.

    这篇关于绘制图像的傅立叶变换时出现问题."ValueError:x和y不能大于2-D,但形状为(2592,)和(2592,1,3)".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 08-21 12:30