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

谁说胖子不能爱 提交于 2021-01-27 21:12:56

问题


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:

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

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

ERROR MESSAGE BLELOW:

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)


回答1:


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:

  • 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)
    
  • ...


Example:

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

ValueError: x and y can be no greater than 2-D, but have shapes (244,) and (244, 244, 4)

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)

Which prints:

(244, 244, 4)
(244, 244)

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



来源:https://stackoverflow.com/questions/53944197/problem-plotting-an-images-fourier-transforms-valueerror-x-and-y-can-be-no-g

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!