为什么傅里叶逆变换不能给出正确的结果?

发布于 2024-09-19 23:29:45 字数 173 浏览 2 评论 0原文

我想在 MATLAB 中反转图像的傅立叶变换,但结果不是原始图像(应有的样子)。显然有一些我不知道的实现细节导致了这个问题。这是代码:

img = imread('img.jpg');
fft = fft2(img);
inv = ifft2(fft);
imshow(inv);

I want to invert the Fourier transform of an image in MATLAB, but the result is not the original image (as it should be). There is obviously some implementation detail that I don't know about that's causing the issue. Here's the code:

img = imread('img.jpg');
fft = fft2(img);
inv = ifft2(fft);
imshow(inv);

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

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

发布评论

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

评论(2

┊风居住的梦幻卍 2024-09-26 23:29:45

fft2ifft2 都在 double 精度,您的图像数据(可能是 uint8) 在被 fft2< 处理之前首先转换为 double 类型/代码>。因此,您必须使用函数 uint8 来恢复原始图像:

>> img = imread('peppers.png');  % Load a sample image
>> fft = fft2(img);   % Get the Fourier transform
>> inv = ifft2(fft);  % Get the inverse Fourier transform
>> inv = uint8(inv);  % Convert to uint8
>> imshow(inv);       % Show the image
>> isequal(img, inv)  % Test if inv matches the original image img

ans =

     1                % It does!

注意: 作为一个额外的提示,我会避免命名你的变量 fftinv 因为具有这些名称的函数已存在于 MATLAB 中。

Since fft2 and ifft2 both perform calculations in either double or single precision, your image data (which is likely of type uint8) gets converted to type double first before being processed by fft2. You will therefore have to convert your output image inv back to an unsigned 8-bit integer using the function uint8 to recover the original image:

>> img = imread('peppers.png');  % Load a sample image
>> fft = fft2(img);   % Get the Fourier transform
>> inv = ifft2(fft);  % Get the inverse Fourier transform
>> inv = uint8(inv);  % Convert to uint8
>> imshow(inv);       % Show the image
>> isequal(img, inv)  % Test if inv matches the original image img

ans =

     1                % It does!

NOTE: As an additional tip, I would avoid naming your variables fft and inv since functions with those names already exist in MATLAB.

奈何桥上唱咆哮 2024-09-26 23:29:45

另外,如果您尝试对彩色(24 位)图像进行 FFT,请注意 imread() 将返回 M x N x 3 数组。因此,您应该对每个 R/G/B 通道分别执行 FFT。

请参阅此了解详细信息。

Also if you are trying to do FFT on color (24-bit) image - note that imread() will return M x N x 3 array. So you should perform FFT on each R/G/B channel separately.

See this for detail.

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