Navigation

Related Articles

Back to Latest Articles

How To Use Ideal Lowpass Filter With FFT – C# Guide


Andraz Krzisnik
How To Use Ideal Lowpass Filter With FFT...

Ideal lowpass filter is a filter used to modify frequency values in the frequency domain, and for transforming an image into a frequency domain, we have to use Fourier transform.

We can also use inverse of the transform to get back the values in spatial domain. Images as we see them everyday on the internet are in the spatial domain.

Once we transform an image into frequency domain, we get complex number values. To be more precise, we get a map of values, which is the same size as the image we transformed.

In the example where I showed how to use Fourier transform, the code processes only one color channel. And for Fast Fourier Transform (FFT) algorithm, you can only pass through square images.

zero padding square image fast fourier transform

So to make the program process images of all sizes, I’ve created a function that creates zero padding around the image to make it square. The dimensions must also be of size 2n for the algorithm to work. These are the constraints that come with the calculation speed of the FFT algorithm.

Because the FFT processes only one channel at a time I’ve grayscaled the image beforehand. In this new example I’ve added to it a little bit.

For one, I’ve written a function that removes zero padding from the image once it’s transformed back to spatial domain. Another thing was to make the program use FFT function three times per image, once per color channel.

Filtering frequencies and ideal lowpass filter

We’ve talked about how transformation works, now let’s get into how processing data in frequency domain does.

There’s a correlation between processing data in spatial domain and frequency domain, we call it convolutional theorem. Sounds familiar right?

We used convolution for processing images in the previous tutorials before. It’s basically multiplication of filter and pixel channel values. Because filter is much smaller in size than the image, we need to slide it across the entire image.

How does this theory connect with processing in frequency domain?

As it turns out, convolution in spatial domain yields the same results as element wise multiplication in the frequency domain.

Filters in the frequency domain are not the same as in spatial domain as they span the whole image in size.

frequencies before filtering

Ideal lowpass filter is one of the most basic filters we can use in frequency domain. We need to set a diameter of a circle located in the middle and use it to pass frequencies that fall inside the circle, while setting freqiencies outside to 0.

ideal lowpass filter

If we delete the lower frequencies as ideal lowpass filter does, we will get a blurred image and the smaller the circle in the middle is, the bigger the blurring of our image will be.

public static Complex[][][] Ideal_Lowpass_Filter(Complex[][][] frequencies, double diameter)
         {
             int channels = 3;
             Complex[][][] filtered = frequencies;
             for (int c = 0; c < channels; c++)
             {
                 for (int u = 0; u < Size; u++)
                 {
                     for (int v = 0; v < Size; v++)
                     {
                         double d = Math.Sqrt(Math.Pow(u - Size / 2, 2) + Math.Pow(v - Size / 2, 2));
                         if (d <= diameter)
                         {
                             filtered[c][u][v] = frequencies[c][u][v];
                         }
                         else
                         {
                             filtered[c][u][v] = new Complex(0, 0);
                         }
                     }
                 }
             }
         return filtered;     
}

Function above takes a jagged array of complex numbers which, in our case, are all frequency values from an image.

Conclusion

This tutorial was an introduction into filtering images in frequency domain. Hopefully you found it understandable and you’re looking forward for the next one. You can download the entire project for free and try it out for yourself.

Related Articles

Sorting Algorithms

How To Make Insertion Sort Algorithm In C# – Made Easy

Insertion sort is one of the simplest sorting algorithms for sorting single dimensional arrays of various different data types.

Posted on by Andraz Krzisnik
Digital Image Watermarking

How To Make Image Watermarking Work With C#

This tutorial shows how image watermarking works by implementing it with C# and we describe various purposes that watermarks have as well.

Posted on by Andraz Krzisnik