fft 和数组到图像/图像到数组转换

发布于 2024-08-29 19:42:53 字数 85 浏览 6 评论 0原文

我想对图像进行傅里叶变换。 但是如何将图片转为数组呢? 之后我想我应该使用 numpy.fft.rfft2 进行转换。 以及如何从数组变回图像? 提前致谢。

I want to make a fourier-transformation of an image.
But how can I change the picture to an array?
And after this I think I should use numpy.fft.rfft2 for the transformation.
And how to change back from the array to the image?
Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

穿透光 2024-09-05 19:42:53

您可以使用 PIL 库来加载/保存图像以及与 numpy 数组之间的转换。

import Image, numpy
i = Image.open('img.png')
i = i.convert('L')    #convert to grayscale
a = numpy.asarray(i) # a is readonly

b = abs(numpy.fft.rfft2(a))

j = Image.fromarray(b)
j.save('img2.png')

我在上面使用了 abs ,因为 FFT 的结果具有复数值,因此将其直接转换为图像并没有多大意义。转换为灰度后,FFT 仅在单个通道上完成 - 您可以选择另一种方式来选择通道,或将正确的 axes 参数传递给 rfft2 然后提取您需要的频道。

编辑:

要执行逆FFT并取回原始图像,以下方法对我有用:

import Image, numpy
i = Image.open('img.png')
i = i.convert('L')    #convert to grayscale
a = numpy.asarray(i)

b = numpy.fft.rfft2(a)
c = numpy.fft.irfft2(b)

j = Image.fromarray(c.astype(numpy.uint8))
j.save('img2.png')

You can use the PIL library to load/save images and convert to/from numpy arrays.

import Image, numpy
i = Image.open('img.png')
i = i.convert('L')    #convert to grayscale
a = numpy.asarray(i) # a is readonly

b = abs(numpy.fft.rfft2(a))

j = Image.fromarray(b)
j.save('img2.png')

I used abs above because the result of the FFT has complex values so it doesn't really make sense to convert it directly to an image. The conversion to grayscale is done so that the FFT is done on a single channel only - you can choose another way to pick a channel instead, or pass the correct axes parameter to rfft2 and later extract the channel you need.

Edit:

To also perform an inverse FFT and get back the original image, the following works for me:

import Image, numpy
i = Image.open('img.png')
i = i.convert('L')    #convert to grayscale
a = numpy.asarray(i)

b = numpy.fft.rfft2(a)
c = numpy.fft.irfft2(b)

j = Image.fromarray(c.astype(numpy.uint8))
j.save('img2.png')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文