- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试获取图像的 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/
我是一名优秀的程序员,十分优秀!