MATLAB Language Image processing Filtering Using a 2D FFT

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

Like for 1D signals, it's possible to filter images by applying a Fourier transformation, multiplying with a filter in the frequency domain, and transforming back into the space domain. Here is how you can apply high- or low-pass filters to an image with Matlab:

Let image be the original, unfiltered image, here's how to compute its 2D FFT:

ft = fftshift(fft2(image));

Now to exclude a part of the spectrum, one need to set its pixel values to 0. The spatial frequency contained in the original image is mapped from the center to the edges (after using fftshift). To exclude the low frequencies, we will set the central circular area to 0.

Here's how to generate a disc-shaped binary mask with radius D using built-in function:

[x y ~] = size(ft);
D = 20;
mask = fspecial('disk', D) == 0;
mask = imresize(padarray(mask, [floor((x/2)-D) floor((y/2)-D)], 1, 'both'), [x y]);

Masking the frequency domain image can be done by multiplying the FFT point-wise with the binary mask obtained above:

masked_ft = ft .* mask;

Now, let's compute the inverse FFT:

filtered_image = ifft2(ifftshift(masked_ft), 'symmetric');

The high frequencies in an image are the sharp edges, so this high-pass filter can be used to sharpen images.



Got any MATLAB Language Question?