开发者

Why isn't this inverse Fourier transform giving the correct results?

开发者 https://www.devze.com 2023-01-16 16:34 出处:网络
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 obvious开发者_Go百科ly some implementation detail that I don\'t know

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 obvious开发者_Go百科ly 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);


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消