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

Chr*_*lan 12 matlab fft image-processing

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

img = imread('img.jpg');
fft = fft2(img);
inv = ifft2(fft);
imshow(inv);
Run Code Online (Sandbox Code Playgroud)

gno*_*ice 15

由于fft2ifft2两者都执行在任一计算doublesingle精度,您的图像数据(其是有可能的类型的uint8)被转换为类型double由处理之前先fft2.因此,您必须inv使用uint8恢复原始图像的函数将输出图像转换回无符号的8位整数:

>> 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!
Run Code Online (Sandbox Code Playgroud)

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